2
0

proc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_arch_threadproc.h"
  17. #include "fspr_strings.h"
  18. #include "fspr_portable.h"
  19. #include "fspr_signal.h"
  20. #include "fspr_random.h"
  21. APR_DECLARE(fspr_status_t) fspr_procattr_create(fspr_procattr_t **new,
  22. fspr_pool_t *pool)
  23. {
  24. (*new) = (fspr_procattr_t *)fspr_pcalloc(pool, sizeof(fspr_procattr_t));
  25. if ((*new) == NULL) {
  26. return APR_ENOMEM;
  27. }
  28. (*new)->pool = pool;
  29. (*new)->cmdtype = APR_PROGRAM;
  30. (*new)->uid = (*new)->gid = -1;
  31. return APR_SUCCESS;
  32. }
  33. APR_DECLARE(fspr_status_t) fspr_procattr_io_set(fspr_procattr_t *attr,
  34. fspr_int32_t in,
  35. fspr_int32_t out,
  36. fspr_int32_t err)
  37. {
  38. fspr_status_t status;
  39. if (in != 0) {
  40. if ((status = fspr_file_pipe_create(&attr->child_in, &attr->parent_in,
  41. attr->pool)) != APR_SUCCESS) {
  42. return status;
  43. }
  44. switch (in) {
  45. case APR_FULL_BLOCK:
  46. break;
  47. case APR_PARENT_BLOCK:
  48. fspr_file_pipe_timeout_set(attr->child_in, 0);
  49. break;
  50. case APR_CHILD_BLOCK:
  51. fspr_file_pipe_timeout_set(attr->parent_in, 0);
  52. break;
  53. default:
  54. fspr_file_pipe_timeout_set(attr->child_in, 0);
  55. fspr_file_pipe_timeout_set(attr->parent_in, 0);
  56. }
  57. }
  58. if (out) {
  59. if ((status = fspr_file_pipe_create(&attr->parent_out, &attr->child_out,
  60. attr->pool)) != APR_SUCCESS) {
  61. return status;
  62. }
  63. switch (out) {
  64. case APR_FULL_BLOCK:
  65. break;
  66. case APR_PARENT_BLOCK:
  67. fspr_file_pipe_timeout_set(attr->child_out, 0);
  68. break;
  69. case APR_CHILD_BLOCK:
  70. fspr_file_pipe_timeout_set(attr->parent_out, 0);
  71. break;
  72. default:
  73. fspr_file_pipe_timeout_set(attr->child_out, 0);
  74. fspr_file_pipe_timeout_set(attr->parent_out, 0);
  75. }
  76. }
  77. if (err) {
  78. if ((status = fspr_file_pipe_create(&attr->parent_err, &attr->child_err,
  79. attr->pool)) != APR_SUCCESS) {
  80. return status;
  81. }
  82. switch (err) {
  83. case APR_FULL_BLOCK:
  84. break;
  85. case APR_PARENT_BLOCK:
  86. fspr_file_pipe_timeout_set(attr->child_err, 0);
  87. break;
  88. case APR_CHILD_BLOCK:
  89. fspr_file_pipe_timeout_set(attr->parent_err, 0);
  90. break;
  91. default:
  92. fspr_file_pipe_timeout_set(attr->child_err, 0);
  93. fspr_file_pipe_timeout_set(attr->parent_err, 0);
  94. }
  95. }
  96. return APR_SUCCESS;
  97. }
  98. APR_DECLARE(fspr_status_t) fspr_procattr_child_in_set(fspr_procattr_t *attr,
  99. fspr_file_t *child_in,
  100. fspr_file_t *parent_in)
  101. {
  102. fspr_status_t rv = APR_SUCCESS;
  103. if (attr->child_in == NULL && attr->parent_in == NULL)
  104. rv = fspr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
  105. if (child_in != NULL && rv == APR_SUCCESS)
  106. rv = fspr_file_dup2(attr->child_in, child_in, attr->pool);
  107. if (parent_in != NULL && rv == APR_SUCCESS)
  108. rv = fspr_file_dup2(attr->parent_in, parent_in, attr->pool);
  109. return rv;
  110. }
  111. APR_DECLARE(fspr_status_t) fspr_procattr_child_out_set(fspr_procattr_t *attr,
  112. fspr_file_t *child_out,
  113. fspr_file_t *parent_out)
  114. {
  115. fspr_status_t rv = APR_SUCCESS;
  116. if (attr->child_out == NULL && attr->parent_out == NULL)
  117. rv = fspr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
  118. if (child_out != NULL && rv == APR_SUCCESS)
  119. rv = fspr_file_dup2(attr->child_out, child_out, attr->pool);
  120. if (parent_out != NULL && rv == APR_SUCCESS)
  121. rv = fspr_file_dup2(attr->parent_out, parent_out, attr->pool);
  122. return rv;
  123. }
  124. APR_DECLARE(fspr_status_t) fspr_procattr_child_err_set(fspr_procattr_t *attr,
  125. fspr_file_t *child_err,
  126. fspr_file_t *parent_err)
  127. {
  128. fspr_status_t rv = APR_SUCCESS;
  129. if (attr->child_err == NULL && attr->parent_err == NULL)
  130. rv = fspr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
  131. if (child_err != NULL && rv == APR_SUCCESS)
  132. rv = fspr_file_dup2(attr->child_err, child_err, attr->pool);
  133. if (parent_err != NULL && rv == APR_SUCCESS)
  134. rv = fspr_file_dup2(attr->parent_err, parent_err, attr->pool);
  135. return rv;
  136. }
  137. APR_DECLARE(fspr_status_t) fspr_procattr_dir_set(fspr_procattr_t *attr,
  138. const char *dir)
  139. {
  140. attr->currdir = fspr_pstrdup(attr->pool, dir);
  141. if (attr->currdir) {
  142. return APR_SUCCESS;
  143. }
  144. return APR_ENOMEM;
  145. }
  146. APR_DECLARE(fspr_status_t) fspr_procattr_cmdtype_set(fspr_procattr_t *attr,
  147. fspr_cmdtype_e cmd)
  148. {
  149. attr->cmdtype = cmd;
  150. return APR_SUCCESS;
  151. }
  152. APR_DECLARE(fspr_status_t) fspr_procattr_detach_set(fspr_procattr_t *attr,
  153. fspr_int32_t detach)
  154. {
  155. attr->detached = detach;
  156. return APR_SUCCESS;
  157. }
  158. APR_DECLARE(fspr_status_t) fspr_proc_fork(fspr_proc_t *proc, fspr_pool_t *pool)
  159. {
  160. int pid;
  161. if ((pid = fork()) < 0) {
  162. return errno;
  163. }
  164. else if (pid == 0) {
  165. proc->pid = pid;
  166. proc->in = NULL;
  167. proc->out = NULL;
  168. proc->err = NULL;
  169. fspr_random_after_fork(proc);
  170. return APR_INCHILD;
  171. }
  172. proc->pid = pid;
  173. proc->in = NULL;
  174. proc->out = NULL;
  175. proc->err = NULL;
  176. return APR_INPARENT;
  177. }
  178. static fspr_status_t limit_proc(fspr_procattr_t *attr)
  179. {
  180. #if APR_HAVE_STRUCT_RLIMIT && APR_HAVE_SETRLIMIT
  181. #ifdef RLIMIT_CPU
  182. if (attr->limit_cpu != NULL) {
  183. if ((setrlimit(RLIMIT_CPU, attr->limit_cpu)) != 0) {
  184. return errno;
  185. }
  186. }
  187. #endif
  188. #ifdef RLIMIT_NPROC
  189. if (attr->limit_nproc != NULL) {
  190. if ((setrlimit(RLIMIT_NPROC, attr->limit_nproc)) != 0) {
  191. return errno;
  192. }
  193. }
  194. #endif
  195. #ifdef RLIMIT_NOFILE
  196. if (attr->limit_nofile != NULL) {
  197. if ((setrlimit(RLIMIT_NOFILE, attr->limit_nofile)) != 0) {
  198. return errno;
  199. }
  200. }
  201. #endif
  202. #if defined(RLIMIT_AS)
  203. if (attr->limit_mem != NULL) {
  204. if ((setrlimit(RLIMIT_AS, attr->limit_mem)) != 0) {
  205. return errno;
  206. }
  207. }
  208. #elif defined(RLIMIT_DATA)
  209. if (attr->limit_mem != NULL) {
  210. if ((setrlimit(RLIMIT_DATA, attr->limit_mem)) != 0) {
  211. return errno;
  212. }
  213. }
  214. #elif defined(RLIMIT_VMEM)
  215. if (attr->limit_mem != NULL) {
  216. if ((setrlimit(RLIMIT_VMEM, attr->limit_mem)) != 0) {
  217. return errno;
  218. }
  219. }
  220. #endif
  221. #else
  222. /*
  223. * Maybe make a note in error_log that setrlimit isn't supported??
  224. */
  225. #endif
  226. return APR_SUCCESS;
  227. }
  228. APR_DECLARE(fspr_status_t) fspr_procattr_child_errfn_set(fspr_procattr_t *attr,
  229. fspr_child_errfn_t *errfn)
  230. {
  231. attr->errfn = errfn;
  232. return APR_SUCCESS;
  233. }
  234. APR_DECLARE(fspr_status_t) fspr_procattr_error_check_set(fspr_procattr_t *attr,
  235. fspr_int32_t chk)
  236. {
  237. attr->errchk = chk;
  238. return APR_SUCCESS;
  239. }
  240. APR_DECLARE(fspr_status_t) fspr_procattr_addrspace_set(fspr_procattr_t *attr,
  241. fspr_int32_t addrspace)
  242. {
  243. /* won't ever be used on this platform, so don't save the flag */
  244. return APR_SUCCESS;
  245. }
  246. APR_DECLARE(fspr_status_t) fspr_procattr_user_set(fspr_procattr_t *attr,
  247. const char *username,
  248. const char *password)
  249. {
  250. fspr_status_t rv;
  251. fspr_gid_t gid;
  252. if ((rv = fspr_uid_get(&attr->uid, &gid, username,
  253. attr->pool)) != APR_SUCCESS) {
  254. attr->uid = -1;
  255. return rv;
  256. }
  257. /* Use default user group if not already set */
  258. if (attr->gid == -1) {
  259. attr->gid = gid;
  260. }
  261. return APR_SUCCESS;
  262. }
  263. APR_DECLARE(fspr_status_t) fspr_procattr_group_set(fspr_procattr_t *attr,
  264. const char *groupname)
  265. {
  266. fspr_status_t rv;
  267. if ((rv = fspr_gid_get(&attr->gid, groupname, attr->pool)) != APR_SUCCESS)
  268. attr->gid = -1;
  269. return rv;
  270. }
  271. APR_DECLARE(fspr_status_t) fspr_proc_create(fspr_proc_t *new,
  272. const char *progname,
  273. const char * const *args,
  274. const char * const *env,
  275. fspr_procattr_t *attr,
  276. fspr_pool_t *pool)
  277. {
  278. int i;
  279. const char * const empty_envp[] = {NULL};
  280. if (!env) { /* Specs require an empty array instead of NULL;
  281. * Purify will trigger a failure, even if many
  282. * implementations don't.
  283. */
  284. env = empty_envp;
  285. }
  286. new->in = attr->parent_in;
  287. new->err = attr->parent_err;
  288. new->out = attr->parent_out;
  289. if (attr->errchk) {
  290. if (attr->currdir) {
  291. if (access(attr->currdir, X_OK) == -1) {
  292. /* chdir() in child wouldn't have worked */
  293. return errno;
  294. }
  295. }
  296. if (attr->cmdtype == APR_PROGRAM ||
  297. attr->cmdtype == APR_PROGRAM_ENV ||
  298. *progname == '/') {
  299. /* for both of these values of cmdtype, caller must pass
  300. * full path, so it is easy to check;
  301. * caller can choose to pass full path for other
  302. * values of cmdtype
  303. */
  304. if (access(progname, R_OK|X_OK) == -1) {
  305. /* exec*() in child wouldn't have worked */
  306. return errno;
  307. }
  308. }
  309. else {
  310. /* todo: search PATH for progname then try to access it */
  311. }
  312. }
  313. if ((new->pid = fork()) < 0) {
  314. return errno;
  315. }
  316. else if (new->pid == 0) {
  317. /* child process */
  318. /*
  319. * If we do exec cleanup before the dup2() calls to set up pipes
  320. * on 0-2, we accidentally close the pipes used by programs like
  321. * mod_cgid.
  322. *
  323. * If we do exec cleanup after the dup2() calls, cleanup can accidentally
  324. * close our pipes which replaced any files which previously had
  325. * descriptors 0-2.
  326. *
  327. * The solution is to kill the cleanup for the pipes, then do
  328. * exec cleanup, then do the dup2() calls.
  329. */
  330. if (attr->child_in) {
  331. fspr_pool_cleanup_kill(fspr_file_pool_get(attr->child_in),
  332. attr->child_in, fspr_unix_file_cleanup);
  333. }
  334. if (attr->child_out) {
  335. fspr_pool_cleanup_kill(fspr_file_pool_get(attr->child_out),
  336. attr->child_out, fspr_unix_file_cleanup);
  337. }
  338. if (attr->child_err) {
  339. fspr_pool_cleanup_kill(fspr_file_pool_get(attr->child_err),
  340. attr->child_err, fspr_unix_file_cleanup);
  341. }
  342. fspr_pool_cleanup_for_exec();
  343. if (attr->child_in) {
  344. fspr_file_close(attr->parent_in);
  345. dup2(attr->child_in->filedes, STDIN_FILENO);
  346. fspr_file_close(attr->child_in);
  347. }
  348. if (attr->child_out) {
  349. fspr_file_close(attr->parent_out);
  350. dup2(attr->child_out->filedes, STDOUT_FILENO);
  351. fspr_file_close(attr->child_out);
  352. }
  353. if (attr->child_err) {
  354. fspr_file_close(attr->parent_err);
  355. dup2(attr->child_err->filedes, STDERR_FILENO);
  356. fspr_file_close(attr->child_err);
  357. }
  358. fspr_signal(SIGCHLD, SIG_DFL); /* not sure if this is needed or not */
  359. if (attr->currdir != NULL) {
  360. if (chdir(attr->currdir) == -1) {
  361. if (attr->errfn) {
  362. attr->errfn(pool, errno, "change of working directory failed");
  363. }
  364. exit(-1); /* We have big problems, the child should exit. */
  365. }
  366. }
  367. /* Only try to switch if we are running as root */
  368. if (attr->gid != -1 && !geteuid()) {
  369. if (setgid(attr->gid)) {
  370. if (attr->errfn) {
  371. attr->errfn(pool, errno, "setting of group failed");
  372. }
  373. exit(-1); /* We have big problems, the child should exit. */
  374. }
  375. }
  376. if (attr->uid != -1 && !geteuid()) {
  377. if (setuid(attr->uid)) {
  378. if (attr->errfn) {
  379. attr->errfn(pool, errno, "setting of user failed");
  380. }
  381. exit(-1); /* We have big problems, the child should exit. */
  382. }
  383. }
  384. if (limit_proc(attr) != APR_SUCCESS) {
  385. if (attr->errfn) {
  386. attr->errfn(pool, errno, "setting of resource limits failed");
  387. }
  388. exit(-1); /* We have big problems, the child should exit. */
  389. }
  390. if (attr->cmdtype == APR_SHELLCMD ||
  391. attr->cmdtype == APR_SHELLCMD_ENV) {
  392. int onearg_len = 0;
  393. const char *newargs[4];
  394. newargs[0] = SHELL_PATH;
  395. newargs[1] = "-c";
  396. i = 0;
  397. while (args[i]) {
  398. onearg_len += strlen(args[i]);
  399. onearg_len++; /* for space delimiter */
  400. i++;
  401. }
  402. switch(i) {
  403. case 0:
  404. /* bad parameters; we're doomed */
  405. break;
  406. case 1:
  407. /* no args, or caller already built a single string from
  408. * progname and args
  409. */
  410. newargs[2] = args[0];
  411. break;
  412. default:
  413. {
  414. char *ch, *onearg;
  415. ch = onearg = fspr_palloc(pool, onearg_len);
  416. i = 0;
  417. while (args[i]) {
  418. size_t len = strlen(args[i]);
  419. memcpy(ch, args[i], len);
  420. ch += len;
  421. *ch = ' ';
  422. ++ch;
  423. ++i;
  424. }
  425. --ch; /* back up to trailing blank */
  426. *ch = '\0';
  427. newargs[2] = onearg;
  428. }
  429. }
  430. newargs[3] = NULL;
  431. if (attr->detached) {
  432. fspr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
  433. }
  434. if (attr->cmdtype == APR_SHELLCMD) {
  435. execve(SHELL_PATH, (char * const *) newargs, (char * const *)env);
  436. }
  437. else {
  438. execv(SHELL_PATH, (char * const *)newargs);
  439. }
  440. }
  441. else if (attr->cmdtype == APR_PROGRAM) {
  442. if (attr->detached) {
  443. fspr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
  444. }
  445. execve(progname, (char * const *)args, (char * const *)env);
  446. }
  447. else if (attr->cmdtype == APR_PROGRAM_ENV) {
  448. if (attr->detached) {
  449. fspr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
  450. }
  451. execv(progname, (char * const *)args);
  452. }
  453. else {
  454. /* APR_PROGRAM_PATH */
  455. if (attr->detached) {
  456. fspr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
  457. }
  458. execvp(progname, (char * const *)args);
  459. }
  460. if (attr->errfn) {
  461. char *desc;
  462. desc = fspr_psprintf(pool, "exec of '%s' failed",
  463. progname);
  464. attr->errfn(pool, errno, desc);
  465. }
  466. exit(-1); /* if we get here, there is a problem, so exit with an
  467. * error code. */
  468. }
  469. /* Parent process */
  470. if (attr->child_in) {
  471. fspr_file_close(attr->child_in);
  472. }
  473. if (attr->child_out) {
  474. fspr_file_close(attr->child_out);
  475. }
  476. if (attr->child_err) {
  477. fspr_file_close(attr->child_err);
  478. }
  479. return APR_SUCCESS;
  480. }
  481. APR_DECLARE(fspr_status_t) fspr_proc_wait_all_procs(fspr_proc_t *proc,
  482. int *exitcode,
  483. fspr_exit_why_e *exitwhy,
  484. fspr_wait_how_e waithow,
  485. fspr_pool_t *p)
  486. {
  487. proc->pid = -1;
  488. return fspr_proc_wait(proc, exitcode, exitwhy, waithow);
  489. }
  490. APR_DECLARE(fspr_status_t) fspr_proc_wait(fspr_proc_t *proc,
  491. int *exitcode, fspr_exit_why_e *exitwhy,
  492. fspr_wait_how_e waithow)
  493. {
  494. pid_t pstatus;
  495. int waitpid_options = WUNTRACED;
  496. int exit_int;
  497. int ignore;
  498. fspr_exit_why_e ignorewhy;
  499. if (exitcode == NULL) {
  500. exitcode = &ignore;
  501. }
  502. if (exitwhy == NULL) {
  503. exitwhy = &ignorewhy;
  504. }
  505. if (waithow != APR_WAIT) {
  506. waitpid_options |= WNOHANG;
  507. }
  508. do {
  509. pstatus = waitpid(proc->pid, &exit_int, waitpid_options);
  510. } while (pstatus < 0 && errno == EINTR);
  511. if (pstatus > 0) {
  512. proc->pid = pstatus;
  513. if (WIFEXITED(exit_int)) {
  514. *exitwhy = APR_PROC_EXIT;
  515. *exitcode = WEXITSTATUS(exit_int);
  516. }
  517. else if (WIFSIGNALED(exit_int)) {
  518. *exitwhy = APR_PROC_SIGNAL;
  519. #ifdef WCOREDUMP
  520. if (WCOREDUMP(exit_int)) {
  521. *exitwhy |= APR_PROC_SIGNAL_CORE;
  522. }
  523. #endif
  524. *exitcode = WTERMSIG(exit_int);
  525. }
  526. else {
  527. /* unexpected condition */
  528. return APR_EGENERAL;
  529. }
  530. return APR_CHILD_DONE;
  531. }
  532. else if (pstatus == 0) {
  533. return APR_CHILD_NOTDONE;
  534. }
  535. return errno;
  536. }
  537. APR_DECLARE(fspr_status_t) fspr_procattr_limit_set(fspr_procattr_t *attr,
  538. fspr_int32_t what,
  539. struct rlimit *limit)
  540. {
  541. switch(what) {
  542. case APR_LIMIT_CPU:
  543. #ifdef RLIMIT_CPU
  544. attr->limit_cpu = limit;
  545. break;
  546. #else
  547. return APR_ENOTIMPL;
  548. #endif
  549. case APR_LIMIT_MEM:
  550. #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
  551. attr->limit_mem = limit;
  552. break;
  553. #else
  554. return APR_ENOTIMPL;
  555. #endif
  556. case APR_LIMIT_NPROC:
  557. #ifdef RLIMIT_NPROC
  558. attr->limit_nproc = limit;
  559. break;
  560. #else
  561. return APR_ENOTIMPL;
  562. #endif
  563. case APR_LIMIT_NOFILE:
  564. #ifdef RLIMIT_NOFILE
  565. attr->limit_nofile = limit;
  566. break;
  567. #else
  568. return APR_ENOTIMPL;
  569. #endif
  570. }
  571. return APR_SUCCESS;
  572. }