proc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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 "apr_arch_threadproc.h"
  17. #include "apr_strings.h"
  18. #include "apr_portable.h"
  19. #include "apr_signal.h"
  20. #include "apr_random.h"
  21. APR_DECLARE(apr_status_t) apr_procattr_create(apr_procattr_t **new,
  22. apr_pool_t *pool)
  23. {
  24. (*new) = (apr_procattr_t *)apr_pcalloc(pool, sizeof(apr_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(apr_status_t) apr_procattr_io_set(apr_procattr_t *attr,
  34. apr_int32_t in,
  35. apr_int32_t out,
  36. apr_int32_t err)
  37. {
  38. apr_status_t status;
  39. if (in != 0) {
  40. if ((status = apr_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. apr_file_pipe_timeout_set(attr->child_in, 0);
  49. break;
  50. case APR_CHILD_BLOCK:
  51. apr_file_pipe_timeout_set(attr->parent_in, 0);
  52. break;
  53. default:
  54. apr_file_pipe_timeout_set(attr->child_in, 0);
  55. apr_file_pipe_timeout_set(attr->parent_in, 0);
  56. }
  57. }
  58. if (out) {
  59. if ((status = apr_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. apr_file_pipe_timeout_set(attr->child_out, 0);
  68. break;
  69. case APR_CHILD_BLOCK:
  70. apr_file_pipe_timeout_set(attr->parent_out, 0);
  71. break;
  72. default:
  73. apr_file_pipe_timeout_set(attr->child_out, 0);
  74. apr_file_pipe_timeout_set(attr->parent_out, 0);
  75. }
  76. }
  77. if (err) {
  78. if ((status = apr_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. apr_file_pipe_timeout_set(attr->child_err, 0);
  87. break;
  88. case APR_CHILD_BLOCK:
  89. apr_file_pipe_timeout_set(attr->parent_err, 0);
  90. break;
  91. default:
  92. apr_file_pipe_timeout_set(attr->child_err, 0);
  93. apr_file_pipe_timeout_set(attr->parent_err, 0);
  94. }
  95. }
  96. return APR_SUCCESS;
  97. }
  98. APR_DECLARE(apr_status_t) apr_procattr_child_in_set(apr_procattr_t *attr,
  99. apr_file_t *child_in,
  100. apr_file_t *parent_in)
  101. {
  102. apr_status_t rv = APR_SUCCESS;
  103. if (attr->child_in == NULL && attr->parent_in == NULL)
  104. rv = apr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
  105. if (child_in != NULL && rv == APR_SUCCESS)
  106. rv = apr_file_dup2(attr->child_in, child_in, attr->pool);
  107. if (parent_in != NULL && rv == APR_SUCCESS)
  108. rv = apr_file_dup2(attr->parent_in, parent_in, attr->pool);
  109. return rv;
  110. }
  111. APR_DECLARE(apr_status_t) apr_procattr_child_out_set(apr_procattr_t *attr,
  112. apr_file_t *child_out,
  113. apr_file_t *parent_out)
  114. {
  115. apr_status_t rv = APR_SUCCESS;
  116. if (attr->child_out == NULL && attr->parent_out == NULL)
  117. rv = apr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
  118. if (child_out != NULL && rv == APR_SUCCESS)
  119. rv = apr_file_dup2(attr->child_out, child_out, attr->pool);
  120. if (parent_out != NULL && rv == APR_SUCCESS)
  121. rv = apr_file_dup2(attr->parent_out, parent_out, attr->pool);
  122. return rv;
  123. }
  124. APR_DECLARE(apr_status_t) apr_procattr_child_err_set(apr_procattr_t *attr,
  125. apr_file_t *child_err,
  126. apr_file_t *parent_err)
  127. {
  128. apr_status_t rv = APR_SUCCESS;
  129. if (attr->child_err == NULL && attr->parent_err == NULL)
  130. rv = apr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
  131. if (child_err != NULL && rv == APR_SUCCESS)
  132. rv = apr_file_dup2(attr->child_err, child_err, attr->pool);
  133. if (parent_err != NULL && rv == APR_SUCCESS)
  134. rv = apr_file_dup2(attr->parent_err, parent_err, attr->pool);
  135. return rv;
  136. }
  137. APR_DECLARE(apr_status_t) apr_procattr_dir_set(apr_procattr_t *attr,
  138. const char *dir)
  139. {
  140. attr->currdir = apr_pstrdup(attr->pool, dir);
  141. if (attr->currdir) {
  142. return APR_SUCCESS;
  143. }
  144. return APR_ENOMEM;
  145. }
  146. APR_DECLARE(apr_status_t) apr_procattr_cmdtype_set(apr_procattr_t *attr,
  147. apr_cmdtype_e cmd)
  148. {
  149. attr->cmdtype = cmd;
  150. return APR_SUCCESS;
  151. }
  152. APR_DECLARE(apr_status_t) apr_procattr_detach_set(apr_procattr_t *attr,
  153. apr_int32_t detach)
  154. {
  155. attr->detached = detach;
  156. return APR_SUCCESS;
  157. }
  158. APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_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. apr_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 apr_status_t limit_proc(apr_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(apr_status_t) apr_procattr_child_errfn_set(apr_procattr_t *attr,
  229. apr_child_errfn_t *errfn)
  230. {
  231. attr->errfn = errfn;
  232. return APR_SUCCESS;
  233. }
  234. APR_DECLARE(apr_status_t) apr_procattr_error_check_set(apr_procattr_t *attr,
  235. apr_int32_t chk)
  236. {
  237. attr->errchk = chk;
  238. return APR_SUCCESS;
  239. }
  240. APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr,
  241. apr_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(apr_status_t) apr_procattr_user_set(apr_procattr_t *attr,
  247. const char *username,
  248. const char *password)
  249. {
  250. apr_status_t rv;
  251. apr_gid_t gid;
  252. if ((rv = apr_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(apr_status_t) apr_procattr_group_set(apr_procattr_t *attr,
  264. const char *groupname)
  265. {
  266. apr_status_t rv;
  267. if ((rv = apr_gid_get(&attr->gid, groupname, attr->pool)) != APR_SUCCESS)
  268. attr->gid = -1;
  269. return rv;
  270. }
  271. APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new,
  272. const char *progname,
  273. const char * const *args,
  274. const char * const *env,
  275. apr_procattr_t *attr,
  276. apr_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. int status;
  318. /* child process */
  319. /*
  320. * If we do exec cleanup before the dup2() calls to set up pipes
  321. * on 0-2, we accidentally close the pipes used by programs like
  322. * mod_cgid.
  323. *
  324. * If we do exec cleanup after the dup2() calls, cleanup can accidentally
  325. * close our pipes which replaced any files which previously had
  326. * descriptors 0-2.
  327. *
  328. * The solution is to kill the cleanup for the pipes, then do
  329. * exec cleanup, then do the dup2() calls.
  330. */
  331. if (attr->child_in) {
  332. apr_pool_cleanup_kill(apr_file_pool_get(attr->child_in),
  333. attr->child_in, apr_unix_file_cleanup);
  334. }
  335. if (attr->child_out) {
  336. apr_pool_cleanup_kill(apr_file_pool_get(attr->child_out),
  337. attr->child_out, apr_unix_file_cleanup);
  338. }
  339. if (attr->child_err) {
  340. apr_pool_cleanup_kill(apr_file_pool_get(attr->child_err),
  341. attr->child_err, apr_unix_file_cleanup);
  342. }
  343. apr_pool_cleanup_for_exec();
  344. if (attr->child_in) {
  345. apr_file_close(attr->parent_in);
  346. dup2(attr->child_in->filedes, STDIN_FILENO);
  347. apr_file_close(attr->child_in);
  348. }
  349. if (attr->child_out) {
  350. apr_file_close(attr->parent_out);
  351. dup2(attr->child_out->filedes, STDOUT_FILENO);
  352. apr_file_close(attr->child_out);
  353. }
  354. if (attr->child_err) {
  355. apr_file_close(attr->parent_err);
  356. dup2(attr->child_err->filedes, STDERR_FILENO);
  357. apr_file_close(attr->child_err);
  358. }
  359. apr_signal(SIGCHLD, SIG_DFL); /* not sure if this is needed or not */
  360. if (attr->currdir != NULL) {
  361. if (chdir(attr->currdir) == -1) {
  362. if (attr->errfn) {
  363. attr->errfn(pool, errno, "change of working directory failed");
  364. }
  365. exit(-1); /* We have big problems, the child should exit. */
  366. }
  367. }
  368. /* Only try to switch if we are running as root */
  369. if (attr->gid != -1 && !geteuid()) {
  370. if ((status = setgid(attr->gid))) {
  371. if (attr->errfn) {
  372. attr->errfn(pool, errno, "setting of group failed");
  373. }
  374. exit(-1); /* We have big problems, the child should exit. */
  375. }
  376. }
  377. if (attr->uid != -1 && !geteuid()) {
  378. if ((status = setuid(attr->uid))) {
  379. if (attr->errfn) {
  380. attr->errfn(pool, errno, "setting of user failed");
  381. }
  382. exit(-1); /* We have big problems, the child should exit. */
  383. }
  384. }
  385. if ((status = limit_proc(attr)) != APR_SUCCESS) {
  386. if (attr->errfn) {
  387. attr->errfn(pool, errno, "setting of resource limits failed");
  388. }
  389. exit(-1); /* We have big problems, the child should exit. */
  390. }
  391. if (attr->cmdtype == APR_SHELLCMD ||
  392. attr->cmdtype == APR_SHELLCMD_ENV) {
  393. int onearg_len = 0;
  394. const char *newargs[4];
  395. newargs[0] = SHELL_PATH;
  396. newargs[1] = "-c";
  397. i = 0;
  398. while (args[i]) {
  399. onearg_len += strlen(args[i]);
  400. onearg_len++; /* for space delimiter */
  401. i++;
  402. }
  403. switch(i) {
  404. case 0:
  405. /* bad parameters; we're doomed */
  406. break;
  407. case 1:
  408. /* no args, or caller already built a single string from
  409. * progname and args
  410. */
  411. newargs[2] = args[0];
  412. break;
  413. default:
  414. {
  415. char *ch, *onearg;
  416. ch = onearg = apr_palloc(pool, onearg_len);
  417. i = 0;
  418. while (args[i]) {
  419. size_t len = strlen(args[i]);
  420. memcpy(ch, args[i], len);
  421. ch += len;
  422. *ch = ' ';
  423. ++ch;
  424. ++i;
  425. }
  426. --ch; /* back up to trailing blank */
  427. *ch = '\0';
  428. newargs[2] = onearg;
  429. }
  430. }
  431. newargs[3] = NULL;
  432. if (attr->detached) {
  433. apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
  434. }
  435. if (attr->cmdtype == APR_SHELLCMD) {
  436. execve(SHELL_PATH, (char * const *) newargs, (char * const *)env);
  437. }
  438. else {
  439. execv(SHELL_PATH, (char * const *)newargs);
  440. }
  441. }
  442. else if (attr->cmdtype == APR_PROGRAM) {
  443. if (attr->detached) {
  444. apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
  445. }
  446. execve(progname, (char * const *)args, (char * const *)env);
  447. }
  448. else if (attr->cmdtype == APR_PROGRAM_ENV) {
  449. if (attr->detached) {
  450. apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
  451. }
  452. execv(progname, (char * const *)args);
  453. }
  454. else {
  455. /* APR_PROGRAM_PATH */
  456. if (attr->detached) {
  457. apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
  458. }
  459. execvp(progname, (char * const *)args);
  460. }
  461. if (attr->errfn) {
  462. char *desc;
  463. desc = apr_psprintf(pool, "exec of '%s' failed",
  464. progname);
  465. attr->errfn(pool, errno, desc);
  466. }
  467. exit(-1); /* if we get here, there is a problem, so exit with an
  468. * error code. */
  469. }
  470. /* Parent process */
  471. if (attr->child_in) {
  472. apr_file_close(attr->child_in);
  473. }
  474. if (attr->child_out) {
  475. apr_file_close(attr->child_out);
  476. }
  477. if (attr->child_err) {
  478. apr_file_close(attr->child_err);
  479. }
  480. return APR_SUCCESS;
  481. }
  482. APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
  483. int *exitcode,
  484. apr_exit_why_e *exitwhy,
  485. apr_wait_how_e waithow,
  486. apr_pool_t *p)
  487. {
  488. proc->pid = -1;
  489. return apr_proc_wait(proc, exitcode, exitwhy, waithow);
  490. }
  491. APR_DECLARE(apr_status_t) apr_proc_wait(apr_proc_t *proc,
  492. int *exitcode, apr_exit_why_e *exitwhy,
  493. apr_wait_how_e waithow)
  494. {
  495. pid_t pstatus;
  496. int waitpid_options = WUNTRACED;
  497. int exit_int;
  498. int ignore;
  499. apr_exit_why_e ignorewhy;
  500. if (exitcode == NULL) {
  501. exitcode = &ignore;
  502. }
  503. if (exitwhy == NULL) {
  504. exitwhy = &ignorewhy;
  505. }
  506. if (waithow != APR_WAIT) {
  507. waitpid_options |= WNOHANG;
  508. }
  509. do {
  510. pstatus = waitpid(proc->pid, &exit_int, waitpid_options);
  511. } while (pstatus < 0 && errno == EINTR);
  512. if (pstatus > 0) {
  513. proc->pid = pstatus;
  514. if (WIFEXITED(exit_int)) {
  515. *exitwhy = APR_PROC_EXIT;
  516. *exitcode = WEXITSTATUS(exit_int);
  517. }
  518. else if (WIFSIGNALED(exit_int)) {
  519. *exitwhy = APR_PROC_SIGNAL;
  520. #ifdef WCOREDUMP
  521. if (WCOREDUMP(exit_int)) {
  522. *exitwhy |= APR_PROC_SIGNAL_CORE;
  523. }
  524. #endif
  525. *exitcode = WTERMSIG(exit_int);
  526. }
  527. else {
  528. /* unexpected condition */
  529. return APR_EGENERAL;
  530. }
  531. return APR_CHILD_DONE;
  532. }
  533. else if (pstatus == 0) {
  534. return APR_CHILD_NOTDONE;
  535. }
  536. return errno;
  537. }
  538. APR_DECLARE(apr_status_t) apr_procattr_limit_set(apr_procattr_t *attr,
  539. apr_int32_t what,
  540. struct rlimit *limit)
  541. {
  542. switch(what) {
  543. case APR_LIMIT_CPU:
  544. #ifdef RLIMIT_CPU
  545. attr->limit_cpu = limit;
  546. break;
  547. #else
  548. return APR_ENOTIMPL;
  549. #endif
  550. case APR_LIMIT_MEM:
  551. #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
  552. attr->limit_mem = limit;
  553. break;
  554. #else
  555. return APR_ENOTIMPL;
  556. #endif
  557. case APR_LIMIT_NPROC:
  558. #ifdef RLIMIT_NPROC
  559. attr->limit_nproc = limit;
  560. break;
  561. #else
  562. return APR_ENOTIMPL;
  563. #endif
  564. case APR_LIMIT_NOFILE:
  565. #ifdef RLIMIT_NOFILE
  566. attr->limit_nofile = limit;
  567. break;
  568. #else
  569. return APR_ENOTIMPL;
  570. #endif
  571. }
  572. return APR_SUCCESS;
  573. }