static BOOL irix_oplocks_available(void)
{
	int fd;
	int pfd[2];
	pstring tmpname;

	set_effective_capability(KERNEL_OPLOCK_CAPABILITY);

	slprintf(tmpname,sizeof(tmpname)-1, "%s/koplock.%d", lp_lockdir(),
		 (int)sys_getpid());

	if(pipe(pfd) != 0) {
		DEBUG(0,("check_kernel_oplocks: Unable to create pipe. Error "
			 "was %s\n",
			 strerror(errno) ));
		return False;
	}

	if((fd = sys_open(tmpname, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600)) < 0) {
		DEBUG(0,("check_kernel_oplocks: Unable to open temp test file "
			 "%s. Error was %s\n",
			 tmpname, strerror(errno) ));
		unlink( tmpname );
		close(pfd[0]);
		close(pfd[1]);
		return False;
	}

	unlink(tmpname);

	if(sys_fcntl_long(fd, F_OPLKREG, pfd[1]) == -1) {
		DEBUG(0,("check_kernel_oplocks: Kernel oplocks are not "
			 "available on this machine. Disabling kernel oplock "
			 "support.\n" ));
		close(pfd[0]);
		close(pfd[1]);
		close(fd);
		return False;
	}

	if(sys_fcntl_long(fd, F_OPLKACK, OP_REVOKE) < 0 ) {
		DEBUG(0,("check_kernel_oplocks: Error when removing kernel "
			 "oplock. Error was %s. Disabling kernel oplock "
			 "support.\n", strerror(errno) ));
		close(pfd[0]);
		close(pfd[1]);
		close(fd);
		return False;
	}

	close(pfd[0]);
	close(pfd[1]);
	close(fd);

	return True;
}
示例#2
0
int linux_setlease(int fd, int leasetype)
{
	int ret;

	ret = fcntl(fd, F_SETLEASE, leasetype);
	if (ret == -1 && errno == EACCES) {
		set_effective_capability(LEASE_CAPABILITY);
		ret = fcntl(fd, F_SETLEASE, leasetype);
	}

	return ret;
}
示例#3
0
文件: dmapi.c 项目: 0x24bin/winexe-1
/* 
   Initialise DMAPI session. The session is persistant kernel state, 
   so it might already exist, in which case we merely want to 
   reconnect to it. This function should be called as root.
*/
static int dmapi_init_session(struct smbd_dmapi_context *ctx)
{
	char	buf[DM_SESSION_INFO_LEN];
	size_t	buflen;
	uint	    nsessions = 5;
	dm_sessid_t *sessions = NULL;
	char    *version;
	char    *session_name;
	TALLOC_CTX *tmp_ctx = talloc_new(NULL);

	int i, err;

	if (ctx->session_num == 0) {
		session_name = talloc_strdup(tmp_ctx, DMAPI_SESSION_NAME);
	} else {
		session_name = talloc_asprintf(tmp_ctx, "%s%u", DMAPI_SESSION_NAME,
					       ctx->session_num);
	}

	if (session_name == NULL) {
		DEBUG(0,("Out of memory in dmapi_init_session\n"));
		talloc_free(tmp_ctx);
		return -1;
	}
 

	if (dm_init_service(&version) < 0) {
		DEBUG(0, ("dm_init_service failed - disabling DMAPI\n"));
		talloc_free(tmp_ctx);
		return -1;
	}

	ZERO_STRUCT(buf);

	/* Fetch kernel DMAPI sessions until we get any of them */
	do {
		dm_sessid_t *new_sessions;
		nsessions *= 2;
		new_sessions = TALLOC_REALLOC_ARRAY(tmp_ctx, sessions, 
						    dm_sessid_t, nsessions);
		if (new_sessions == NULL) {
			talloc_free(tmp_ctx);
			return -1;
		}

		sessions = new_sessions;
		err = dm_getall_sessions(nsessions, sessions, &nsessions);
	} while (err == -1 && errno == E2BIG);

	if (err == -1) {
		DEBUGADD(DMAPI_TRACE,
			("failed to retrieve DMAPI sessions: %s\n",
			strerror(errno)));
		talloc_free(tmp_ctx);
		return -1;
	}

	/* Look through existing kernel DMAPI sessions to find out ours */
	for (i = 0; i < nsessions; ++i) {
		err = dm_query_session(sessions[i], sizeof(buf), buf, &buflen);
		buf[sizeof(buf) - 1] = '\0';
		if (err == 0 && strcmp(session_name, buf) == 0) {
			ctx->session = sessions[i];
			DEBUGADD(DMAPI_TRACE,
				("attached to existing DMAPI session "
				 "named '%s'\n", buf));
			break;
		}
	}

	/* No session already defined. */
	if (ctx->session == DM_NO_SESSION) {
		err = dm_create_session(DM_NO_SESSION, 
					session_name,
					&ctx->session);
		if (err < 0) {
			DEBUGADD(DMAPI_TRACE,
				("failed to create new DMAPI session: %s\n",
				strerror(errno)));
			ctx->session = DM_NO_SESSION;
			talloc_free(tmp_ctx);
			return -1;
		}

		DEBUG(0, ("created new DMAPI session named '%s' for %s\n",
			  session_name, version));
	}

	if (ctx->session != DM_NO_SESSION) {
		set_effective_capability(DMAPI_ACCESS_CAPABILITY);
	}

	/* 
	   Note that we never end the DMAPI session. It gets re-used if possiblie. 
	   DMAPI session is a kernel resource that is usually lives until server reboot
	   and doesn't get destroed when an application finishes.

	   However, we free list of references to DMAPI sessions we've got from the kernel
	   as it is not needed anymore once we have found (or created) our session.
	 */

	talloc_free(tmp_ctx);
	return 0;
}
示例#4
0
/*
 * public function to get linux lease capability. Needed by some VFS modules (eg. gpfs.c)
 */
void linux_set_lease_capability(void)
{
	set_effective_capability(LEASE_CAPABILITY);
}
示例#5
0
static bool irix_oplocks_available(void)
{
    int fd;
    int pfd[2];
    TALLOC_CTX *ctx = talloc_stackframe();
    char *tmpname = NULL;

    set_effective_capability(KERNEL_OPLOCK_CAPABILITY);

    tmpname = talloc_asprintf(ctx,
                              "%s/koplock.%d",
                              lp_lockdir(),
                              (int)getpid());
    if (!tmpname) {
        TALLOC_FREE(ctx);
        return False;
    }

    if(pipe(pfd) != 0) {
        DEBUG(0,("check_kernel_oplocks: Unable to create pipe. Error "
                 "was %s\n",
                 strerror(errno) ));
        TALLOC_FREE(ctx);
        return False;
    }

    if((fd = open(tmpname, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600)) < 0) {
        DEBUG(0,("check_kernel_oplocks: Unable to open temp test file "
                 "%s. Error was %s\n",
                 tmpname, strerror(errno) ));
        unlink( tmpname );
        close(pfd[0]);
        close(pfd[1]);
        TALLOC_FREE(ctx);
        return False;
    }

    unlink(tmpname);

    TALLOC_FREE(ctx);

    if(sys_fcntl_long(fd, F_OPLKREG, pfd[1]) == -1) {
        DEBUG(0,("check_kernel_oplocks: Kernel oplocks are not "
                 "available on this machine. Disabling kernel oplock "
                 "support.\n" ));
        close(pfd[0]);
        close(pfd[1]);
        close(fd);
        return False;
    }

    if(sys_fcntl_long(fd, F_OPLKACK, OP_REVOKE) < 0 ) {
        DEBUG(0,("check_kernel_oplocks: Error when removing kernel "
                 "oplock. Error was %s. Disabling kernel oplock "
                 "support.\n", strerror(errno) ));
        close(pfd[0]);
        close(pfd[1]);
        close(fd);
        return False;
    }

    close(pfd[0]);
    close(pfd[1]);
    close(fd);

    return True;
}
示例#6
0
/* 
   Initialise DMAPI session. The session is persistant kernel state,
   so it might already exist, in which case we merely want to
   reconnect to it. This function should be called as root.
 */
static int dmapi_init_session(void)
{
	char	buf[DM_SESSION_INFO_LEN];
	size_t	buflen;
	uint	    nsessions = 5;
	dm_sessid_t *sessions = NULL;
	int i, err;
	char *version;

	if (dm_init_service(&version) < 0) {
		DEBUG(0,("dm_init_service failed - disabling DMAPI\n"));
		return -1;
	}

	ZERO_STRUCT(buf);

	do {
		dm_sessid_t *new_sessions;
		nsessions *= 2;
		new_sessions = TALLOC_REALLOC_ARRAY(NULL, sessions, 
						    dm_sessid_t, nsessions);
		if (new_sessions == NULL) {
			talloc_free(sessions);
			return -1;
		}
		sessions = new_sessions;
		err = dm_getall_sessions(nsessions, sessions, &nsessions);
	} while (err == -1 && errno == E2BIG);

	if (err == -1) {
		DEBUGADD(DMAPI_TRACE,
			("failed to retrieve DMAPI sessions: %s\n",
			strerror(errno)));
		talloc_free(sessions);
		return -1;
	}

	for (i = 0; i < nsessions; ++i) {
		err = dm_query_session(sessions[i], sizeof(buf), buf, &buflen);
		buf[sizeof(buf) - 1] = '\0';
		if (err == 0 && strcmp(DMAPI_SESSION_NAME, buf) == 0) {
			samba_dmapi_session = sessions[i];
			DEBUGADD(DMAPI_TRACE,
				("attached to existing DMAPI session "
				 "named '%s'\n", buf));
			break;
		}
	}

	talloc_free(sessions);

	/* No session already defined. */
	if (samba_dmapi_session == DM_NO_SESSION) {
		err = dm_create_session(DM_NO_SESSION, DMAPI_SESSION_NAME,
					&samba_dmapi_session);
		if (err < 0) {
			DEBUGADD(DMAPI_TRACE,
				("failed to create new DMAPI session: %s\n",
				strerror(errno)));
			samba_dmapi_session = DM_NO_SESSION;
			return -1;
		}

		DEBUG(0,("created new DMAPI session named '%s' for %s\n", 
			 DMAPI_SESSION_NAME, version));
	}

	if (samba_dmapi_session != DM_NO_SESSION) {
		set_effective_capability(DMAPI_ACCESS_CAPABILITY);
	}

	/* 
	   Note that we never end the DMAPI session. It gets re-used
	   if possible
	 */

	return 0;
}