本文整理汇总了C++中setgroups函数的典型用法代码示例。如果您正苦于以下问题:C++ setgroups函数的具体用法?C++ setgroups怎么用?C++ setgroups使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setgroups函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: daemonize_trqauthd
int daemonize_trqauthd(char *server_ip, int server_port, void *(*process_meth)(void *))
{
int gid;
pid_t pid;
int rc;
char error_buf[1024];
umask(022);
gid = getgid();
/* secure supplemental groups */
if(setgroups(1, (gid_t *)&gid) != 0)
{
fprintf(stderr, "Unable to drop secondary groups. Some MAC framework is active?\n");
snprintf(error_buf, sizeof(error_buf),
"setgroups(group = %lu) failed: %s\n",
(unsigned long)gid, strerror(errno));
fprintf(stderr, "%s\n", error_buf);
return(1);
}
if (getenv("PBSDEBUG") != NULL)
debug_mode = TRUE;
if (debug_mode == FALSE)
{
pid = fork();
if(pid > 0)
{
/* parent. We are done */
return(0);
}
else if (pid < 0)
{
/* something went wrong */
fprintf(stderr, "fork failed. errno = %d\n", errno);
return(PBSE_RMSYSTEM);
}
else
{
fprintf(stderr, "trqauthd daemonized - port %d\n", server_port);
/* If I made it here I am the child */
fclose(stdin);
fclose(stdout);
fclose(stderr);
/* We closed 0 (stdin), 1 (stdout), and 2 (stderr). fopen should give us
0, 1 and 2 in that order. this is a UNIX practice */
(void)fopen("/dev/null", "r");
(void)fopen("/dev/null", "r");
(void)fopen("/dev/null", "r");
}
}
else
{
fprintf(stderr, "trqauthd port: %d\n", server_port);
}
/* start the listener */
rc = start_listener(server_ip, server_port, process_meth);
if(rc != PBSE_NONE)
{
openlog("daemonize_trqauthd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
syslog(LOG_ALERT, "trqauthd could not start: %d\n", rc);
exit(-1);
}
exit(0);
}
示例2: main
//.........这里部分代码省略.........
log_error_write(srv, __FILE__, __LINE__, "s",
"I will not set uid to 0\n");
return -1;
}
}
if (srv->srvconf.groupname->used) {
if (NULL == (grp = getgrnam(srv->srvconf.groupname->ptr))) {
log_error_write(srv, __FILE__, __LINE__, "sb",
"can't find groupname", srv->srvconf.groupname);
return -1;
}
if (grp->gr_gid == 0) {
log_error_write(srv, __FILE__, __LINE__, "s",
"I will not set gid to 0\n");
return -1;
}
}
#endif
/* we need root-perms for port < 1024 */
if (0 != network_init(srv)) {
plugins_free(srv);
server_free(srv);
return -1;
}
#ifdef HAVE_PWD_H
/*
* Change group before chroot, when we have access
* to /etc/group
* */
if (srv->srvconf.groupname->used) {
setgid(grp->gr_gid);
setgroups(0, NULL);
if (srv->srvconf.username->used) {
initgroups(srv->srvconf.username->ptr, grp->gr_gid);
}
}
#endif
#ifdef HAVE_CHROOT
if (srv->srvconf.changeroot->used) {
tzset();
if (-1 == chroot(srv->srvconf.changeroot->ptr)) {
log_error_write(srv, __FILE__, __LINE__, "ss", "chroot failed: ", strerror(errno));
return -1;
}
if (-1 == chdir("/")) {
log_error_write(srv, __FILE__, __LINE__, "ss", "chdir failed: ", strerror(errno));
return -1;
}
}
#endif
#ifdef HAVE_PWD_H
/* drop root privs */
if (srv->srvconf.username->used) {
setuid(pwd->pw_uid);
}
#endif
#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_DUMPABLE)
/**
* on IRIX 6.5.30 they have prctl() but no DUMPABLE
*/
if (srv->srvconf.enable_cores) {
prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
}
示例3: main
//.........这里部分代码省略.........
client_set_nameserver(&nameservaddr, nameservaddr_len);
} else {
warnx("No nameserver found - not connected to any network?\n");
usage();
/* NOTREACHED */
}
if(check_topdomain(topdomain, &errormsg)) {
warnx("Invalid topdomain: %s", errormsg);
usage();
/* NOTREACHED */
}
client_set_selecttimeout(selecttimeout);
client_set_lazymode(lazymode);
client_set_topdomain(topdomain);
client_set_hostname_maxlen(hostname_maxlen);
if (username != NULL) {
#ifndef WINDOWS32
if ((pw = getpwnam(username)) == NULL) {
warnx("User %s does not exist!\n", username);
usage();
/* NOTREACHED */
}
#endif
}
if (strlen(password) == 0) {
if (NULL != getenv(PASSWORD_ENV_VAR))
snprintf(password, sizeof(password), "%s", getenv(PASSWORD_ENV_VAR));
else
read_password(password, sizeof(password));
}
client_set_password(password);
if ((tun_fd = open_tun(device)) == -1) {
retval = 1;
goto cleanup1;
}
if ((dns_fd = open_dns_from_host(NULL, 0, nameservaddr.ss_family, AI_PASSIVE)) < 0) {
retval = 1;
goto cleanup2;
}
#ifdef OPENBSD
if (rtable > 0)
socket_setrtable(dns_fd, rtable);
#endif
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
fprintf(stderr, "Sending DNS queries for %s to %s\n",
topdomain, format_addr(&nameservaddr, nameservaddr_len));
if (client_handshake(dns_fd, raw_mode, autodetect_frag_size, max_downstream_frag_size)) {
retval = 1;
goto cleanup2;
}
if (client_get_conn() == CONN_RAW_UDP) {
fprintf(stderr, "Sending raw traffic directly to %s\n", client_get_raw_addr());
}
fprintf(stderr, "Connection setup complete, transmitting data.\n");
if (foreground == 0)
do_detach();
if (pidfile != NULL)
do_pidfile(pidfile);
if (newroot != NULL)
do_chroot(newroot);
if (username != NULL) {
#ifndef WINDOWS32
gid_t gids[1];
gids[0] = pw->pw_gid;
if (setgroups(1, gids) < 0 || setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
warnx("Could not switch to user %s!\n", username);
usage();
/* NOTREACHED */
}
#endif
}
if (context != NULL)
do_setcon(context);
client_tunnel(tun_fd, dns_fd);
cleanup2:
close_dns(dns_fd);
close_tun(tun_fd);
cleanup1:
return retval;
}
示例4: main
//.........这里部分代码省略.........
"killing prior auth (pid %d) of %s by user %s",
otherpid, ipsrc, otherluser);
if (kill((pid_t) otherpid, SIGTERM) == -1) {
syslog(LOG_INFO,
"could not kill process %d: (%m)",
otherpid);
}
}
/*
* we try to kill the previous process and acquire the lock
* for 10 seconds, trying once a second. if we can't after
* 10 attempts we log an error and give up
*/
if (++lockcnt > 10) {
syslog(LOG_ERR, "cannot kill previous authpf (pid %d)",
otherpid);
fclose(pidfp);
pidfp = NULL;
goto dogdeath;
}
sleep(1);
/* re-open, and try again. The previous authpf process
* we killed above should unlink the file and release
* it's lock, giving us a chance to get it now
*/
fclose(pidfp);
pidfp = NULL;
} while (1);
/* whack the group list */
gid = getegid();
if (setgroups(1, &gid) == -1) {
syslog(LOG_INFO, "setgroups: %s", strerror(errno));
do_death(0);
}
/* revoke privs */
uid = getuid();
#if defined(__OpenBSD__)
if (setresuid(uid, uid, uid) == -1) {
syslog(LOG_INFO, "setresuid: %s", strerror(errno));
do_death(0);
}
#else /* defined(__OpenBSD__) */
/* NetBSD */
if (setuid(uid) == -1) {
syslog(LOG_INFO, "setresuid: %s", strerror(errno));
do_death(0);
}
#endif /* defined(__OpenBSD__) */
openlog("authpf", LOG_PID | LOG_NDELAY, LOG_DAEMON);
if (!check_luser(PATH_BAN_DIR, luser) || !allowed_luser(luser)) {
syslog(LOG_INFO, "user %s prohibited", luser);
do_death(0);
}
if (read_config(config)) {
syslog(LOG_ERR, "invalid config file %s", PATH_CONFFILE);
do_death(0);
}
if (remove_stale_rulesets()) {
syslog(LOG_INFO, "error removing stale rulesets");
示例5: main
int
main (int argc, char *argv[])
{
int i, pidfd;
int blocked_signals[] = {SIGPIPE, 0};
int cc;
char *oom_value;
uint32_t slurmd_uid = 0;
uint32_t curr_uid = 0;
char time_stamp[256];
log_options_t lopts = LOG_OPTS_INITIALIZER;
/* NOTE: logfile is NULL at this point */
log_init(argv[0], lopts, LOG_DAEMON, NULL);
/*
* Make sure we have no extra open files which
* would be propagated to spawned tasks.
*/
cc = sysconf(_SC_OPEN_MAX);
for (i = 3; i < cc; i++)
close(i);
/*
* Drop supplementary groups.
*/
if (geteuid() == 0) {
if (setgroups(0, NULL) != 0) {
fatal("Failed to drop supplementary groups, "
"setgroups: %m");
}
} else {
debug("Not running as root. Can't drop supplementary groups");
}
/*
* Create and set default values for the slurmd global
* config variable "conf"
*/
conf = xmalloc(sizeof(slurmd_conf_t));
_init_conf();
conf->argv = &argv;
conf->argc = &argc;
if (_slurmd_init() < 0) {
error( "slurmd initialization failed" );
fflush( NULL );
exit(1);
}
slurmd_uid = slurm_get_slurmd_user_id();
curr_uid = getuid();
if (curr_uid != slurmd_uid) {
struct passwd *pw = NULL;
char *slurmd_user = NULL;
char *curr_user = NULL;
/* since when you do a getpwuid you get a pointer to a
* structure you have to do a xstrdup on the first
* call or your information will just get over
* written. This is a memory leak, but a fatal is
* called right after so it isn't that big of a deal.
*/
if ((pw=getpwuid(slurmd_uid)))
slurmd_user = xstrdup(pw->pw_name);
if ((pw=getpwuid(curr_uid)))
curr_user = pw->pw_name;
fatal("You are running slurmd as something "
"other than user %s(%d). If you want to "
"run as this user add SlurmdUser=%s "
"to the slurm.conf file.",
slurmd_user, slurmd_uid, curr_user);
}
init_setproctitle(argc, argv);
xsignal(SIGTERM, &_term_handler);
xsignal(SIGINT, &_term_handler);
xsignal(SIGHUP, &_hup_handler );
xsignal_block(blocked_signals);
debug3("slurmd initialization successful");
/*
* Become a daemon if desired.
* Do not chdir("/") or close all fd's
*/
if (conf->daemonize) {
if (daemon(1,1) == -1)
error("Couldn't daemonize slurmd: %m");
}
test_core_limit();
info("slurmd version %s started", SLURM_VERSION_STRING);
debug3("finished daemonize");
if ((oom_value = getenv("SLURMD_OOM_ADJ"))) {
i = atoi(oom_value);
debug("Setting slurmd oom_adj to %d", i);
set_oom_adj(i);
}
//.........这里部分代码省略.........
示例6: Daemonize
/*-------------------------------------------------------------------------*/
int Daemonize(const char* pidfile)
/* Turn the calling process into a daemon (detach from tty setuid(), etc */
/* */
/* Returns: zero on success non-zero if slpd could not daemonize (or if */
/* slpd is already running . */
/*-------------------------------------------------------------------------*/
{
pid_t pid;
FILE* fd;
struct passwd* pwent;
char pidstr[13];
if(G_SlpdCommandLine.detach)
{
/*-------------------------------------------*/
/* Release the controlling tty and std files */
/*-------------------------------------------*/
switch(fork())
{
case -1:
return -1;
case 0:
/* child lives */
break;
default:
/* parent dies */
exit(0);
}
close(0);
close(1);
close(2);
setsid(); /* will only fail if we are already the process group leader */
}
/*------------------------------------------*/
/* make sure that we're not running already */
/*------------------------------------------*/
/* read the pid from the file */
fd = fopen(pidfile,"r");
if(fd)
{
fread(pidstr,13,1,fd);
fclose(fd);
pid = atoi(pidstr);
if(pid)
{
if(kill(pid,0) == 0)
{
/* we are already running */
SLPFatal("slpd daemon is already running\n");
return -1;
}
}
}
/* write my pid to the pidfile */
fd = fopen(pidfile,"w");
if(fd)
{
sprintf(pidstr,"%i",getpid());
fwrite(pidstr,strlen(pidstr),1,fd);
fclose(fd);
}
/*----------------*/
/* suid to daemon */
/*----------------*/
/* TODO: why do the following lines mess up my signal handlers? */
pwent = getpwnam("daemon");
if(pwent)
{
if( setgroups(1, &pwent->pw_gid) < 0 ||
setgid(pwent->pw_gid) < 0 ||
setuid(pwent->pw_uid) < 0 )
{
/* TODO: should we log here and return fail */
}
}
return 0;
}
示例7: main
//.........这里部分代码省略.........
} else {
fprintf(PID, "%d\n", (int) getpid());
fclose(PID);
}
#ifndef _MSC_VER
/* The sequence open()/fdopen()/fclose()/close() makes MSVC crash,
hence skip the close() call when using the MSVC runtime. */
close(fd);
#endif
}
}
#endif
#if HAVE_UNISTD_H
persistent_dir = get_persistent_directory();
mkdirhier( persistent_dir, NETSNMP_AGENT_DIRECTORY_MODE, 0 );
uid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_USERID);
gid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_GROUPID);
#ifdef HAVE_CHOWN
if ( uid != 0 || gid != 0 )
chown( persistent_dir, uid, gid );
#endif
#ifdef HAVE_SETGID
if ((gid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_GROUPID)) != 0) {
DEBUGMSGTL(("snmpd/main", "Changing gid to %d.\n", gid));
if (setgid(gid) == -1
#ifdef HAVE_SETGROUPS
|| setgroups(1, (gid_t *)&gid) == -1
#endif
) {
snmp_log_perror("setgid failed");
if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
exit(1);
}
}
}
#endif
#ifdef HAVE_SETUID
if ((uid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_USERID)) != 0) {
#if HAVE_GETPWNAM && HAVE_PWD_H && HAVE_INITGROUPS
/*
* Set supplementary groups before changing UID
* (which probably involves giving up privileges)
*/
info = getpwuid(uid);
if (info) {
DEBUGMSGTL(("snmpd/main", "Supplementary groups for %s.\n", info->pw_name));
if (initgroups(info->pw_name, (gid != 0 ? (gid_t)gid : info->pw_gid)) == -1) {
snmp_log_perror("initgroups failed");
if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
exit(1);
}
}
}
#endif
DEBUGMSGTL(("snmpd/main", "Changing uid to %d.\n", uid));
if (setuid(uid) == -1) {
示例8: BecomeNonRoot
/*++
* Function: BecomeNonRoot
*
* Purpose: If we are running as root, attempt to change that.
*
* Parameters: nada
*
* Returns: 0 on success
* -1 on failure
*
* Authors: Dave McMurtrie <[email protected]>
*
* Notes: Relies on global copy of ProxyConfig_Struct "PC_Struct".
* Also worth mentioning that instead of just becoming non-root
* this function is now also responsible for chown()ing
* the global statistics file. I had to choose
* between doing a passwd and group lookup twice (and further
* cluttering main) or doing the chown here where it
* doesn't logically belong. I chose to put it here, but at
* least I documented it...
*
* In addition to becoming non-root, this function now also
* does a chroot() if so configured. Soon I'll rename this
* function to something more fitting...
*--
*/
extern int BecomeNonRoot( void )
{
char *fn = "BecomeNonRoot()";
struct passwd *pwent; /* ptr to a passwd file entry */
struct group *gp; /* ptr to a group file entry */
uid_t newuid; /* uid we want to run as */
gid_t newgid; /* gid we want to run as */
if ((pwent = getpwnam( PC_Struct.proc_username )) == NULL)
{
syslog(LOG_WARNING, "%s: getpwnam(%s) failed.",
fn, PC_Struct.proc_username);
return(-1);
}
else
{
newuid = pwent->pw_uid;
}
/*
* Since the whole purpose here is to not run as root, make sure that
* we don't get UID 0 back for username.
*/
if ( newuid == 0 )
{
syslog( LOG_ERR, "%s: getpwnam returned UID 0 for '%s'.",
fn, PC_Struct.proc_username );
return(-1);
}
if ((gp = getgrnam( PC_Struct.proc_groupname )) == NULL)
{
syslog(LOG_WARNING, "%s: getgrnam(%s) failed.",
fn, PC_Struct.proc_groupname);
return(-1);
}
else
{
newgid = gp->gr_gid;
}
/*
* The chown() call gets stuck here. I hate it, but
* once in a while there are going to be things in life that I hate.
*/
if ( chown( PC_Struct.stat_filename, newuid, newgid ) < 0 )
{
syslog( LOG_WARNING, "%s: chown() failed to set ownership of file '%s' to '%s:%s': %s", fn, PC_Struct.stat_filename, PC_Struct.proc_username, PC_Struct.proc_groupname, strerror( errno ) );
return( -1 );
}
/*
* Now the whole reason this function exists... setgid and setuid.
*
* Patch by Jarno Huuskonen -- also drop any supplementary groups.
*/
syslog( LOG_INFO, "%s: Process will run as uid %d (%s) and gid %d (%s).",
fn, newuid, PC_Struct.proc_username,
newgid, PC_Struct.proc_groupname );
if ( setgroups( 0, NULL ) < 0 )
{
syslog( LOG_WARNING, "%s: setgroups() failed: %s", fn, strerror( errno ) );
return( -1 );
}
if ((setgid(newgid)) < 0 )
{
syslog(LOG_WARNING, "%s: setgid(%d) failed: %s", fn,
newgid, strerror(errno));
return(-1);
}
//.........这里部分代码省略.........
示例9: main
//.........这里部分代码省略.........
if (opt_log_trace) {
log_level = LOG_TRACE;
log_trace = opt_log_trace;
}
if (opt_log_debug)
log_debug = opt_log_debug;
tvhlog_init(log_level, log_options, opt_logpath);
tvhlog_set_debug(log_debug);
tvhlog_set_trace(log_trace);
signal(SIGPIPE, handle_sigpipe); // will be redundant later
/* Daemonise */
if(opt_fork) {
const char *homedir;
gid_t gid;
uid_t uid;
struct group *grp = getgrnam(opt_group ?: "video");
struct passwd *pw = opt_user ? getpwnam(opt_user) : NULL;
FILE *pidfile = fopen(opt_pidpath, "w+");
if(grp != NULL) {
gid = grp->gr_gid;
} else {
gid = 1;
}
if (pw != NULL) {
if (getuid() != pw->pw_uid) {
gid_t glist[10];
int gnum;
gnum = get_user_groups(pw, glist, 10);
if (setgroups(gnum, glist)) {
tvhlog(LOG_ALERT, "START",
"setgroups() failed, do you have permission?");
return 1;
}
}
uid = pw->pw_uid;
homedir = pw->pw_dir;
setenv("HOME", homedir, 1);
} else {
uid = 1;
}
if ((getgid() != gid) && setgid(gid)) {
tvhlog(LOG_ALERT, "START",
"setgid() failed, do you have permission?");
return 1;
}
if ((getuid() != uid) && setuid(uid)) {
tvhlog(LOG_ALERT, "START",
"setuid() failed, do you have permission?");
return 1;
}
if(daemon(0, 0)) {
exit(2);
}
if(pidfile != NULL) {
fprintf(pidfile, "%d\n", getpid());
fclose(pidfile);
}
/* Make dumpable */
if (opt_dump) {
示例10: table_api_dispatch
int
table_api_dispatch(void)
{
#if 0
struct passwd *pw;
#endif
ssize_t n;
#if 0
pw = getpwnam(user);
if (pw == NULL) {
log_warn("table-api: getpwnam");
fatalx("table-api: exiting");
}
if (rootpath) {
if (chroot(rootpath) == -1) {
log_warn("table-api: chroot");
fatalx("table-api: exiting");
}
if (chdir("/") == -1) {
log_warn("table-api: chdir");
fatalx("table-api: exiting");
}
}
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) {
log_warn("table-api: cannot drop privileges");
fatalx("table-api: exiting");
}
#endif
imsg_init(&ibuf, 0);
while (1) {
n = imsg_get(&ibuf, &imsg);
if (n == -1) {
log_warn("warn: table-api: imsg_get");
break;
}
if (n) {
rdata = imsg.data;
rlen = imsg.hdr.len - IMSG_HEADER_SIZE;
table_msg_dispatch();
if (quit)
break;
imsg_flush(&ibuf);
continue;
}
n = imsg_read(&ibuf);
if (n == -1 && errno != EAGAIN) {
log_warn("warn: table-api: imsg_read");
break;
}
if (n == 0) {
log_warnx("warn: table-api: pipe closed");
break;
}
}
return (1);
}
示例11: pony
pid_t
pony(void)
{
pid_t pid;
struct passwd *pw;
struct event ev_sigint;
struct event ev_sigterm;
switch (pid = fork()) {
case -1:
fatal("pony: cannot fork");
case 0:
post_fork(PROC_PONY);
break;
default:
return (pid);
}
mda_postfork();
mta_postfork();
smtp_postfork();
filter_postfork();
/* do not purge listeners and pki, they are purged
* in smtp_configure()
*/
purge_config(PURGE_TABLES|PURGE_RULES);
if ((pw = getpwnam(SMTPD_USER)) == NULL)
fatalx("unknown user " SMTPD_USER);
if (chroot(PATH_CHROOT) == -1)
fatal("pony: chroot");
if (chdir("/") == -1)
fatal("pony: chdir(\"/\")");
config_process(PROC_PONY);
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("pony: cannot drop privileges");
imsg_callback = pony_imsg;
event_init();
mda_postprivdrop();
mta_postprivdrop();
smtp_postprivdrop();
signal_set(&ev_sigint, SIGINT, pony_sig_handler, NULL);
signal_set(&ev_sigterm, SIGTERM, pony_sig_handler, NULL);
signal_add(&ev_sigint, NULL);
signal_add(&ev_sigterm, NULL);
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
config_peer(PROC_PARENT);
config_peer(PROC_QUEUE);
config_peer(PROC_LKA);
config_peer(PROC_CONTROL);
config_peer(PROC_CA);
config_done();
ca_engine_init();
if (event_dispatch() < 0)
fatal("event_dispatch");
pony_shutdown();
return (0);
}
示例12: main
//.........这里部分代码省略.........
errx(EXIT_FAILURE,
_("duplicate --apparmor-profile option"));
opts.apparmor_profile = optarg;
break;
case 'h':
usage(stdout);
case 'V':
printf(UTIL_LINUX_VERSION);
return EXIT_SUCCESS;
case '?':
usage(stderr);
default:
errx(EXIT_FAILURE, _("unrecognized option '%c'"), c);
}
}
if (dumplevel) {
if (total_opts != dumplevel || optind < argc)
errx(EXIT_FAILURE,
_("--dump is incompatible with all other options"));
dump(dumplevel);
return EXIT_SUCCESS;
}
if (list_caps) {
if (total_opts != 1 || optind < argc)
errx(EXIT_FAILURE,
_("--list-caps must be specified alone"));
list_known_caps();
return EXIT_SUCCESS;
}
if (argc <= optind)
errx(EXIT_FAILURE, _("No program specified"));
if ((opts.have_rgid || opts.have_egid)
&& !opts.keep_groups && !opts.clear_groups && !opts.have_groups)
errx(EXIT_FAILURE,
_("--[re]gid requires --keep-groups, --clear-groups, or --groups"));
if (opts.nnp)
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
err(EXIT_FAILURE, _("disallow granting new privileges failed"));
if (opts.selinux_label)
do_selinux_label(opts.selinux_label);
if (opts.apparmor_profile)
do_apparmor_profile(opts.apparmor_profile);
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1)
err(EXIT_FAILURE, _("keep process capabilities failed"));
/* We're going to want CAP_SETPCAP, CAP_SETUID, and CAP_SETGID if
* possible. */
bump_cap(CAP_SETPCAP);
bump_cap(CAP_SETUID);
bump_cap(CAP_SETGID);
if (capng_apply(CAPNG_SELECT_CAPS) != 0)
err(SETPRIV_EXIT_PRIVERR, _("activate capabilities"));
if (opts.have_ruid || opts.have_euid) {
do_setresuid(&opts);
/* KEEPCAPS doesn't work for the effective mask. */
if (capng_apply(CAPNG_SELECT_CAPS) != 0)
err(SETPRIV_EXIT_PRIVERR, _("reactivate capabilities"));
}
if (opts.have_rgid || opts.have_egid)
do_setresgid(&opts);
if (opts.have_groups) {
if (setgroups(opts.num_groups, opts.groups) != 0)
err(SETPRIV_EXIT_PRIVERR, _("setgroups failed"));
} else if (opts.clear_groups) {
gid_t x = 0;
if (setgroups(0, &x) != 0)
err(SETPRIV_EXIT_PRIVERR, _("setgroups failed"));
}
if (opts.have_securebits)
if (prctl(PR_SET_SECUREBITS, opts.securebits, 0, 0, 0) != 0)
err(SETPRIV_EXIT_PRIVERR, _("set process securebits failed"));
if (opts.bounding_set) {
do_caps(CAPNG_BOUNDING_SET, opts.bounding_set);
errno = EPERM; /* capng doesn't set errno if we're missing CAP_SETPCAP */
if (capng_apply(CAPNG_SELECT_BOUNDS) != 0)
err(SETPRIV_EXIT_PRIVERR, _("apply bounding set"));
}
if (opts.caps_to_inherit) {
do_caps(CAPNG_INHERITABLE, opts.caps_to_inherit);
if (capng_apply(CAPNG_SELECT_CAPS) != 0)
err(SETPRIV_EXIT_PRIVERR, _("apply capabilities"));
}
execvp(argv[optind], argv + optind);
err(EXIT_FAILURE, _("cannot execute: %s"), argv[optind]);
}
示例13: f_create_process
//.........这里部分代码省略.........
||(tmp2=simple_mapping_string_lookup(tmp->u.mapping, "vmem")))
ADD_LIMIT( "map_mem", RLIMIT_VMEM, tmp2 );
#endif
#ifdef RLIMIT_AS
if((tmp2=simple_mapping_string_lookup(tmp->u.mapping, "as"))
||(tmp2=simple_mapping_string_lookup(tmp->u.mapping, "mem")))
ADD_LIMIT( "mem", RLIMIT_AS, tmp2 );
#endif
#undef ADD_LIMIT
}
}
if((tmp=simple_mapping_string_lookup(optional, "env"))) {
if(tmp->type == T_MAPPING) {
struct mapping *m=tmp->u.mapping;
struct array *i,*v;
int ptr=0;
i=mapping_indices(m);
v=mapping_values(m);
storage.env=(char **)xalloc((1+m_sizeof(m)) * sizeof(char *));
for(e=0;e<i->size;e++)
{
if(ITEM(i)[e].type == T_STRING &&
ITEM(v)[e].type == T_STRING)
{
check_stack(3);
ref_push_string(ITEM(i)[e].u.string);
push_string(make_shared_string("="));
ref_push_string(ITEM(v)[e].u.string);
f_add(3);
storage.env[ptr++]=Pike_sp[-1].u.string->str;
}
}
storage.env[ptr++]=0;
free_array(i);
free_array(v);
}
}
storage.argv = (char **)xalloc((1 + cmd->size) * sizeof(char *));
for (e = 0; e < cmd->size; e++) storage.argv[e] = ITEM(cmd)[e].u.string->str;
storage.argv[e] = 0;
th_atfork_prepare();
pid = fork();
if (pid) {
th_atfork_parent();
} else {
th_atfork_child();
}
if (pid == -1) {
Pike_error("Caudium.create_process() failed.");
} else if (pid) {
pop_n_elems(args);
push_int(pid);
return;
} else {
if(storage.limits) {
struct plimit *l = storage.limits;
while(l) {
int tmpres = setrlimit( l->resource, &l->rlp );
l = l->next;
}
}
if(storage.env) environ = storage.env;
chdir(tmp_cwd);
seteuid(0);
setegid(0);
setgroups(0, NULL);
if (gid_request) setgid(wanted_gid);
if (uid_request) setuid(wanted_uid);
dup2(fds[0], 0);
dup2(fds[1], 1);
dup2(fds[2], 2);
set_close_on_exec(0,0);
set_close_on_exec(1,0);
set_close_on_exec(2,0);
execvp(storage.argv[0],storage.argv);
exit(99);
}
pop_n_elems(args);
push_int(0);
}
示例14: uv__process_child_init
static void uv__process_child_init(const uv_process_options_t* options,
int stdio_count,
int (*pipes)[2],
int error_fd) {
int close_fd;
int use_fd;
int fd;
if (options->flags & UV_PROCESS_DETACHED)
setsid();
for (fd = 0; fd < stdio_count; fd++) {
close_fd = pipes[fd][0];
use_fd = pipes[fd][1];
if (use_fd < 0) {
if (fd >= 3)
continue;
else {
/* redirect stdin, stdout and stderr to /dev/null even if UV_IGNORE is
* set
*/
use_fd = open("/dev/null", fd == 0 ? O_RDONLY : O_RDWR);
close_fd = use_fd;
if (use_fd == -1) {
uv__write_int(error_fd, -errno);
_exit(127);
}
}
}
if (fd == use_fd)
uv__cloexec(use_fd, 0);
else
dup2(use_fd, fd);
if (fd <= 2)
uv__nonblock(fd, 0);
if (close_fd >= stdio_count)
uv__close(close_fd);
}
for (fd = 0; fd < stdio_count; fd++) {
use_fd = pipes[fd][1];
if (use_fd >= 0 && fd != use_fd)
close(use_fd);
}
if (options->cwd != NULL && chdir(options->cwd)) {
uv__write_int(error_fd, -errno);
_exit(127);
}
if (options->flags & (UV_PROCESS_SETUID | UV_PROCESS_SETGID)) {
/* When dropping privileges from root, the `setgroups` call will
* remove any extraneous groups. If we don't call this, then
* even though our uid has dropped, we may still have groups
* that enable us to do super-user things. This will fail if we
* aren't root, so don't bother checking the return value, this
* is just done as an optimistic privilege dropping function.
*/
SAVE_ERRNO(setgroups(0, NULL));
}
if ((options->flags & UV_PROCESS_SETGID) && setgid(options->gid)) {
uv__write_int(error_fd, -errno);
_exit(127);
}
if ((options->flags & UV_PROCESS_SETUID) && setuid(options->uid)) {
uv__write_int(error_fd, -errno);
_exit(127);
}
if (options->env != NULL) {
environ = options->env;
}
execvp(options->file, options->args);
uv__write_int(error_fd, -errno);
_exit(127);
}
示例15: attach_child_main
//.........这里部分代码省略.........
}
/* Set {u,g}id. */
new_uid = 0;
new_gid = 0;
/* Ignore errors, we will fall back to root in that case (/proc was not
* mounted etc.).
*/
if (options->namespaces & CLONE_NEWUSER)
lxc_attach_get_init_uidgid(&new_uid, &new_gid);
if (options->uid != (uid_t)-1)
new_uid = options->uid;
if (options->gid != (gid_t)-1)
new_gid = options->gid;
/* Setup the controlling tty. */
if (options->stdin_fd && isatty(options->stdin_fd)) {
if (setsid() < 0) {
SYSERROR("Unable to setsid.");
shutdown(ipc_socket, SHUT_RDWR);
rexit(-1);
}
if (ioctl(options->stdin_fd, TIOCSCTTY, (char *)NULL) < 0) {
SYSERROR("Unable to set TIOCSTTY.");
shutdown(ipc_socket, SHUT_RDWR);
rexit(-1);
}
}
/* Try to set the {u,g}id combination. */
if ((new_gid != 0 || options->namespaces & CLONE_NEWUSER)) {
if (setgid(new_gid) || setgroups(0, NULL)) {
SYSERROR("Switching to container gid.");
shutdown(ipc_socket, SHUT_RDWR);
rexit(-1);
}
}
if ((new_uid != 0 || options->namespaces & CLONE_NEWUSER) && setuid(new_uid)) {
SYSERROR("Switching to container uid.");
shutdown(ipc_socket, SHUT_RDWR);
rexit(-1);
}
/* Tell initial process it may now put us into cgroups. */
status = 1;
ret = lxc_write_nointr(ipc_socket, &status, sizeof(status));
if (ret != sizeof(status)) {
ERROR("Intended to send sequence number 1: %s.", strerror(errno));
shutdown(ipc_socket, SHUT_RDWR);
rexit(-1);
}
/* Wait for the initial thread to signal us that it has done everything
* for us when it comes to cgroups etc.
*/
expected = 2;
status = -1;
ret = lxc_read_nointr_expect(ipc_socket, &status, sizeof(status), &expected);
if (ret <= 0) {
ERROR("Expected to receive sequence number 2: %s", strerror(errno));
shutdown(ipc_socket, SHUT_RDWR);
rexit(-1);
}