X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=runit%2Fchpst.c;h=0f605cc455ffbae8af072ba838a17ad1275f22b8;hb=518d8dd97949be5303a405bedd26d0f325854c84;hp=b26d6286632f9e37acee9d4c311711c639a3d6ac;hpb=4e6ceb44554d265eed24c414b9a201d51d00fee2;p=oweals%2Fbusybox.git diff --git a/runit/chpst.c b/runit/chpst.c index b26d62866..0f605cc45 100644 --- a/runit/chpst.c +++ b/runit/chpst.c @@ -1,36 +1,87 @@ -#include "busybox.h" +/* +Copyright (c) 2001-2006, Gerrit Pape +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* Busyboxed by Denis Vlasenko */ +/* Dependencies on runit_lib.c removed */ + +#include "libbb.h" #include -static unsigned option_mask; // Must match constants in chpst_main! -#define OPT_verbose (option_mask & 0x2000) -#define OPT_pgrp (option_mask & 0x4000) -#define OPT_nostdin (option_mask & 0x8000) -#define OPT_nostdout (option_mask & 0x10000) -#define OPT_nostderr (option_mask & 0x20000) +#define OPT_verbose (option_mask32 & 0x2000) +#define OPT_pgrp (option_mask32 & 0x4000) +#define OPT_nostdin (option_mask32 & 0x8000) +#define OPT_nostdout (option_mask32 & 0x10000) +#define OPT_nostderr (option_mask32 & 0x20000) -static char *set_user; -static char *env_user; -static const char *env_dir; -static long limitd = -2; -static long limits = -2; -static long limitl = -2; -static long limita = -2; -static long limito = -2; -static long limitp = -2; -static long limitf = -2; -static long limitc = -2; -static long limitr = -2; -static long limitt = -2; -static long nicelvl; -static const char *root; +struct globals { + char *set_user; + char *env_user; + const char *env_dir; + const char *root; + long limitd; /* limitX are initialized to -2 */ + long limits; + long limitl; + long limita; + long limito; + long limitp; + long limitf; + long limitc; + long limitr; + long limitt; + int nicelvl; +}; +#define G (*(struct globals*)&bb_common_bufsiz1) +#define set_user (G.set_user) +#define env_user (G.env_user) +#define env_dir (G.env_dir ) +#define root (G.root ) +#define limitd (G.limitd ) +#define limits (G.limits ) +#define limitl (G.limitl ) +#define limita (G.limita ) +#define limito (G.limito ) +#define limitp (G.limitp ) +#define limitf (G.limitf ) +#define limitc (G.limitc ) +#define limitr (G.limitr ) +#define limitt (G.limitt ) +#define nicelvl (G.nicelvl ) +#define INIT_G() do { \ + long *p = &limitd; \ + do *p++ = -2; while (p <= &limitt); \ +} while (0) static void suidgid(char *user) { struct bb_uidgid_t ugid; - if (!uidgid_get(&ugid, user)) { + if (!get_uidgid(&ugid, user, 1)) { bb_error_msg_and_die("unknown user/group: %s", user); } if (setgroups(1, &ugid.gid) == -1) @@ -43,7 +94,7 @@ static void euidgid(char *user) { struct bb_uidgid_t ugid; - if (!uidgid_get(&ugid, user)) { + if (!get_uidgid(&ugid, user, 1)) { bb_error_msg_and_die("unknown user/group: %s", user); } xsetenv("GID", utoa(ugid.gid)); @@ -66,20 +117,23 @@ static void edir(const char *directory_name) errno = 0; d = readdir(dir); if (!d) { - if (errno) bb_perror_msg_and_die("readdir %s", directory_name); + if (errno) + bb_perror_msg_and_die("readdir %s", + directory_name); break; } - if (d->d_name[0] == '.') continue; + if (d->d_name[0] == '.') + continue; fd = open(d->d_name, O_RDONLY | O_NDELAY); if (fd < 0) { if ((errno == EISDIR) && env_dir) { if (OPT_verbose) - bb_perror_msg("warning: %s/%s is a directory", directory_name, - d->d_name); + bb_perror_msg("warning: %s/%s is a directory", + directory_name, d->d_name); continue; } else - bb_perror_msg_and_die("open %s/%s", directory_name, /* was exiting 111 */ - d->d_name); + bb_perror_msg_and_die("open %s/%s", + directory_name, d->d_name); } if (fd >= 0) { char buf[256]; @@ -88,8 +142,8 @@ static void edir(const char *directory_name) size = safe_read(fd, buf, sizeof(buf)-1); if (size < 0) - bb_perror_msg_and_die("read %s/%s", directory_name, /* was exiting 111 */ - d->d_name); + bb_perror_msg_and_die("read %s/%s", + directory_name, d->d_name); if (size == 0) { unsetenv(d->d_name); continue; @@ -98,9 +152,9 @@ static void edir(const char *directory_name) tail = memchr(buf, '\n', sizeof(buf)); /* skip trailing whitespace */; while (1) { - if (tail[0]==' ') tail[0] = '\0'; - if (tail[0]=='\t') tail[0] = '\0'; - if (tail[0]=='\n') tail[0] = '\0'; + if (tail[0] == ' ') tail[0] = '\0'; + if (tail[0] == '\t') tail[0] = '\0'; + if (tail[0] == '\n') tail[0] = '\0'; if (tail == buf) break; tail--; } @@ -108,7 +162,8 @@ static void edir(const char *directory_name) } } closedir(dir); - if (fchdir(wdir) == -1) bb_perror_msg_and_die("fchdir"); + if (fchdir(wdir) == -1) + bb_perror_msg_and_die("fchdir"); close(wdir); } @@ -116,12 +171,14 @@ static void limit(int what, long l) { struct rlimit r; - if (getrlimit(what, &r) == -1) bb_perror_msg_and_die("getrlimit"); + if (getrlimit(what, &r) == -1) + bb_perror_msg_and_die("getrlimit"); if ((l < 0) || (l > r.rlim_max)) r.rlim_cur = r.rlim_max; else r.rlim_cur = l; - if (setrlimit(what, &r) == -1) bb_perror_msg_and_die("setrlimit"); + if (setrlimit(what, &r) == -1) + bb_perror_msg_and_die("setrlimit"); } static void slimit(void) @@ -130,21 +187,27 @@ static void slimit(void) #ifdef RLIMIT_DATA limit(RLIMIT_DATA, limitd); #else - if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_DATA"); + if (OPT_verbose) + bb_error_msg("system does not support RLIMIT_%s", + "DATA"); #endif } if (limits >= -1) { #ifdef RLIMIT_STACK limit(RLIMIT_STACK, limits); #else - if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_STACK"); + if (OPT_verbose) + bb_error_msg("system does not support RLIMIT_%s", + "STACK"); #endif } if (limitl >= -1) { #ifdef RLIMIT_MEMLOCK limit(RLIMIT_MEMLOCK, limitl); #else - if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_MEMLOCK"); + if (OPT_verbose) + bb_error_msg("system does not support RLIMIT_%s", + "MEMLOCK"); #endif } if (limita >= -1) { @@ -155,7 +218,8 @@ static void slimit(void) limit(RLIMIT_AS, limita); #else if (OPT_verbose) - bb_error_msg("system does not support %s", "RLIMIT_VMEM"); + bb_error_msg("system does not support RLIMIT_%s", + "VMEM"); #endif #endif } @@ -167,7 +231,8 @@ static void slimit(void) limit(RLIMIT_OFILE, limito); #else if (OPT_verbose) - bb_error_msg("system does not support %s", "RLIMIT_NOFILE"); + bb_error_msg("system does not support RLIMIT_%s", + "NOFILE"); #endif #endif } @@ -175,81 +240,94 @@ static void slimit(void) #ifdef RLIMIT_NPROC limit(RLIMIT_NPROC, limitp); #else - if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_NPROC"); + if (OPT_verbose) + bb_error_msg("system does not support RLIMIT_%s", + "NPROC"); #endif } if (limitf >= -1) { #ifdef RLIMIT_FSIZE limit(RLIMIT_FSIZE, limitf); #else - if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_FSIZE"); + if (OPT_verbose) + bb_error_msg("system does not support RLIMIT_%s", + "FSIZE"); #endif } if (limitc >= -1) { #ifdef RLIMIT_CORE limit(RLIMIT_CORE, limitc); #else - if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_CORE"); + if (OPT_verbose) + bb_error_msg("system does not support RLIMIT_%s", + "CORE"); #endif } if (limitr >= -1) { #ifdef RLIMIT_RSS limit(RLIMIT_RSS, limitr); #else - if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_RSS"); + if (OPT_verbose) + bb_error_msg("system does not support RLIMIT_%s", + "RSS"); #endif } if (limitt >= -1) { #ifdef RLIMIT_CPU limit(RLIMIT_CPU, limitt); #else - if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_CPU"); + if (OPT_verbose) + bb_error_msg("system does not support RLIMIT_%s", + "CPU"); #endif } } /* argv[0] */ -static void setuidgid(int, char **); -static void envuidgid(int, char **); -static void envdir(int, char **); -static void softlimit(int, char **); +static void setuidgid(int, char **) ATTRIBUTE_NORETURN; +static void envuidgid(int, char **) ATTRIBUTE_NORETURN; +static void envdir(int, char **) ATTRIBUTE_NORETURN; +static void softlimit(int, char **) ATTRIBUTE_NORETURN; +int chpst_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int chpst_main(int argc, char **argv) { + INIT_G(); + if (applet_name[3] == 'd') envdir(argc, argv); if (applet_name[1] == 'o') softlimit(argc, argv); if (applet_name[0] == 's') setuidgid(argc, argv); if (applet_name[0] == 'e') envuidgid(argc, argv); - // otherwise we are.......... chpst + // otherwise we are chpst { char *m,*d,*o,*p,*f,*c,*r,*t,*n; - option_mask = getopt32(argc, argv, "u:U:e:m:d:o:p:f:c:r:t:/:n:vP012", + getopt32(argv, "+u:U:e:m:d:o:p:f:c:r:t:/:n:vP012", &set_user,&env_user,&env_dir, &m,&d,&o,&p,&f,&c,&r,&t,&root,&n); - // if (option_mask & 0x1) // -u - // if (option_mask & 0x2) // -U - // if (option_mask & 0x4) // -e - if (option_mask & 0x8) limits = limitl = limita = limitd = bb_xgetularg10(m); // -m - if (option_mask & 0x10) limitd = bb_xgetularg10(d); // -d - if (option_mask & 0x20) limito = bb_xgetularg10(o); // -o - if (option_mask & 0x40) limitp = bb_xgetularg10(p); // -p - if (option_mask & 0x80) limitf = bb_xgetularg10(f); // -f - if (option_mask & 0x100) limitc = bb_xgetularg10(c); // -c - if (option_mask & 0x200) limitr = bb_xgetularg10(r); // -r - if (option_mask & 0x400) limitt = bb_xgetularg10(t); // -t - // if (option_mask & 0x800) // -/ - if (option_mask & 0x1000) nicelvl = bb_xgetlarg_bnd_sfx(n, 10, -20, 20, NULL); // -n + // if (option_mask32 & 0x1) // -u + // if (option_mask32 & 0x2) // -U + // if (option_mask32 & 0x4) // -e + if (option_mask32 & 0x8) limits = limitl = limita = limitd = xatoul(m); // -m + if (option_mask32 & 0x10) limitd = xatoul(d); // -d + if (option_mask32 & 0x20) limito = xatoul(o); // -o + if (option_mask32 & 0x40) limitp = xatoul(p); // -p + if (option_mask32 & 0x80) limitf = xatoul(f); // -f + if (option_mask32 & 0x100) limitc = xatoul(c); // -c + if (option_mask32 & 0x200) limitr = xatoul(r); // -r + if (option_mask32 & 0x400) limitt = xatoul(t); // -t + // if (option_mask32 & 0x800) // -/ + if (option_mask32 & 0x1000) nicelvl = xatoi(n); // -n // The below consts should match #defines at top! - //if (option_mask & 0x2000) OPT_verbose = 1; // -v - //if (option_mask & 0x4000) OPT_pgrp = 1; // -P - //if (option_mask & 0x8000) OPT_nostdin = 1; // -0 - //if (option_mask & 0x10000) OPT_nostdout = 1; // -1 - //if (option_mask & 0x20000) OPT_nostderr = 1; // -2 + //if (option_mask32 & 0x2000) OPT_verbose = 1; // -v + //if (option_mask32 & 0x4000) OPT_pgrp = 1; // -P + //if (option_mask32 & 0x8000) OPT_nostdin = 1; // -0 + //if (option_mask32 & 0x10000) OPT_nostdout = 1; // -1 + //if (option_mask32 & 0x20000) OPT_nostderr = 1; // -2 } argv += optind; if (!argv || !*argv) bb_show_usage(); - + if (OPT_pgrp) setsid(); if (env_dir) edir(env_dir); if (root) { @@ -268,7 +346,7 @@ int chpst_main(int argc, char **argv) if (OPT_nostdin) close(0); if (OPT_nostdout) close(1); if (OPT_nostderr) close(2); - execvp(argv[0], argv); + BB_EXECVP(argv[0], argv); bb_perror_msg_and_die("exec %s", argv[0]); } @@ -280,7 +358,7 @@ static void setuidgid(int argc, char **argv) if (!account) bb_show_usage(); if (!*++argv) bb_show_usage(); suidgid((char*)account); - execvp(argv[0], argv); + BB_EXECVP(argv[0], argv); bb_perror_msg_and_die("exec %s", argv[0]); } @@ -292,7 +370,7 @@ static void envuidgid(int argc, char **argv) if (!account) bb_show_usage(); if (!*++argv) bb_show_usage(); euidgid((char*)account); - execvp(argv[0], argv); + BB_EXECVP(argv[0], argv); bb_perror_msg_and_die("exec %s", argv[0]); } @@ -304,29 +382,29 @@ static void envdir(int argc, char **argv) if (!dir) bb_show_usage(); if (!*++argv) bb_show_usage(); edir(dir); - execvp(argv[0], argv); + BB_EXECVP(argv[0], argv); bb_perror_msg_and_die("exec %s", argv[0]); } static void softlimit(int argc, char **argv) { char *a,*c,*d,*f,*l,*m,*o,*p,*r,*s,*t; - option_mask = getopt32(argc, argv, "a:c:d:f:l:m:o:p:r:s:t:", + getopt32(argv, "+a:c:d:f:l:m:o:p:r:s:t:", &a,&c,&d,&f,&l,&m,&o,&p,&r,&s,&t); - if (option_mask & 0x001) limita = bb_xgetularg10(a); // -a - if (option_mask & 0x002) limitc = bb_xgetularg10(c); // -c - if (option_mask & 0x004) limitd = bb_xgetularg10(d); // -d - if (option_mask & 0x008) limitf = bb_xgetularg10(f); // -f - if (option_mask & 0x010) limitl = bb_xgetularg10(l); // -l - if (option_mask & 0x020) limits = limitl = limita = limitd = bb_xgetularg10(m); // -m - if (option_mask & 0x040) limito = bb_xgetularg10(o); // -o - if (option_mask & 0x080) limitp = bb_xgetularg10(p); // -p - if (option_mask & 0x100) limitr = bb_xgetularg10(r); // -r - if (option_mask & 0x200) limits = bb_xgetularg10(s); // -s - if (option_mask & 0x400) limitt = bb_xgetularg10(t); // -t + if (option_mask32 & 0x001) limita = xatoul(a); // -a + if (option_mask32 & 0x002) limitc = xatoul(c); // -c + if (option_mask32 & 0x004) limitd = xatoul(d); // -d + if (option_mask32 & 0x008) limitf = xatoul(f); // -f + if (option_mask32 & 0x010) limitl = xatoul(l); // -l + if (option_mask32 & 0x020) limits = limitl = limita = limitd = xatoul(m); // -m + if (option_mask32 & 0x040) limito = xatoul(o); // -o + if (option_mask32 & 0x080) limitp = xatoul(p); // -p + if (option_mask32 & 0x100) limitr = xatoul(r); // -r + if (option_mask32 & 0x200) limits = xatoul(s); // -s + if (option_mask32 & 0x400) limitt = xatoul(t); // -t argv += optind; if (!argv[0]) bb_show_usage(); slimit(); - execvp(argv[0], argv); + BB_EXECVP(argv[0], argv); bb_perror_msg_and_die("exec %s", argv[0]); }