proc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. struct send_pipe {
  19. int in;
  20. int out;
  21. int err;
  22. };
  23. APR_DECLARE(fspr_status_t) fspr_procattr_create(fspr_procattr_t **new, fspr_pool_t *pool)
  24. {
  25. (*new) = (fspr_procattr_t *)fspr_palloc(pool,
  26. sizeof(fspr_procattr_t));
  27. if ((*new) == NULL) {
  28. return APR_ENOMEM;
  29. }
  30. (*new)->pool = pool;
  31. (*new)->parent_in = NULL;
  32. (*new)->child_in = NULL;
  33. (*new)->parent_out = NULL;
  34. (*new)->child_out = NULL;
  35. (*new)->parent_err = NULL;
  36. (*new)->child_err = NULL;
  37. (*new)->currdir = NULL;
  38. (*new)->cmdtype = APR_PROGRAM;
  39. (*new)->detached = 0;
  40. return APR_SUCCESS;
  41. }
  42. APR_DECLARE(fspr_status_t) fspr_procattr_io_set(fspr_procattr_t *attr, fspr_int32_t in,
  43. fspr_int32_t out, fspr_int32_t err)
  44. {
  45. fspr_status_t status;
  46. if (in != 0) {
  47. if ((status = fspr_file_pipe_create(&attr->child_in, &attr->parent_in,
  48. attr->pool)) != APR_SUCCESS) {
  49. return status;
  50. }
  51. switch (in) {
  52. case APR_FULL_BLOCK:
  53. fspr_file_pipe_timeout_set(attr->child_in, -1);
  54. fspr_file_pipe_timeout_set(attr->parent_in, -1);
  55. break;
  56. case APR_PARENT_BLOCK:
  57. fspr_file_pipe_timeout_set(attr->child_in, -1);
  58. break;
  59. case APR_CHILD_BLOCK:
  60. fspr_file_pipe_timeout_set(attr->parent_in, -1);
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. if (out) {
  67. if ((status = fspr_file_pipe_create(&attr->parent_out, &attr->child_out,
  68. attr->pool)) != APR_SUCCESS) {
  69. return status;
  70. }
  71. switch (out) {
  72. case APR_FULL_BLOCK:
  73. fspr_file_pipe_timeout_set(attr->child_out, -1);
  74. fspr_file_pipe_timeout_set(attr->parent_out, -1);
  75. break;
  76. case APR_PARENT_BLOCK:
  77. fspr_file_pipe_timeout_set(attr->child_out, -1);
  78. break;
  79. case APR_CHILD_BLOCK:
  80. fspr_file_pipe_timeout_set(attr->parent_out, -1);
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. if (err) {
  87. if ((status = fspr_file_pipe_create(&attr->parent_err, &attr->child_err,
  88. attr->pool)) != APR_SUCCESS) {
  89. return status;
  90. }
  91. switch (err) {
  92. case APR_FULL_BLOCK:
  93. fspr_file_pipe_timeout_set(attr->child_err, -1);
  94. fspr_file_pipe_timeout_set(attr->parent_err, -1);
  95. break;
  96. case APR_PARENT_BLOCK:
  97. fspr_file_pipe_timeout_set(attr->child_err, -1);
  98. break;
  99. case APR_CHILD_BLOCK:
  100. fspr_file_pipe_timeout_set(attr->parent_err, -1);
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. return APR_SUCCESS;
  107. }
  108. APR_DECLARE(fspr_status_t) fspr_procattr_dir_set(fspr_procattr_t *attr,
  109. const char *dir)
  110. {
  111. char * cwd;
  112. if (dir[0] != '/') {
  113. cwd = (char*)malloc(sizeof(char) * PATH_MAX);
  114. getcwd(cwd, PATH_MAX);
  115. attr->currdir = (char *)fspr_pstrcat(attr->pool, cwd, "/", dir, NULL);
  116. free(cwd);
  117. } else {
  118. attr->currdir = (char *)fspr_pstrdup(attr->pool, dir);
  119. }
  120. if (attr->currdir) {
  121. return APR_SUCCESS;
  122. }
  123. return APR_ENOMEM;
  124. }
  125. APR_DECLARE(fspr_status_t) fspr_procattr_cmdtype_set(fspr_procattr_t *attr,
  126. fspr_cmdtype_e cmd)
  127. {
  128. attr->cmdtype = cmd;
  129. return APR_SUCCESS;
  130. }
  131. APR_DECLARE(fspr_status_t) fspr_procattr_detach_set(fspr_procattr_t *attr, fspr_int32_t detach)
  132. {
  133. attr->detached = detach;
  134. return APR_SUCCESS;
  135. }
  136. APR_DECLARE(fspr_status_t) fspr_proc_fork(fspr_proc_t *proc, fspr_pool_t *pool)
  137. {
  138. int pid;
  139. if ((pid = fork()) < 0) {
  140. return errno;
  141. }
  142. else if (pid == 0) {
  143. /* This is really ugly...
  144. * The semantics of BeOS's fork() are that areas (used for shared
  145. * memory) get COW'd :-( The only way we can make shared memory
  146. * work across fork() is therefore to find any areas that have
  147. * been created and then clone them into our address space.
  148. * Thankfully only COW'd areas have the lock variable set at
  149. * anything but 0, so we can use that to find the areas we need to
  150. * copy. Of course what makes it even worse is that the loop through
  151. * the area's will go into an infinite loop, eating memory and then
  152. * eventually segfault unless we know when we reach then end of the
  153. * "original" areas and stop. Why? Well, we delete the area and then
  154. * add another to the end of the list...
  155. */
  156. area_info ai;
  157. int32 cookie = 0;
  158. area_id highest = 0;
  159. while (get_next_area_info(0, &cookie, &ai) == B_OK)
  160. if (ai.area > highest)
  161. highest = ai.area;
  162. cookie = 0;
  163. while (get_next_area_info(0, &cookie, &ai) == B_OK) {
  164. if (ai.area > highest)
  165. break;
  166. if (ai.lock > 0) {
  167. area_id original = find_area(ai.name);
  168. delete_area(ai.area);
  169. clone_area(ai.name, &ai.address, B_CLONE_ADDRESS,
  170. ai.protection, original);
  171. }
  172. }
  173. proc->pid = pid;
  174. proc->in = NULL;
  175. proc->out = NULL;
  176. proc->err = NULL;
  177. return APR_INCHILD;
  178. }
  179. proc->pid = pid;
  180. proc->in = NULL;
  181. proc->out = NULL;
  182. proc->err = NULL;
  183. return APR_INPARENT;
  184. }
  185. APR_DECLARE(fspr_status_t) fspr_procattr_child_errfn_set(fspr_procattr_t *attr,
  186. fspr_child_errfn_t *errfn)
  187. {
  188. /* won't ever be called on this platform, so don't save the function pointer */
  189. return APR_SUCCESS;
  190. }
  191. APR_DECLARE(fspr_status_t) fspr_procattr_error_check_set(fspr_procattr_t *attr,
  192. fspr_int32_t chk)
  193. {
  194. /* won't ever be used on this platform, so don't save the flag */
  195. return APR_SUCCESS;
  196. }
  197. APR_DECLARE(fspr_status_t) fspr_procattr_addrspace_set(fspr_procattr_t *attr,
  198. fspr_int32_t addrspace)
  199. {
  200. /* won't ever be used on this platform, so don't save the flag */
  201. return APR_SUCCESS;
  202. }
  203. APR_DECLARE(fspr_status_t) fspr_proc_create(fspr_proc_t *new, const char *progname,
  204. const char * const *args,
  205. const char * const *env,
  206. fspr_procattr_t *attr,
  207. fspr_pool_t *pool)
  208. {
  209. int i=0,nargs=0;
  210. char **newargs = NULL;
  211. thread_id newproc, sender;
  212. struct send_pipe *sp;
  213. char * dir = NULL;
  214. sp = (struct send_pipe *)fspr_palloc(pool, sizeof(struct send_pipe));
  215. new->in = attr->parent_in;
  216. new->err = attr->parent_err;
  217. new->out = attr->parent_out;
  218. sp->in = attr->child_in ? attr->child_in->filedes : -1;
  219. sp->out = attr->child_out ? attr->child_out->filedes : -1;
  220. sp->err = attr->child_err ? attr->child_err->filedes : -1;
  221. i = 0;
  222. while (args && args[i]) {
  223. i++;
  224. }
  225. newargs = (char**)malloc(sizeof(char *) * (i + 4));
  226. newargs[0] = strdup("/boot/home/config/bin/fspr_proc_stub");
  227. if (attr->currdir == NULL) {
  228. /* we require the directory , so use a temp. variable */
  229. dir = malloc(sizeof(char) * PATH_MAX);
  230. getcwd(dir, PATH_MAX);
  231. newargs[1] = strdup(dir);
  232. free(dir);
  233. } else {
  234. newargs[1] = strdup(attr->currdir);
  235. }
  236. newargs[2] = strdup(progname);
  237. i=0;nargs = 3;
  238. while (args && args[i]) {
  239. newargs[nargs] = strdup(args[i]);
  240. i++;nargs++;
  241. }
  242. newargs[nargs] = NULL;
  243. /* ### we should be looking at attr->cmdtype in here... */
  244. newproc = load_image(nargs, (const char**)newargs, (const char**)env);
  245. /* load_image copies the data so now we can free it... */
  246. while (--nargs >= 0)
  247. free (newargs[nargs]);
  248. free(newargs);
  249. if (newproc < B_NO_ERROR) {
  250. return errno;
  251. }
  252. resume_thread(newproc);
  253. if (attr->child_in) {
  254. fspr_file_close(attr->child_in);
  255. }
  256. if (attr->child_out) {
  257. fspr_file_close(attr->child_out);
  258. }
  259. if (attr->child_err) {
  260. fspr_file_close(attr->child_err);
  261. }
  262. send_data(newproc, 0, (void*)sp, sizeof(struct send_pipe));
  263. new->pid = newproc;
  264. /* before we go charging on we need the new process to get to a
  265. * certain point. When it gets there it'll let us know and we
  266. * can carry on. */
  267. receive_data(&sender, (void*)NULL,0);
  268. return APR_SUCCESS;
  269. }
  270. APR_DECLARE(fspr_status_t) fspr_proc_wait_all_procs(fspr_proc_t *proc,
  271. int *exitcode,
  272. fspr_exit_why_e *exitwhy,
  273. fspr_wait_how_e waithow,
  274. fspr_pool_t *p)
  275. {
  276. proc->pid = -1;
  277. return fspr_proc_wait(proc, exitcode, exitwhy, waithow);
  278. }
  279. APR_DECLARE(fspr_status_t) fspr_proc_wait(fspr_proc_t *proc,
  280. int *exitcode,
  281. fspr_exit_why_e *exitwhy,
  282. fspr_wait_how_e waithow)
  283. {
  284. pid_t pstatus;
  285. int waitpid_options = WUNTRACED;
  286. int exit_int;
  287. int ignore;
  288. fspr_exit_why_e ignorewhy;
  289. if (exitcode == NULL) {
  290. exitcode = &ignore;
  291. }
  292. if (exitwhy == NULL) {
  293. exitwhy = &ignorewhy;
  294. }
  295. if (waithow != APR_WAIT) {
  296. waitpid_options |= WNOHANG;
  297. }
  298. if ((pstatus = waitpid(proc->pid, &exit_int, waitpid_options)) > 0) {
  299. proc->pid = pstatus;
  300. if (WIFEXITED(exit_int)) {
  301. *exitwhy = APR_PROC_EXIT;
  302. *exitcode = WEXITSTATUS(exit_int);
  303. }
  304. else if (WIFSIGNALED(exit_int)) {
  305. *exitwhy = APR_PROC_SIGNAL;
  306. *exitcode = WTERMSIG(exit_int);
  307. }
  308. else {
  309. /* unexpected condition */
  310. return APR_EGENERAL;
  311. }
  312. return APR_CHILD_DONE;
  313. }
  314. else if (pstatus == 0) {
  315. return APR_CHILD_NOTDONE;
  316. }
  317. return errno;
  318. }
  319. APR_DECLARE(fspr_status_t) fspr_procattr_child_in_set(fspr_procattr_t *attr, fspr_file_t *child_in,
  320. fspr_file_t *parent_in)
  321. {
  322. if (attr->child_in == NULL && attr->parent_in == NULL)
  323. fspr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
  324. if (child_in != NULL)
  325. fspr_file_dup(&attr->child_in, child_in, attr->pool);
  326. if (parent_in != NULL)
  327. fspr_file_dup(&attr->parent_in, parent_in, attr->pool);
  328. return APR_SUCCESS;
  329. }
  330. APR_DECLARE(fspr_status_t) fspr_procattr_child_out_set(fspr_procattr_t *attr, fspr_file_t *child_out,
  331. fspr_file_t *parent_out)
  332. {
  333. if (attr->child_out == NULL && attr->parent_out == NULL)
  334. fspr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
  335. if (child_out != NULL)
  336. fspr_file_dup(&attr->child_out, child_out, attr->pool);
  337. if (parent_out != NULL)
  338. fspr_file_dup(&attr->parent_out, parent_out, attr->pool);
  339. return APR_SUCCESS;
  340. }
  341. APR_DECLARE(fspr_status_t) fspr_procattr_child_err_set(fspr_procattr_t *attr, fspr_file_t *child_err,
  342. fspr_file_t *parent_err)
  343. {
  344. if (attr->child_err == NULL && attr->parent_err == NULL)
  345. fspr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
  346. if (child_err != NULL)
  347. fspr_file_dup(&attr->child_err, child_err, attr->pool);
  348. if (parent_err != NULL)
  349. fspr_file_dup(&attr->parent_err, parent_err, attr->pool);
  350. return APR_SUCCESS;
  351. }
  352. APR_DECLARE(fspr_status_t) fspr_procattr_limit_set(fspr_procattr_t *attr, fspr_int32_t what,
  353. void *limit)
  354. {
  355. return APR_ENOTIMPL;
  356. }
  357. APR_DECLARE(fspr_status_t) fspr_procattr_user_set(fspr_procattr_t *attr,
  358. const char *username,
  359. const char *password)
  360. {
  361. return APR_ENOTIMPL;
  362. }
  363. APR_DECLARE(fspr_status_t) fspr_procattr_group_set(fspr_procattr_t *attr,
  364. const char *groupname)
  365. {
  366. return APR_ENOTIMPL;
  367. }