proc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_arch_file_io.h"
  18. #include "fspr_strings.h"
  19. #include "fspr_portable.h"
  20. #include <proc.h>
  21. fspr_status_t fspr_netware_proc_cleanup(void *theproc)
  22. {
  23. fspr_proc_t *proc = theproc;
  24. int exit_int;
  25. int waitpid_options = WUNTRACED | WNOHANG;
  26. if (proc->pid > 0) {
  27. waitpid(proc->pid, &exit_int, waitpid_options);
  28. }
  29. /* NXVmDestroy(proc->pid); */
  30. return APR_SUCCESS;
  31. }
  32. APR_DECLARE(fspr_status_t) fspr_procattr_create(fspr_procattr_t **new,fspr_pool_t *pool)
  33. {
  34. (*new) = (fspr_procattr_t *)fspr_pcalloc(pool, sizeof(fspr_procattr_t));
  35. if ((*new) == NULL) {
  36. return APR_ENOMEM;
  37. }
  38. (*new)->pool = pool;
  39. (*new)->cmdtype = APR_PROGRAM;
  40. /* Default to a current path since NetWare doesn't handle it very well */
  41. fspr_filepath_get(&((*new)->currdir), APR_FILEPATH_NATIVE, pool);
  42. (*new)->detached = 1;
  43. return APR_SUCCESS;
  44. }
  45. APR_DECLARE(fspr_status_t) fspr_procattr_io_set(fspr_procattr_t *attr, fspr_int32_t in,
  46. fspr_int32_t out, fspr_int32_t err)
  47. {
  48. fspr_status_t status;
  49. if (in != 0) {
  50. if ((status = fspr_file_pipe_create(&attr->child_in, &attr->parent_in,
  51. attr->pool)) != APR_SUCCESS) {
  52. return status;
  53. }
  54. switch (in) {
  55. case APR_FULL_BLOCK:
  56. break;
  57. case APR_PARENT_BLOCK:
  58. fspr_file_pipe_timeout_set(attr->child_in, 0);
  59. break;
  60. case APR_CHILD_BLOCK:
  61. fspr_file_pipe_timeout_set(attr->parent_in, 0);
  62. break;
  63. default:
  64. fspr_file_pipe_timeout_set(attr->child_in, 0);
  65. fspr_file_pipe_timeout_set(attr->parent_in, 0);
  66. }
  67. }
  68. if (out) {
  69. if ((status = fspr_file_pipe_create(&attr->parent_out, &attr->child_out,
  70. attr->pool)) != APR_SUCCESS) {
  71. return status;
  72. }
  73. switch (out) {
  74. case APR_FULL_BLOCK:
  75. break;
  76. case APR_PARENT_BLOCK:
  77. fspr_file_pipe_timeout_set(attr->child_out, 0);
  78. break;
  79. case APR_CHILD_BLOCK:
  80. fspr_file_pipe_timeout_set(attr->parent_out, 0);
  81. break;
  82. default:
  83. fspr_file_pipe_timeout_set(attr->child_out, 0);
  84. fspr_file_pipe_timeout_set(attr->parent_out, 0);
  85. }
  86. }
  87. if (err) {
  88. if ((status = fspr_file_pipe_create(&attr->parent_err, &attr->child_err,
  89. attr->pool)) != APR_SUCCESS) {
  90. return status;
  91. }
  92. switch (err) {
  93. case APR_FULL_BLOCK:
  94. break;
  95. case APR_PARENT_BLOCK:
  96. fspr_file_pipe_timeout_set(attr->child_err, 0);
  97. break;
  98. case APR_CHILD_BLOCK:
  99. fspr_file_pipe_timeout_set(attr->parent_err, 0);
  100. break;
  101. default:
  102. fspr_file_pipe_timeout_set(attr->child_err, 0);
  103. fspr_file_pipe_timeout_set(attr->parent_err, 0);
  104. }
  105. }
  106. return APR_SUCCESS;
  107. }
  108. APR_DECLARE(fspr_status_t) fspr_procattr_child_in_set(fspr_procattr_t *attr, fspr_file_t *child_in,
  109. fspr_file_t *parent_in)
  110. {
  111. if (attr->child_in == NULL && attr->parent_in == NULL)
  112. fspr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
  113. if (child_in != NULL)
  114. fspr_file_dup2(attr->child_in, child_in, attr->pool);
  115. if (parent_in != NULL)
  116. fspr_file_dup2(attr->parent_in, parent_in, attr->pool);
  117. return APR_SUCCESS;
  118. }
  119. APR_DECLARE(fspr_status_t) fspr_procattr_child_out_set(fspr_procattr_t *attr, fspr_file_t *child_out,
  120. fspr_file_t *parent_out)
  121. {
  122. if (attr->child_out == NULL && attr->parent_out == NULL)
  123. fspr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
  124. if (child_out != NULL)
  125. fspr_file_dup2(attr->child_out, child_out, attr->pool);
  126. if (parent_out != NULL)
  127. fspr_file_dup2(attr->parent_out, parent_out, attr->pool);
  128. return APR_SUCCESS;
  129. }
  130. APR_DECLARE(fspr_status_t) fspr_procattr_child_err_set(fspr_procattr_t *attr, fspr_file_t *child_err,
  131. fspr_file_t *parent_err)
  132. {
  133. if (attr->child_err == NULL && attr->parent_err == NULL)
  134. fspr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
  135. if (child_err != NULL)
  136. fspr_file_dup2(attr->child_err, child_err, attr->pool);
  137. if (parent_err != NULL)
  138. fspr_file_dup2(attr->parent_err, parent_err, attr->pool);
  139. return APR_SUCCESS;
  140. }
  141. APR_DECLARE(fspr_status_t) fspr_procattr_dir_set(fspr_procattr_t *attr,
  142. const char *dir)
  143. {
  144. return fspr_filepath_merge(&attr->currdir, NULL, dir,
  145. APR_FILEPATH_NATIVE, attr->pool);
  146. }
  147. APR_DECLARE(fspr_status_t) fspr_procattr_cmdtype_set(fspr_procattr_t *attr,
  148. fspr_cmdtype_e cmd)
  149. {
  150. /* won't ever be called on this platform, so don't save the function pointer */
  151. return APR_SUCCESS;
  152. }
  153. APR_DECLARE(fspr_status_t) fspr_procattr_detach_set(fspr_procattr_t *attr, fspr_int32_t detach)
  154. {
  155. attr->detached = detach;
  156. return APR_SUCCESS;
  157. }
  158. #if APR_HAS_FORK
  159. APR_DECLARE(fspr_status_t) fspr_proc_fork(fspr_proc_t *proc, fspr_pool_t *pool)
  160. {
  161. int pid;
  162. if ((pid = fork()) < 0) {
  163. return errno;
  164. }
  165. else if (pid == 0) {
  166. proc->pid = pid;
  167. proc->in = NULL;
  168. proc->out = NULL;
  169. proc->err = NULL;
  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. #endif
  179. static fspr_status_t limit_proc(fspr_procattr_t *attr)
  180. {
  181. #if APR_HAVE_STRUCT_RLIMIT && APR_HAVE_SETRLIMIT
  182. #ifdef RLIMIT_CPU
  183. if (attr->limit_cpu != NULL) {
  184. if ((setrlimit(RLIMIT_CPU, attr->limit_cpu)) != 0) {
  185. return errno;
  186. }
  187. }
  188. #endif
  189. #ifdef RLIMIT_NPROC
  190. if (attr->limit_nproc != NULL) {
  191. if ((setrlimit(RLIMIT_NPROC, attr->limit_nproc)) != 0) {
  192. return errno;
  193. }
  194. }
  195. #endif
  196. #if defined(RLIMIT_AS)
  197. if (attr->limit_mem != NULL) {
  198. if ((setrlimit(RLIMIT_AS, attr->limit_mem)) != 0) {
  199. return errno;
  200. }
  201. }
  202. #elif defined(RLIMIT_DATA)
  203. if (attr->limit_mem != NULL) {
  204. if ((setrlimit(RLIMIT_DATA, attr->limit_mem)) != 0) {
  205. return errno;
  206. }
  207. }
  208. #elif defined(RLIMIT_VMEM)
  209. if (attr->limit_mem != NULL) {
  210. if ((setrlimit(RLIMIT_VMEM, attr->limit_mem)) != 0) {
  211. return errno;
  212. }
  213. }
  214. #endif
  215. #else
  216. /*
  217. * Maybe make a note in error_log that setrlimit isn't supported??
  218. */
  219. #endif
  220. return APR_SUCCESS;
  221. }
  222. APR_DECLARE(fspr_status_t) fspr_procattr_child_errfn_set(fspr_procattr_t *attr,
  223. fspr_child_errfn_t *errfn)
  224. {
  225. /* won't ever be called on this platform, so don't save the function pointer */
  226. return APR_SUCCESS;
  227. }
  228. APR_DECLARE(fspr_status_t) fspr_procattr_error_check_set(fspr_procattr_t *attr,
  229. fspr_int32_t chk)
  230. {
  231. /* won't ever be used on this platform, so don't save the flag */
  232. return APR_SUCCESS;
  233. }
  234. APR_DECLARE(fspr_status_t) fspr_procattr_addrspace_set(fspr_procattr_t *attr,
  235. fspr_int32_t addrspace)
  236. {
  237. attr->addrspace = addrspace;
  238. return APR_SUCCESS;
  239. }
  240. APR_DECLARE(fspr_status_t) fspr_proc_create(fspr_proc_t *newproc,
  241. const char *progname,
  242. const char * const *args,
  243. const char * const *env,
  244. fspr_procattr_t *attr,
  245. fspr_pool_t *pool)
  246. {
  247. wiring_t wire;
  248. int addr_space;
  249. wire.infd = attr->child_in ? attr->child_in->filedes : FD_UNUSED;
  250. wire.outfd = attr->child_out ? attr->child_out->filedes : FD_UNUSED;
  251. wire.errfd = attr->child_err ? attr->child_err->filedes : FD_UNUSED;
  252. newproc->in = attr->parent_in;
  253. newproc->out = attr->parent_out;
  254. newproc->err = attr->parent_err;
  255. /* attr->detached and PROC_DETACHED do not mean the same thing. attr->detached means
  256. * start the NLM in a separate address space. PROC_DETACHED means don't wait for the
  257. * NLM to unload by calling wait() or waitpid(), just clean up */
  258. addr_space = PROC_LOAD_SILENT | (attr->addrspace ? 0 : PROC_CURRENT_SPACE);
  259. addr_space |= (attr->detached ? PROC_DETACHED : 0);
  260. if (attr->currdir) {
  261. char *fullpath = NULL;
  262. fspr_status_t rv;
  263. if ((rv = fspr_filepath_merge(&fullpath, attr->currdir, progname,
  264. APR_FILEPATH_NATIVE, pool)) != APR_SUCCESS) {
  265. return rv;
  266. }
  267. progname = fullpath;
  268. }
  269. if ((newproc->pid = procve(progname, addr_space, (const char**)env, &wire,
  270. NULL, NULL, 0, NULL, (const char **)args)) == -1) {
  271. return errno;
  272. }
  273. if (attr->child_in) {
  274. fspr_pool_cleanup_kill(fspr_file_pool_get(attr->child_in),
  275. attr->child_in, fspr_unix_file_cleanup);
  276. fspr_file_close(attr->child_in);
  277. }
  278. if (attr->child_out) {
  279. fspr_pool_cleanup_kill(fspr_file_pool_get(attr->child_out),
  280. attr->child_out, fspr_unix_file_cleanup);
  281. fspr_file_close(attr->child_out);
  282. }
  283. if (attr->child_err) {
  284. fspr_pool_cleanup_kill(fspr_file_pool_get(attr->child_err),
  285. attr->child_err, fspr_unix_file_cleanup);
  286. fspr_file_close(attr->child_err);
  287. }
  288. fspr_pool_cleanup_register(pool, (void *)newproc, fspr_netware_proc_cleanup,
  289. fspr_pool_cleanup_null);
  290. return APR_SUCCESS;
  291. }
  292. APR_DECLARE(fspr_status_t) fspr_proc_wait_all_procs(fspr_proc_t *proc,
  293. int *exitcode,
  294. fspr_exit_why_e *exitwhy,
  295. fspr_wait_how_e waithow,
  296. fspr_pool_t *p)
  297. {
  298. proc->pid = -1;
  299. return fspr_proc_wait(proc, exitcode, exitwhy, waithow);
  300. }
  301. APR_DECLARE(fspr_status_t) fspr_proc_wait(fspr_proc_t *proc,
  302. int *exitcode, fspr_exit_why_e *exitwhy,
  303. fspr_wait_how_e waithow)
  304. {
  305. pid_t pstatus;
  306. int waitpid_options = WUNTRACED;
  307. int exit_int;
  308. int ignore;
  309. fspr_exit_why_e ignorewhy;
  310. if (exitcode == NULL) {
  311. exitcode = &ignore;
  312. }
  313. if (exitwhy == NULL) {
  314. exitwhy = &ignorewhy;
  315. }
  316. if (waithow != APR_WAIT) {
  317. waitpid_options |= WNOHANG;
  318. }
  319. /* If the pid is 0 then the process was started detached. There
  320. is no need to wait since there is nothing to wait for on a
  321. detached process. Starting a process as non-detached and
  322. then calling wait or waitpid could cause the thread to hang.
  323. The reason for this is because NetWare does not have a way
  324. to kill or even signal a process to be killed. Starting
  325. all processes as detached avoids the possibility of a
  326. thread hanging. */
  327. if (proc->pid == 0) {
  328. *exitwhy = APR_PROC_EXIT;
  329. *exitcode = 0;
  330. return APR_CHILD_DONE;
  331. }
  332. if ((pstatus = waitpid(proc->pid, &exit_int, waitpid_options)) > 0) {
  333. proc->pid = pstatus;
  334. if (WIFEXITED(exit_int)) {
  335. *exitwhy = APR_PROC_EXIT;
  336. *exitcode = WEXITSTATUS(exit_int);
  337. }
  338. else if (WIFSIGNALED(exit_int)) {
  339. *exitwhy = APR_PROC_SIGNAL;
  340. *exitcode = WIFTERMSIG(exit_int);
  341. }
  342. else {
  343. /* unexpected condition */
  344. return APR_EGENERAL;
  345. }
  346. return APR_CHILD_DONE;
  347. }
  348. else if (pstatus == 0) {
  349. return APR_CHILD_NOTDONE;
  350. }
  351. return errno;
  352. }
  353. APR_DECLARE(fspr_status_t) fspr_procattr_limit_set(fspr_procattr_t *attr, fspr_int32_t what,
  354. struct rlimit *limit)
  355. {
  356. switch(what) {
  357. case APR_LIMIT_CPU:
  358. #ifdef RLIMIT_CPU
  359. attr->limit_cpu = limit;
  360. break;
  361. #else
  362. return APR_ENOTIMPL;
  363. #endif
  364. case APR_LIMIT_MEM:
  365. #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
  366. attr->limit_mem = limit;
  367. break;
  368. #else
  369. return APR_ENOTIMPL;
  370. #endif
  371. case APR_LIMIT_NPROC:
  372. #ifdef RLIMIT_NPROC
  373. attr->limit_nproc = limit;
  374. break;
  375. #else
  376. return APR_ENOTIMPL;
  377. #endif
  378. }
  379. return APR_SUCCESS;
  380. }
  381. APR_DECLARE(fspr_status_t) fspr_procattr_user_set(fspr_procattr_t *attr,
  382. const char *username,
  383. const char *password)
  384. {
  385. /* Always return SUCCESS because NetWare threads don't run as a user */
  386. return APR_SUCCESS;
  387. }
  388. APR_DECLARE(fspr_status_t) fspr_procattr_group_set(fspr_procattr_t *attr,
  389. const char *groupname)
  390. {
  391. /* Always return SUCCESS because NetWare threads don't run within a group */
  392. return APR_SUCCESS;
  393. }