Skip to content

Commit 2b8000f

Browse files
ahesfordericonr
authored andcommitted
chpst: add -C to change working directory
1 parent 82d4680 commit 2b8000f

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

man/chpst.8

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ Change the root directory to
146146
before starting
147147
.IR prog .
148148
.TP
149+
.B \-C \fIpwd
150+
chdir.
151+
Change the working directory to
152+
.I pwd
153+
before starting
154+
.IR prog .
155+
When combined with \-/, the working directory is changed after the chroot.
156+
.TP
149157
.B \-n \fIinc
150158
nice.
151159
Add

src/chpst.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "openreadclose.h"
2121
#include "direntry.h"
2222

23-
#define USAGE_MAIN " [-vP012] [-u user[:group]] [-U user[:group]] [-b argv0] [-e dir] [-/ root] [-n nice] [-l|-L lock] [-m n] [-d n] [-o n] [-p n] [-f n] [-c n] prog"
23+
#define USAGE_MAIN " [-vP012] [-u user[:group]] [-U user[:group]] [-b argv0] [-e dir] [-/ root] [-C pwd] [-n nice] [-l|-L lock] [-m n] [-d n] [-o n] [-p n] [-f n] [-c n] prog"
2424
#define FATAL "chpst: fatal: "
2525
#define WARNING "chpst: warning: "
2626

@@ -60,6 +60,7 @@ long limitt =-2;
6060
long nicelvl =0;
6161
const char *lock =0;
6262
const char *root =0;
63+
const char *pwd =0;
6364
unsigned int lockdelay;
6465

6566
void suidgid(char *user, unsigned int ext) {
@@ -286,7 +287,7 @@ int main(int argc, const char **argv) {
286287
if (str_equal(progname, "setlock")) setlock(argc, argv);
287288
if (str_equal(progname, "softlimit")) softlimit(argc, argv);
288289

289-
while ((opt =getopt(argc, argv, "u:U:b:e:m:d:o:p:f:c:r:t:/:n:l:L:vP012V"))
290+
while ((opt =getopt(argc, argv, "u:U:b:e:m:d:o:p:f:c:r:t:/:C:n:l:L:vP012V"))
290291
!= opteof)
291292
switch(opt) {
292293
case 'u': set_user =(char*)optarg; break;
@@ -305,6 +306,7 @@ int main(int argc, const char **argv) {
305306
case 'r': if (optarg[scan_ulong(optarg, &ul)]) usage(); limitr =ul; break;
306307
case 't': if (optarg[scan_ulong(optarg, &ul)]) usage(); limitt =ul; break;
307308
case '/': root =optarg; break;
309+
case 'C': pwd =optarg; break;
308310
case 'n':
309311
switch (*optarg) {
310312
case '-':
@@ -337,6 +339,9 @@ int main(int argc, const char **argv) {
337339
if (chdir(root) == -1) fatal2("unable to change directory", root);
338340
if (chroot(".") == -1) fatal("unable to change root directory");
339341
}
342+
if (pwd) {
343+
if (chdir(pwd) == -1) fatal2("unable to change directory", pwd);
344+
}
340345
if (nicelvl) {
341346
errno =0;
342347
if (nice(nicelvl) == -1) if (errno) fatal("unable to set nice level");

0 commit comments

Comments
 (0)