mirror of
https://repo.or.cz/socat.git
synced 2025-07-08 21:36:34 +00:00
New option chdir (cd)
This commit is contained in:
parent
e5cbf2feeb
commit
6125ed4e4e
11 changed files with 301 additions and 62 deletions
|
@ -44,29 +44,61 @@ int xio_set_namespace(
|
|||
return 0;
|
||||
}
|
||||
|
||||
int xio_apply_namespace(
|
||||
struct opt *opts)
|
||||
{
|
||||
int old_netfd;
|
||||
char *netns_name;
|
||||
char old_nspath[PATH_MAX];
|
||||
int rc;
|
||||
|
||||
if (retropt_string(opts, OPT_SET_NETNS, &netns_name) < 0)
|
||||
return 0;
|
||||
|
||||
/* Get path describing current namespace */
|
||||
snprintf(old_nspath, sizeof(old_nspath)-1, "/proc/"F_pid"/ns/net",
|
||||
Getpid());
|
||||
|
||||
/* Get a file descriptor to current ns for later reset */
|
||||
old_netfd = Open(old_nspath, O_RDONLY|O_CLOEXEC, 000);
|
||||
if (old_netfd < 0) {
|
||||
Error2("open(%s, O_RDONLY|O_CLOEXEC): %s",
|
||||
old_nspath, strerror(errno));
|
||||
free(netns_name);
|
||||
return -1;
|
||||
}
|
||||
if (old_netfd == 0) {
|
||||
/* 0 means not netns option, oops */
|
||||
Error1("%s(): INTERNAL", __func__);
|
||||
free(netns_name);
|
||||
Close(old_netfd);
|
||||
return -1;
|
||||
}
|
||||
rc = xio_set_namespace("netns", netns_name);
|
||||
free(netns_name);
|
||||
if (rc < 0) {
|
||||
Close(old_netfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return old_netfd;
|
||||
}
|
||||
|
||||
/* Sets the given namespace to that of process 1, this is assumed to be the
|
||||
systems default.
|
||||
Returns 0 on success, or -1 on error. */
|
||||
int xio_reset_namespace(
|
||||
const char *nstype)
|
||||
int saved_netfd)
|
||||
{
|
||||
char nspath[PATH_MAX];
|
||||
int nsfd;
|
||||
int rc;
|
||||
|
||||
snprintf(nspath, sizeof(nspath)-1, "/proc/1/ns/%s", nstype);
|
||||
Info("switching back to default namespace");
|
||||
nsfd = Open(nspath, O_RDONLY|O_CLOEXEC, 000);
|
||||
if (nsfd < 0) {
|
||||
Error2("open(%s, O_RDONLY|O_CLOEXEC): %s", nspath, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
rc = Setns(nsfd, CLONE_NEWNET);
|
||||
rc = Setns(saved_netfd, CLONE_NEWNET);
|
||||
if (rc < 0) {
|
||||
Error2("setns(%d, CLONE_NEWNET): %s", nsfd, strerror(errno));
|
||||
Close(nsfd);
|
||||
Error2("xio_reset_namespace(%d): %s", saved_netfd, strerror(errno));
|
||||
Close(saved_netfd);
|
||||
return STAT_NORETRY;
|
||||
}
|
||||
Close(nsfd);
|
||||
Close(saved_netfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue