2
0

proc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. #define INCL_DOS
  17. #define INCL_DOSERRORS
  18. #include "fspr_arch_threadproc.h"
  19. #include "fspr_arch_file_io.h"
  20. #include "fspr_private.h"
  21. #include "fspr_thread_proc.h"
  22. #include "fspr_file_io.h"
  23. #include "fspr_general.h"
  24. #include "fspr_lib.h"
  25. #include "fspr_portable.h"
  26. #include "fspr_strings.h"
  27. #include "fspr_signal.h"
  28. #include <signal.h>
  29. #include <string.h>
  30. #include <sys/wait.h>
  31. #include <unistd.h>
  32. #include <process.h>
  33. #include <stdlib.h>
  34. APR_DECLARE(fspr_status_t) fspr_procattr_create(fspr_procattr_t **new, fspr_pool_t *pool)
  35. {
  36. (*new) = (fspr_procattr_t *)fspr_palloc(pool,
  37. sizeof(fspr_procattr_t));
  38. if ((*new) == NULL) {
  39. return APR_ENOMEM;
  40. }
  41. (*new)->pool = pool;
  42. (*new)->parent_in = NULL;
  43. (*new)->child_in = NULL;
  44. (*new)->parent_out = NULL;
  45. (*new)->child_out = NULL;
  46. (*new)->parent_err = NULL;
  47. (*new)->child_err = NULL;
  48. (*new)->currdir = NULL;
  49. (*new)->cmdtype = APR_PROGRAM;
  50. (*new)->detached = FALSE;
  51. return APR_SUCCESS;
  52. }
  53. APR_DECLARE(fspr_status_t) fspr_procattr_io_set(fspr_procattr_t *attr, fspr_int32_t in,
  54. fspr_int32_t out, fspr_int32_t err)
  55. {
  56. fspr_status_t stat;
  57. if (in) {
  58. if ((stat = fspr_file_pipe_create(&attr->child_in, &attr->parent_in,
  59. attr->pool)) != APR_SUCCESS) {
  60. return stat;
  61. }
  62. switch (in) {
  63. case APR_FULL_BLOCK:
  64. break;
  65. case APR_PARENT_BLOCK:
  66. fspr_file_pipe_timeout_set(attr->child_in, 0);
  67. break;
  68. case APR_CHILD_BLOCK:
  69. fspr_file_pipe_timeout_set(attr->parent_in, 0);
  70. break;
  71. default:
  72. fspr_file_pipe_timeout_set(attr->child_in, 0);
  73. fspr_file_pipe_timeout_set(attr->parent_in, 0);
  74. }
  75. }
  76. if (out) {
  77. if ((stat = fspr_file_pipe_create(&attr->parent_out, &attr->child_out,
  78. attr->pool)) != APR_SUCCESS) {
  79. return stat;
  80. }
  81. switch (out) {
  82. case APR_FULL_BLOCK:
  83. break;
  84. case APR_PARENT_BLOCK:
  85. fspr_file_pipe_timeout_set(attr->child_out, 0);
  86. break;
  87. case APR_CHILD_BLOCK:
  88. fspr_file_pipe_timeout_set(attr->parent_out, 0);
  89. break;
  90. default:
  91. fspr_file_pipe_timeout_set(attr->child_out, 0);
  92. fspr_file_pipe_timeout_set(attr->parent_out, 0);
  93. }
  94. }
  95. if (err) {
  96. if ((stat = fspr_file_pipe_create(&attr->parent_err, &attr->child_err,
  97. attr->pool)) != APR_SUCCESS) {
  98. return stat;
  99. }
  100. switch (err) {
  101. case APR_FULL_BLOCK:
  102. break;
  103. case APR_PARENT_BLOCK:
  104. fspr_file_pipe_timeout_set(attr->child_err, 0);
  105. break;
  106. case APR_CHILD_BLOCK:
  107. fspr_file_pipe_timeout_set(attr->parent_err, 0);
  108. break;
  109. default:
  110. fspr_file_pipe_timeout_set(attr->child_err, 0);
  111. fspr_file_pipe_timeout_set(attr->parent_err, 0);
  112. }
  113. }
  114. return APR_SUCCESS;
  115. }
  116. APR_DECLARE(fspr_status_t) fspr_procattr_child_in_set(fspr_procattr_t *attr, fspr_file_t *child_in,
  117. fspr_file_t *parent_in)
  118. {
  119. if (attr->child_in == NULL && attr->parent_in == NULL)
  120. fspr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
  121. if (child_in != NULL)
  122. fspr_file_dup(&attr->child_in, child_in, attr->pool);
  123. if (parent_in != NULL)
  124. fspr_file_dup(&attr->parent_in, parent_in, attr->pool);
  125. return APR_SUCCESS;
  126. }
  127. APR_DECLARE(fspr_status_t) fspr_procattr_child_out_set(fspr_procattr_t *attr, fspr_file_t *child_out,
  128. fspr_file_t *parent_out)
  129. {
  130. if (attr->child_out == NULL && attr->parent_out == NULL)
  131. fspr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
  132. if (child_out != NULL)
  133. fspr_file_dup(&attr->child_out, child_out, attr->pool);
  134. if (parent_out != NULL)
  135. fspr_file_dup(&attr->parent_out, parent_out, attr->pool);
  136. return APR_SUCCESS;
  137. }
  138. APR_DECLARE(fspr_status_t) fspr_procattr_child_err_set(fspr_procattr_t *attr, fspr_file_t *child_err,
  139. fspr_file_t *parent_err)
  140. {
  141. if (attr->child_err == NULL && attr->parent_err == NULL)
  142. fspr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
  143. if (child_err != NULL)
  144. fspr_file_dup(&attr->child_err, child_err, attr->pool);
  145. if (parent_err != NULL)
  146. fspr_file_dup(&attr->parent_err, parent_err, attr->pool);
  147. return APR_SUCCESS;
  148. }
  149. APR_DECLARE(fspr_status_t) fspr_procattr_dir_set(fspr_procattr_t *attr, const char *dir)
  150. {
  151. attr->currdir = fspr_pstrdup(attr->pool, dir);
  152. if (attr->currdir) {
  153. return APR_SUCCESS;
  154. }
  155. return APR_ENOMEM;
  156. }
  157. APR_DECLARE(fspr_status_t) fspr_procattr_cmdtype_set(fspr_procattr_t *attr,
  158. fspr_cmdtype_e cmd)
  159. {
  160. attr->cmdtype = cmd;
  161. return APR_SUCCESS;
  162. }
  163. APR_DECLARE(fspr_status_t) fspr_procattr_detach_set(fspr_procattr_t *attr, fspr_int32_t detach)
  164. {
  165. attr->detached = detach;
  166. return APR_SUCCESS;
  167. }
  168. APR_DECLARE(fspr_status_t) fspr_proc_fork(fspr_proc_t *proc, fspr_pool_t *pool)
  169. {
  170. int pid;
  171. if ((pid = fork()) < 0) {
  172. return errno;
  173. }
  174. else if (pid == 0) {
  175. proc->pid = pid;
  176. proc->in = NULL;
  177. proc->out = NULL;
  178. proc->err = NULL;
  179. return APR_INCHILD;
  180. }
  181. proc->pid = pid;
  182. proc->in = NULL;
  183. proc->out = NULL;
  184. proc->err = NULL;
  185. return APR_INPARENT;
  186. }
  187. /* quotes in the string are doubled up.
  188. * Used to escape quotes in args passed to OS/2's cmd.exe
  189. */
  190. static char *double_quotes(fspr_pool_t *pool, const char *str)
  191. {
  192. int num_quotes = 0;
  193. int len = 0;
  194. char *quote_doubled_str, *dest;
  195. while (str[len]) {
  196. num_quotes += str[len++] == '\"';
  197. }
  198. quote_doubled_str = fspr_palloc(pool, len + num_quotes + 1);
  199. dest = quote_doubled_str;
  200. while (*str) {
  201. if (*str == '\"')
  202. *(dest++) = '\"';
  203. *(dest++) = *(str++);
  204. }
  205. *dest = 0;
  206. return quote_doubled_str;
  207. }
  208. APR_DECLARE(fspr_status_t) fspr_procattr_child_errfn_set(fspr_procattr_t *attr,
  209. fspr_child_errfn_t *errfn)
  210. {
  211. /* won't ever be called on this platform, so don't save the function pointer */
  212. return APR_SUCCESS;
  213. }
  214. APR_DECLARE(fspr_status_t) fspr_procattr_error_check_set(fspr_procattr_t *attr,
  215. fspr_int32_t chk)
  216. {
  217. /* won't ever be used on this platform, so don't save the flag */
  218. return APR_SUCCESS;
  219. }
  220. APR_DECLARE(fspr_status_t) fspr_procattr_addrspace_set(fspr_procattr_t *attr,
  221. fspr_int32_t addrspace)
  222. {
  223. /* won't ever be used on this platform, so don't save the flag */
  224. return APR_SUCCESS;
  225. }
  226. APR_DECLARE(fspr_status_t) fspr_proc_create(fspr_proc_t *proc, const char *progname,
  227. const char * const *args,
  228. const char * const *env,
  229. fspr_procattr_t *attr, fspr_pool_t *pool)
  230. {
  231. int i, arg, numargs, cmdlen;
  232. fspr_status_t status;
  233. const char **newargs;
  234. char savedir[300];
  235. HFILE save_in, save_out, save_err, dup;
  236. int criticalsection = FALSE;
  237. char *extension, *newprogname, *extra_arg = NULL, *cmdline, *cmdline_pos;
  238. char interpreter[1024];
  239. char error_object[260];
  240. fspr_file_t *progfile;
  241. int env_len, e;
  242. char *env_block, *env_block_pos;
  243. RESULTCODES rescodes;
  244. /* Prevent other threads from running while these process-wide resources are modified */
  245. if (attr->child_in || attr->child_out || attr->child_err || attr->currdir) {
  246. criticalsection = TRUE;
  247. DosEnterCritSec();
  248. }
  249. if (attr->child_in) {
  250. save_in = -1;
  251. DosDupHandle(STDIN_FILENO, &save_in);
  252. dup = STDIN_FILENO;
  253. DosDupHandle(attr->child_in->filedes, &dup);
  254. DosSetFHState(attr->parent_in->filedes, OPEN_FLAGS_NOINHERIT);
  255. }
  256. if (attr->child_out) {
  257. save_out = -1;
  258. DosDupHandle(STDOUT_FILENO, &save_out);
  259. dup = STDOUT_FILENO;
  260. DosDupHandle(attr->child_out->filedes, &dup);
  261. DosSetFHState(attr->parent_out->filedes, OPEN_FLAGS_NOINHERIT);
  262. }
  263. if (attr->child_err) {
  264. save_err = -1;
  265. DosDupHandle(STDERR_FILENO, &save_err);
  266. dup = STDERR_FILENO;
  267. DosDupHandle(attr->child_err->filedes, &dup);
  268. DosSetFHState(attr->parent_err->filedes, OPEN_FLAGS_NOINHERIT);
  269. }
  270. fspr_signal(SIGCHLD, SIG_DFL); /*not sure if this is needed or not */
  271. if (attr->currdir != NULL) {
  272. _getcwd2(savedir, sizeof(savedir));
  273. if (_chdir2(attr->currdir) < 0) {
  274. if (criticalsection)
  275. DosExitCritSec();
  276. return errno;
  277. }
  278. }
  279. interpreter[0] = 0;
  280. extension = strrchr(progname, '.');
  281. if (extension == NULL || strchr(extension, '/') || strchr(extension, '\\'))
  282. extension = "";
  283. /* ### how to handle APR_PROGRAM_ENV and APR_PROGRAM_PATH? */
  284. if (attr->cmdtype == APR_SHELLCMD ||
  285. attr->cmdtype == APR_SHELLCMD_ENV ||
  286. strcasecmp(extension, ".cmd") == 0) {
  287. strcpy(interpreter, "#!" SHELL_PATH);
  288. extra_arg = "/C";
  289. } else if (stricmp(extension, ".exe") != 0) {
  290. status = fspr_file_open(&progfile, progname, APR_READ|APR_BUFFERED, 0, pool);
  291. if (status != APR_SUCCESS && APR_STATUS_IS_ENOENT(status)) {
  292. progname = fspr_pstrcat(pool, progname, ".exe", NULL);
  293. }
  294. if (status == APR_SUCCESS) {
  295. status = fspr_file_gets(interpreter, sizeof(interpreter), progfile);
  296. if (status == APR_SUCCESS) {
  297. if (interpreter[0] == '#' && interpreter[1] == '!') {
  298. /* delete CR/LF & any other whitespace off the end */
  299. int end = strlen(interpreter) - 1;
  300. while (end >= 0 && fspr_isspace(interpreter[end])) {
  301. interpreter[end] = '\0';
  302. end--;
  303. }
  304. if (interpreter[2] != '/' && interpreter[2] != '\\' && interpreter[3] != ':') {
  305. char buffer[300];
  306. if (DosSearchPath(SEARCH_ENVIRONMENT, "PATH", interpreter+2, buffer, sizeof(buffer)) == 0) {
  307. strcpy(interpreter+2, buffer);
  308. } else {
  309. strcat(interpreter, ".exe");
  310. if (DosSearchPath(SEARCH_ENVIRONMENT, "PATH", interpreter+2, buffer, sizeof(buffer)) == 0) {
  311. strcpy(interpreter+2, buffer);
  312. }
  313. }
  314. }
  315. } else {
  316. interpreter[0] = 0;
  317. }
  318. }
  319. fspr_file_close(progfile);
  320. }
  321. }
  322. i = 0;
  323. while (args && args[i]) {
  324. i++;
  325. }
  326. newargs = (const char **)fspr_palloc(pool, sizeof (char *) * (i + 4));
  327. numargs = 0;
  328. if (interpreter[0])
  329. newargs[numargs++] = interpreter + 2;
  330. if (extra_arg)
  331. newargs[numargs++] = "/c";
  332. newargs[numargs++] = newprogname = fspr_pstrdup(pool, progname);
  333. arg = 1;
  334. while (args && args[arg]) {
  335. newargs[numargs++] = args[arg++];
  336. }
  337. newargs[numargs] = NULL;
  338. for (i=0; newprogname[i]; i++)
  339. if (newprogname[i] == '/')
  340. newprogname[i] = '\\';
  341. cmdlen = 0;
  342. for (i=0; i<numargs; i++)
  343. cmdlen += strlen(newargs[i]) + 3;
  344. cmdline = fspr_palloc(pool, cmdlen + 2);
  345. cmdline_pos = cmdline;
  346. for (i=0; i<numargs; i++) {
  347. const char *a = newargs[i];
  348. if (strpbrk(a, "&|<>\" "))
  349. a = fspr_pstrcat(pool, "\"", double_quotes(pool, a), "\"", NULL);
  350. if (i)
  351. *(cmdline_pos++) = ' ';
  352. strcpy(cmdline_pos, a);
  353. cmdline_pos += strlen(cmdline_pos);
  354. }
  355. *(++cmdline_pos) = 0; /* Add required second terminator */
  356. cmdline_pos = strchr(cmdline, ' ');
  357. if (cmdline_pos) {
  358. *cmdline_pos = 0;
  359. cmdline_pos++;
  360. }
  361. /* Create environment block from list of envariables */
  362. if (env) {
  363. for (env_len=1, e=0; env[e]; e++)
  364. env_len += strlen(env[e]) + 1;
  365. env_block = fspr_palloc(pool, env_len);
  366. env_block_pos = env_block;
  367. for (e=0; env[e]; e++) {
  368. strcpy(env_block_pos, env[e]);
  369. env_block_pos += strlen(env_block_pos) + 1;
  370. }
  371. *env_block_pos = 0; /* environment block is terminated by a double null */
  372. } else
  373. env_block = NULL;
  374. status = DosExecPgm(error_object, sizeof(error_object),
  375. attr->detached ? EXEC_BACKGROUND : EXEC_ASYNCRESULT,
  376. cmdline, env_block, &rescodes, cmdline);
  377. proc->pid = rescodes.codeTerminate;
  378. if (attr->currdir != NULL) {
  379. chdir(savedir);
  380. }
  381. if (attr->child_in) {
  382. fspr_file_close(attr->child_in);
  383. dup = STDIN_FILENO;
  384. DosDupHandle(save_in, &dup);
  385. DosClose(save_in);
  386. }
  387. if (attr->child_out) {
  388. fspr_file_close(attr->child_out);
  389. dup = STDOUT_FILENO;
  390. DosDupHandle(save_out, &dup);
  391. DosClose(save_out);
  392. }
  393. if (attr->child_err) {
  394. fspr_file_close(attr->child_err);
  395. dup = STDERR_FILENO;
  396. DosDupHandle(save_err, &dup);
  397. DosClose(save_err);
  398. }
  399. if (criticalsection)
  400. DosExitCritSec();
  401. proc->in = attr->parent_in;
  402. proc->err = attr->parent_err;
  403. proc->out = attr->parent_out;
  404. return status;
  405. }
  406. static void proces_result_codes(RESULTCODES codes,
  407. int *exitcode,
  408. fspr_exit_why_e *exitwhy)
  409. {
  410. int result = 0;
  411. fspr_exit_why_e why = APR_PROC_EXIT;
  412. switch (codes.codeTerminate) {
  413. case TC_EXIT: /* Normal exit */
  414. why = APR_PROC_EXIT;
  415. result = codes.codeResult;
  416. break;
  417. case TC_HARDERROR: /* Hard error halt */
  418. why = APR_PROC_SIGNAL;
  419. result = SIGSYS;
  420. break;
  421. case TC_KILLPROCESS: /* Was killed by a DosKillProcess() */
  422. why = APR_PROC_SIGNAL;
  423. result = SIGKILL;
  424. break;
  425. case TC_TRAP: /* TRAP in 16 bit code */
  426. case TC_EXCEPTION: /* Threw an exception (32 bit code) */
  427. why = APR_PROC_SIGNAL;
  428. switch (codes.codeResult | XCPT_FATAL_EXCEPTION) {
  429. case XCPT_ACCESS_VIOLATION:
  430. result = SIGSEGV;
  431. break;
  432. case XCPT_ILLEGAL_INSTRUCTION:
  433. result = SIGILL;
  434. break;
  435. case XCPT_FLOAT_DIVIDE_BY_ZERO:
  436. case XCPT_INTEGER_DIVIDE_BY_ZERO:
  437. result = SIGFPE;
  438. break;
  439. default:
  440. result = codes.codeResult;
  441. break;
  442. }
  443. }
  444. if (exitcode) {
  445. *exitcode = result;
  446. }
  447. if (exitwhy) {
  448. *exitwhy = why;
  449. }
  450. }
  451. APR_DECLARE(fspr_status_t) fspr_proc_wait_all_procs(fspr_proc_t *proc,
  452. int *exitcode,
  453. fspr_exit_why_e *exitwhy,
  454. fspr_wait_how_e waithow,
  455. fspr_pool_t *p)
  456. {
  457. RESULTCODES codes;
  458. ULONG rc;
  459. PID pid;
  460. rc = DosWaitChild(DCWA_PROCESSTREE, waithow == APR_WAIT ? DCWW_WAIT : DCWW_NOWAIT, &codes, &pid, 0);
  461. if (rc == 0) {
  462. proc->pid = pid;
  463. proces_result_codes(codes, exitcode, exitwhy);
  464. return APR_CHILD_DONE;
  465. } else if (rc == ERROR_CHILD_NOT_COMPLETE) {
  466. return APR_CHILD_NOTDONE;
  467. }
  468. return APR_OS2_STATUS(rc);
  469. }
  470. APR_DECLARE(fspr_status_t) fspr_proc_wait(fspr_proc_t *proc,
  471. int *exitcode, fspr_exit_why_e *exitwhy,
  472. fspr_wait_how_e waithow)
  473. {
  474. RESULTCODES codes;
  475. ULONG rc;
  476. PID pid;
  477. rc = DosWaitChild(DCWA_PROCESS, waithow == APR_WAIT ? DCWW_WAIT : DCWW_NOWAIT, &codes, &pid, proc->pid);
  478. if (rc == 0) {
  479. proces_result_codes(codes, exitcode, exitwhy);
  480. return APR_CHILD_DONE;
  481. } else if (rc == ERROR_CHILD_NOT_COMPLETE) {
  482. return APR_CHILD_NOTDONE;
  483. }
  484. return APR_OS2_STATUS(rc);
  485. }
  486. APR_DECLARE(fspr_status_t) fspr_proc_detach(int daemonize)
  487. {
  488. return APR_ENOTIMPL;
  489. }
  490. APR_DECLARE(fspr_status_t) fspr_procattr_user_set(fspr_procattr_t *attr,
  491. const char *username,
  492. const char *password)
  493. {
  494. return APR_ENOTIMPL;
  495. }
  496. APR_DECLARE(fspr_status_t) fspr_procattr_group_set(fspr_procattr_t *attr,
  497. const char *groupname)
  498. {
  499. return APR_ENOTIMPL;
  500. }