本文整理汇总了C++中setns函数的典型用法代码示例。如果您正苦于以下问题:C++ setns函数的具体用法?C++ setns怎么用?C++ setns使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setns函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: mount
int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data)
{
int (*orig_mount)(const char *, const char *,
const char *, unsigned long,
const void *);
int targetfd = -1;
orig_mount = dlsym(RTLD_NEXT, "mount");
if (!strcmp("fuse.glusterfs", filesystemtype)) {
openlog ("mount.so", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
if (!open_target_fd(&targetfd, "/proc/1/ns/mnt")){
if (setns(targetfd, CLONE_NEWNS)){
syslog(LOG_NOTICE, "setns failed for filesystem: %s", filesystemtype);
}else {
syslog(LOG_NOTICE, "setns succeeded for filesystem: %s", filesystemtype);
}
}else {
syslog(LOG_NOTICE, "failed to open ns for filesystem: %s", filesystemtype);
}
closelog();
}
if (orig_mount) {
return orig_mount(source, target, filesystemtype, mountflags, data);
}else {
return -ENOENT;
}
}
开发者ID:ricktrintech,项目名称:install-glusterfs-on-fc21,代码行数:32,代码来源:mymount.c
示例2: rwnetns_change
/**
* This function is called whenever the tasklet wants to change its net namespace. The
* caller must hold the mutex. We need to assert that...
* @param[in] name
* @return 0 if success
* @return errno if failed
*/
int rwnetns_change(const char *name)
{
int new_netfd = 0;
static int tried = 0;
int ret = -1;
if (!name || name[0] == 0){
return 0;
}
tryagain:
//AKKI assert that the pthread lock has been taken.
new_netfd = rwnetns_get_netfd(name);
if (new_netfd > 0){
setns(new_netfd, CLONE_NEWNET);
ret = 0;
}else if (!tried){
RWNETNS_UNLOCK();
rwnetns_create_context(name);
RWNETNS_LOCK();
tried = 1;
goto tryagain;
}
if (new_netfd > 0){
close(new_netfd);
}
return ret;
}
开发者ID:RIFTIO,项目名称:RIFT.ware,代码行数:36,代码来源:rwnetns.c
示例3: set_netns
int set_netns(char *ns) {
int nsfd, perm_issue;
char *nspath;
if (bad_nsname(ns)) {
fprintf(stderr, PROGRAM ": namespace names can't contain '/', be empty, or be '.' or '..'.\n");
return 0;
}
if (asprintf(&nspath, "%s/%s", NS_PATH, ns) == -1) {
perror(PROGRAM ": asprintf");
return 0;
}
if ((nsfd = open(nspath, O_RDONLY | O_CLOEXEC)) == -1) {
fprintf(stderr, PROGRAM ": open(\"%s\"): %s\n", nspath, strerror(errno));
return 0;
}
free(nspath);
if (setns(nsfd, CLONE_NEWNET) == -1) {
perm_issue = errno == EPERM;
perror(PROGRAM ": setns");
if (perm_issue)
fprintf(stderr, "\nis the " PROGRAM " binary missing the setuid bit?\n");
return 0;
} else {
return 1;
}
}
开发者ID:ausbin,项目名称:nsdo,代码行数:33,代码来源:nsdo.c
示例4: mkimplstmt
Node *
mkimplstmt(Srcloc loc, Node *name, Type *t, Type **aux, size_t naux, Node **decls, size_t ndecls)
{
Node *n;
size_t i;
n = mknode(loc, Nimpl);
n->impl.traitname = name;
n->impl.type = t;
n->impl.aux = aux;
n->impl.naux = naux;
n->impl.decls = decls;
n->impl.ndecls = ndecls;
lappend(&impltab, &nimpltab, n);
if (hasparams(t)) {
n->impl.env = mkenv();
bindtype(n->impl.env, t);
}
for (i = 0; i < naux; i++)
if (hasparams(aux[i]))
bindtype(n->impl.env, aux[i]);
for (i = 0; i < ndecls; i++) {
if (name->name.ns)
setns(decls[i]->decl.name, name->name.ns);
if (decls[i]->decl.env)
decls[i]->decl.env->super = n->impl.env;
}
return n;
}
开发者ID:oridb,项目名称:mc,代码行数:29,代码来源:node.c
示例5: rwnetns_change_fd
/**
* This function is called whenever the tasklet wants to change its net namespace. The caller must hold the pthread mutex. We need to assert that...
* @param[in] fd
* @return 0 if success
* @return errno if failed
*/
int rwnetns_change_fd(int fd)
{
//AKKI assert that the pthread lock has been taken.
RW_ASSERT(fd > 0);
setns(fd, CLONE_NEWNET);
return 0;
}
开发者ID:RIFTIO,项目名称:RIFT.ware,代码行数:13,代码来源:rwnetns.c
示例6: nif_setns
static ERL_NIF_TERM
nif_setns(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
#ifdef HAVE_SETNS
int fd;
ErlNifBinary buf = {0};
int nstype = 0;
int errnum = 0;
if (!enif_inspect_iolist_as_binary(env, argv[0], &buf))
return enif_make_badarg(env);
if (!enif_get_int(env, argv[1], &nstype))
return enif_make_badarg(env);
PROCKET_REALLOC(buf, buf.size+1);
buf.data[buf.size-1] = '\0';
fd = open((const char*)buf.data, O_RDONLY); /* Get descriptor for namespace */
if (fd < 0)
return error_tuple(env, errno);
if (setns(fd, nstype) == -1) {
errnum = errno;
(void)close(fd);
return error_tuple(env, errnum);
}
(void)close(fd);
return atom_ok;
#else
return error_tuple(env, ENOTSUP);
#endif
}
开发者ID:fhunleth,项目名称:procket,代码行数:35,代码来源:procket.c
示例7: virProcessSetNamespaces
int virProcessSetNamespaces(size_t nfdlist,
int *fdlist)
{
size_t i;
if (nfdlist == 0) {
virReportInvalidArg(nfdlist, "%s",
_("Expected at least one file descriptor"));
return -1;
}
for (i = 0; i < nfdlist; i++) {
if (fdlist[i] < 0)
continue;
/* We get EINVAL if new NS is same as the current
* NS, or if the fd namespace doesn't match the
* type passed to setns()'s second param. Since we
* pass 0, we know the EINVAL is harmless
*/
if (setns(fdlist[i], 0) < 0 &&
errno != EINVAL) {
virReportSystemError(errno, "%s",
_("Unable to join domain namespace"));
return -1;
}
}
return 0;
}
开发者ID:Antique,项目名称:libvirt,代码行数:28,代码来源:virprocess.c
示例8: lxc_attach_to_ns
int lxc_attach_to_ns(pid_t pid)
{
char path[MAXPATHLEN];
char *ns[] = { "pid", "mnt", "net", "ipc", "uts" };
const int size = sizeof(ns) / sizeof(char *);
int fd[size];
int i;
snprintf(path, MAXPATHLEN, "/proc/%d/ns", pid);
if (access(path, X_OK)) {
ERROR("Does this kernel version support 'attach' ?");
return -1;
}
for (i = 0; i < size; i++) {
snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid, ns[i]);
fd[i] = open(path, O_RDONLY);
if (fd[i] < 0) {
SYSERROR("failed to open '%s'", path);
return -1;
}
}
for (i = 0; i < size; i++) {
if (setns(fd[i], 0)) {
SYSERROR("failed to set namespace '%s'", ns[i]);
return -1;
}
close(fd[i]);
}
return 0;
}
开发者ID:coconutpilot,项目名称:lxc-gentoo-new,代码行数:34,代码来源:attach.c
示例9: netns_exec
static int netns_exec(int argc, char **argv)
{
/* Setup the proper environment for apps that are not netns
* aware, and execute a program in that environment.
*/
const char *name, *cmd;
char net_path[MAXPATHLEN];
int netns;
if (argc < 1) {
fprintf(stderr, "No netns name specified\n");
return EXIT_FAILURE;
}
if (argc < 2) {
fprintf(stderr, "No command specified\n");
return EXIT_FAILURE;
}
name = argv[0];
cmd = argv[1];
snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
netns = open(net_path, O_RDONLY);
if (netns < 0) {
fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
name, strerror(errno));
return EXIT_FAILURE;
}
if (setns(netns, CLONE_NEWNET) < 0) {
fprintf(stderr, "seting the network namespace \"%s\" failed: %s\n",
name, strerror(errno));
return EXIT_FAILURE;
}
if (unshare(CLONE_NEWNS) < 0) {
fprintf(stderr, "unshare failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
/* Don't let any mounts propogate back to the parent */
if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
/* Mount a version of /sys that describes the network namespace */
if (umount2("/sys", MNT_DETACH) < 0) {
fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
return EXIT_FAILURE;
}
/* Setup bind mounts for config files in /etc */
bind_etc(name);
if (execvp(cmd, argv + 1) < 0)
fprintf(stderr, "exec of \"%s\" failed: %s\n",
cmd, strerror(errno));
return EXIT_FAILURE;
}
开发者ID:Azzik,项目名称:iproute2,代码行数:60,代码来源:ipnetns.c
示例10: rwnetns_socket
/**
* This function is called to open a socket in a given namespace
* @param[in] namespace name
* @param[in] domain
* @param[in] type
* @param[in] protocol
* @return fd value
*/
int rwnetns_socket(const char *name, int domain, int type, int proto)
{
int new_netfd = 0;
int ret_fd = -1;
int currfd = 0;
int tried = 0;
RWNETNS_LOCK();
currfd = rwnetns_get_current_netfd();
tryagain:
if (!name || name[0] == 0){
new_netfd = 0;
}else{
new_netfd = rwnetns_get_netfd(name);
}
if (new_netfd > 0){
setns(new_netfd, CLONE_NEWNET);
ret_fd = socket(domain, type, proto);
setns(currfd, CLONE_NEWNET);
}else if (new_netfd < 0){
if (!tried){
RWNETNS_UNLOCK();
rwnetns_create_context(name);
RWNETNS_LOCK();
tried = 1;
goto tryagain;
}else{
goto ret;
}
}else if (new_netfd == 0){
ret_fd = socket(domain, type, proto);
}
ret:
if (currfd > 0){
close(currfd);
}
if (new_netfd > 0){
close(new_netfd);
}
RWNETNS_UNLOCK();
return ret_fd;
}
开发者ID:RIFTIO,项目名称:RIFT.ware,代码行数:54,代码来源:rwnetns.c
示例11: rwnetns_open
/**
* This function is called to open a socket in a given namespace
* @param[in] namespace name
* @param[in] domain
* @param[in] type
* @param[in] protocol
* @return fd value
*/
int rwnetns_open(const char *name, char *pathname, int flags)
{
int new_netfd = 0;
int ret_fd = -1;
int currfd = 0;
static int tried = 0;
RWNETNS_LOCK();
currfd = rwnetns_get_current_netfd();
tryagain:
if (!name || name[0] == 0){
new_netfd = 0;
}else{
new_netfd = rwnetns_get_netfd(name);
}
if (new_netfd > 0){
setns(new_netfd, CLONE_NEWNET);
ret_fd = open(pathname, flags);
setns(currfd, CLONE_NEWNET);
}else if (new_netfd < 0){
if (!tried){
RWNETNS_UNLOCK();
rwnetns_create_context(name);
RWNETNS_LOCK();
tried = 1;
goto tryagain;
}else{
goto ret;
}
}else if (new_netfd == 0){
ret_fd = open(pathname, flags);
}
ret:
if (currfd > 0){
close(currfd);
}
if (new_netfd > 0){
close(new_netfd);
}
RWNETNS_UNLOCK();
return ret_fd;
}
开发者ID:RIFTIO,项目名称:RIFT.ware,代码行数:54,代码来源:rwnetns.c
示例12: lxc_attach_to_ns
static int lxc_attach_to_ns(pid_t pid, int which)
{
int fd[LXC_NS_MAX];
int i, j, saved_errno;
if (access("/proc/self/ns", X_OK)) {
ERROR("Does this kernel version support namespaces?");
return -1;
}
for (i = 0; i < LXC_NS_MAX; i++) {
/* Ignore if we are not supposed to attach to that namespace. */
if (which != -1 && !(which & ns_info[i].clone_flag)) {
fd[i] = -1;
continue;
}
fd[i] = lxc_preserve_ns(pid, ns_info[i].proc_name);
if (fd[i] < 0) {
saved_errno = errno;
/* Close all already opened file descriptors before we
* return an error, so we don't leak them.
*/
for (j = 0; j < i; j++)
close(fd[j]);
errno = saved_errno;
SYSERROR("Failed to open namespace: \"%s\".", ns_info[i].proc_name);
return -1;
}
}
for (i = 0; i < LXC_NS_MAX; i++) {
if (fd[i] < 0)
continue;
if (setns(fd[i], 0) < 0) {
saved_errno = errno;
for (j = i; j < LXC_NS_MAX; j++)
close(fd[j]);
errno = saved_errno;
SYSERROR("Failed to attach to namespace \"%s\".", ns_info[i].proc_name);
return -1;
}
DEBUG("Attached to namespace \"%s\".", ns_info[i].proc_name);
close(fd[i]);
}
return 0;
}
开发者ID:LynxChaus,项目名称:lxc,代码行数:56,代码来源:attach.c
示例13: make_sock_for
/*
* This routine is to create PF_UNIX/SOCK_SEQPACKET socket
* in the target net namespace
*/
static int make_sock_for(int pid)
{
int ret, mfd, fd, sk = -1;
char p[32];
pr_debug("Preparing seqsk for %d\n", pid);
sprintf(p, "/proc/%d/ns/net", pid);
fd = open(p, O_RDONLY);
if (fd < 0) {
pr_perror("Can't open %p", p);
goto out;
}
mfd = open("/proc/self/ns/net", O_RDONLY);
if (mfd < 0) {
pr_perror("Can't open self netns");
goto out_c;
}
if (setns(fd, CLONE_NEWNET)) {
pr_perror("Can't setup target netns");
goto out_cm;
}
sk = socket(PF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0);
if (sk < 0)
pr_perror("Can't create seqsk");
ret = setns(mfd, CLONE_NEWNET);
if (ret) {
pr_perror("Can't restore former netns");
if (sk >= 0)
close(sk);
sk = -1;
}
out_cm:
close(mfd);
out_c:
close(fd);
out:
return sk;
}
开发者ID:Snorch,项目名称:criu,代码行数:47,代码来源:infect.c
示例14: child
static int child(void *arg) {
int fd;
fd = open((char *) arg, O_RDONLY);
setns(fd, 0);
execlp("bash", "bash", (char *) NULL);
return 0;
}
开发者ID:matthewbentley,项目名称:container_talk,代码行数:10,代码来源:test_net.c
示例15: netns_change
static int netns_change(const char *nsname, int fd_global_netns)
{
/* If you need to change current netns to global netns:
* set the first argument "NULL", the second argument
* is file descriptor of global netns (it must be saved
* before first call of this function)
* If you need to change current netns to another netns:
* set the first argument as netns which you need,
* the second argument may be any
*/
char netns_path[MAXPATHLEN];
int fd;
/* Join to global network namespace
* if (network namespace "nsname" == NULL &&
* file descriptor of global network namespace
* "fd_global_netns" >-1)
* else exit function with error -1
* if (file descriptor of global network namespace
* "fd_global_netns" < 0)
*/
if (nsname == NULL) {
fprintf(stdout,"*** fd_global_netns is:%d\n",fd_global_netns);
if (setns(fd_global_netns, CLONE_NEWNET) == -1) {
fprintf(stderr, "*** Failed to set a global network namespace : %s\n",
strerror(errno));
return -2;
}
return 0;
}
/* Get path for network namespace "nsname" */
snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, nsname);
/* Create the base netns directory if it doesn't exist */
mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
/* Create the filesystem state */
fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
if (fd < 0) {
fprintf(stderr, "*** Cannot create namespace file \"%s\": %s\n",
netns_path, strerror(errno));
return -1;
}
close(fd);
/* Join to network namespace "nsname" */
if (unshare(CLONE_NEWNET) < 0) {
fprintf(stderr, "*** Failed to create a new network namespace \"%s\": %s\n",
nsname, strerror(errno));
return -1;
}
return 0;
}
开发者ID:BrezhnevSA,项目名称:launch_in_netns,代码行数:55,代码来源:launch_in_ns_v10.c
示例16: netns_exec
static int netns_exec(int argc, char **argv)
{
/* Setup the proper environment for apps that are not netns
* aware, and execute a program in that environment.
*/
const char *name, *cmd;
char net_path[MAXPATHLEN];
int netns;
if (argc < 1) {
fprintf(stderr, "No netns name specified\n");
return -1;
}
if (argc < 2) {
fprintf(stderr, "No cmd specified\n");
return -1;
}
name = argv[0];
cmd = argv[1];
snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
netns = open(net_path, O_RDONLY);
if (netns < 0) {
fprintf(stderr, "Cannot open network namespace: %s\n",
strerror(errno));
return -1;
}
if (setns(netns, CLONE_NEWNET) < 0) {
fprintf(stderr, "seting the network namespace failed: %s\n",
strerror(errno));
return -1;
}
/*
if (unshare(CLONE_NEWNS) < 0) {
fprintf(stderr, "unshare failed: %s\n", strerror(errno));
return -1;
}
*/
/* Mount a version of /sys that describes the network namespace */
if (umount2("/sys", MNT_DETACH) < 0) {
fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
return -1;
}
if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
return -1;
}
/* Setup bind mounts for config files in /etc */
bind_etc(name);
if (execvp(cmd, argv + 1) < 0)
fprintf(stderr, "exec of %s failed: %s\n",
cmd, strerror(errno));
exit(-1);
}
开发者ID:Tuccro,项目名称:LightVPN,代码行数:55,代码来源:ipnetns.c
示例17: sc_reassociate_with_pid1_mount_ns
void sc_reassociate_with_pid1_mount_ns(void)
{
int init_mnt_fd SC_CLEANUP(sc_cleanup_close) = -1;
int self_mnt_fd SC_CLEANUP(sc_cleanup_close) = -1;
debug("checking if the current process shares mount namespace"
" with the init process");
init_mnt_fd = open("/proc/1/ns/mnt",
O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_PATH);
if (init_mnt_fd < 0) {
die("cannot open mount namespace of the init process (O_PATH)");
}
self_mnt_fd = open("/proc/self/ns/mnt",
O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_PATH);
if (self_mnt_fd < 0) {
die("cannot open mount namespace of the current process (O_PATH)");
}
char init_buf[128] = { 0 };
char self_buf[128] = { 0 };
memset(init_buf, 0, sizeof init_buf);
if (readlinkat(init_mnt_fd, "", init_buf, sizeof init_buf) < 0) {
if (errno == ENOENT) {
// According to namespaces(7) on a pre 3.8 kernel the namespace
// files are hardlinks, not sylinks. If that happens readlinkat
// fails with ENOENT. As a quick workaround for this special-case
// functionality, just bail out and do nothing without raising an
// error.
return;
}
die("cannot perform readlinkat() on the mount namespace file "
"descriptor of the init process");
}
memset(self_buf, 0, sizeof self_buf);
if (readlinkat(self_mnt_fd, "", self_buf, sizeof self_buf) < 0) {
die("cannot perform readlinkat() on the mount namespace file "
"descriptor of the current process");
}
if (memcmp(init_buf, self_buf, sizeof init_buf) != 0) {
debug("the current process does not share mount namespace with "
"the init process, re-association required");
// NOTE: we cannot use O_NOFOLLOW here because that file will always be a
// symbolic link. We actually want to open it this way.
int init_mnt_fd_real SC_CLEANUP(sc_cleanup_close) = -1;
init_mnt_fd_real = open("/proc/1/ns/mnt", O_RDONLY | O_CLOEXEC);
if (init_mnt_fd_real < 0) {
die("cannot open mount namespace of the init process");
}
if (setns(init_mnt_fd_real, CLONE_NEWNS) < 0) {
die("cannot re-associate the mount namespace with the init process");
}
} else {
debug("re-associating is not required");
}
}
开发者ID:alfonsosanchezbeato,项目名称:snappy,代码行数:55,代码来源:ns-support.c
示例18: restore_ns
int restore_ns(int rst, struct ns_desc *nd)
{
int ret;
ret = setns(rst, nd->cflag);
if (ret < 0)
pr_perror("Can't restore ns back");
close(rst);
return ret;
}
开发者ID:KKoukiou,项目名称:criu-remote,代码行数:12,代码来源:namespaces.c
示例19: attachToNS
/* Attach to the specified namespace FD path */
int attachToNS(char *path) {
int nsid;
nsid = open(path, O_RDONLY);
if (nsid < 0) {
perror(path);
return 1;
}
if (setns(nsid, 0) != 0) {
perror("setns");
return 1;
}
return 0;
}
开发者ID:Mohdrz,项目名称:miniNExT,代码行数:14,代码来源:mxexec.c
示例20: _setns
static int
_setns (NMPNetns *self, int type)
{
char buf[100];
int fd;
NMPNetnsPrivate *priv = NMP_NETNS_GET_PRIVATE (self);
nm_assert (NM_IN_SET (type, _CLONE_NS_ALL_V));
fd = (type == CLONE_NEWNET) ? priv->fd_net : priv->fd_mnt;
_LOGt (self, "set netns(%s, %d)", _ns_types_to_str (type, 0, buf), fd);
return setns (fd, type);
}
开发者ID:lkundrak,项目名称:NetworkManager,代码行数:15,代码来源:nmp-netns.c
注:本文中的setns函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论