Debian Codegraph @

xpa/xlaunch.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2007 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xlaunch.h>
6
7 /*
8 *----------------------------------------------------------------------------
9 *
10 *
11 * Private Routines and Data
12 *
13 *
14 *----------------------------------------------------------------------------
15 */
16
17 static pid_t pid=0;
18
19 #if HAVE_MINGW32==0
20
21 /* wait for child process to start, using waitpid() */
22 #ifdef ANSI_FUNC
23 static int launch_pipes(int *pipes, int flag)
24 #else
25 static int launch_pipes(pipes, flag)
26 int *pipes;
27 int flag;
28 #endif
29 {
30 int i;
31 char tbuf[SZ_LINE];
32 if( pipes ){
33 for(i=0; i<4; i++){
34 pipes[i] = -1;
35 }
36 if( (pipe(&pipes[0]) < 0) || (pipe(&pipes[2]) < 0) ) return -1;
37 if( flag ){
38 #if HAVE_SETENV
39 snprintf(tbuf, SZ_LINE-1, "%d,%d,%d,%d",
40 pipes[0], pipes[1], pipes[2], pipes[3]);
41 setenv("LAUNCH_PIPES", tbuf, 1);
42 #else
43 snprintf(tbuf, SZ_LINE-1, "LAUNCH_PIPES=%d,%d,%d,%d",
44 pipes[0], pipes[1], pipes[2], pipes[3]);
45 putenv(xstrdup(tbuf));
46 #endif
47 }
48 }
49 return 0;
50 }
51
52 #ifdef ANSI_FUNC
53 static int cleanup_pipes(int *pipes)
54 #else
55 static int cleanup_pipes(pipes)
56 int *pipes;
57 #endif
58 {
59 if( pipes ){
60 /* close child pipes */
61 close(pipes[1]);
62 close(pipes[2]);
63 /* move parent write into slot 1 */
64 pipes[1] = pipes[3];
65 /* set unused pipes to impossible value */
66 pipes[2] = -1;
67 pipes[3] = -1;
68 }
69 return 0;
70 }
71
72 #if LAUNCH_USE_WAITPID
73 /* wait for child process to start, using waitpid() */
74 #ifdef ANSI_FUNC
75 static int launch_waitstart(pid_t pid)
76 #else
77 static int launch_waitstart(pid)
78 pid_t pid;
79 #endif
80 {
81 int i, got;
82 int status=0;
83 struct timeval tv;
84 /* wait up to LAUNCH_WAIT_TRIES millisec to make sure the child started,
85 but if we get an error, we can exit immediately */
86 for(i=0; i<LAUNCH_WAIT_TRIES; i++){
87 errno = 0;
88 got=waitpid(pid, &status, WNOHANG);
89 /* look for error termination */
90 if( (got < 0) || ((got == 0) && xerrno) ){
91 got = -1;
92 /* make sure status shows error */
93 if( status == 0 )
94 status = -1;
95 break;
96 }
97 /* look for normal termination */
98 else if( got > 0 ){
99 break;
100 }
101 /* no termination, sleep and wait some more */
102 else{
103 tv.tv_sec = 0;
104 tv.tv_usec = LAUNCH_WAIT_MSEC;
105 xselect(1, NULL, NULL, NULL, &tv);
106 }
107 }
108 /* no termination means the child is still running */
109 if( got == 0 ) status = 0;
110 /* return the news */
111 return status;
112 }
113 #endif
114
115 /*
116 * standard unix version of launch:
117 * adapted from the system() code in:
118 * W. Richard Stevens
119 * "Advanced Programming in the Unix Environment"
120 * Addison-Wesley Publishing Co, 1992
121 * p. 314
122 */
123 #ifdef ANSI_FUNC
124 static int launch_fork_exec(char *cmdstring, int attach,
125 char **stdfiles, int *pipes)
126 #else
127 static int launch_fork_exec(cmdstring, attach, stdfiles, pipes)
128 char *cmdstring;
129 int attach;
130 char **stdfiles;
131 int *pipes;
132 #endif
133 {
134 int status;
135 int tpipes[4];
136 struct sigaction ignore, saveintr, savequit;
137 sigset_t chldmask, savemask;
138 #if LAUNCH_USE_PIPE
139 int fd[2];
140 #endif
141
142 /* return false if no command is specified */
143 if( !cmdstring || !*cmdstring ) return -1;
144
145 ignore.sa_handler = SIG_IGN; /* ignore SIGINT and SIGQUIT */
146 sigemptyset(&ignore.sa_mask);
147 ignore.sa_flags = 0;
148 if (sigaction(SIGINT, &ignore, &saveintr) < 0)
149 return -1;
150 if (sigaction(SIGQUIT, &ignore, &savequit) < 0)
151 return -1;
152
153 sigemptyset(&chldmask); /* now block SIGCHLD */
154 sigaddset(&chldmask, SIGCHLD);
155 if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0)
156 return -1;
157
158 #if LAUNCH_USE_PIPE
159 /* open a pipe so parent can hear if the child fails to exec */
160 if( !attach ){
161 if( pipe(fd) < 0 )
162 return -1;
163 xfcntl(fd[0], F_SETFD, FD_CLOEXEC);
164 xfcntl(fd[1], F_SETFD, FD_CLOEXEC);
165 }
166 #endif
167
168 /* create temp ipc pipes if necessary */
169 if( pipes ){
170 if( launch_pipes(tpipes, 0) < 0 ) return -1;
171 }
172
173 /* start new process */
174 if( (pid = fork()) < 0 ){
175 #if LAUNCH_USE_PIPE
176 if( !attach ){
177 close(fd[0]);
178 close(fd[1]);
179 }
180 #endif
181 if( pipes ){
182 close(tpipes[0]);
183 close(tpipes[1]);
184 close(tpipes[2]);
185 close(tpipes[3]);
186 }
187 status = -1; /* ERROR: probably out of processes */
188 } else if( pid == 0 ){ /* child */
189 int i, j, len;
190 char *argv[LAUNCH_ARGS+1];
191 char *path=NULL;
192 char *s=NULL, *t=NULL;
193
194 /* reset pipes, if necessary */
195 if( pipes ){
196 /* close parent's read/write pipes */
197 close(tpipes[0]);
198 close(tpipes[3]);
199 /* change child's stdin/stdout to use the passed pipes to parent */
200 dup2(tpipes[2], 0); close(tpipes[2]);
201 dup2(tpipes[1], 1); close(tpipes[1]);
202 }
203
204 /* close and reopen stdio files, if necessary */
205 if( stdfiles ){
206 for(i=0; i<3; i++){
207 if( stdfiles[i] ){
208 close(i);
209 switch(i){
210 case 0:
211 if( open(stdfiles[i], O_RDONLY) < 0){
212 _exit(-1);
213 }
214 break;
215 case 1:
216 if( open(stdfiles[i], O_CREAT|O_WRONLY|O_TRUNC, 0600) < 0){
217 _exit(-1);
218 }
219 break;
220 case 2:
221 /* if stderr is the same as stdout, just dup */
222 if( stdfiles[1] && !strcmp(stdfiles[1], stdfiles[i]) ){
223 dup(1);
224 }
225 else{
226 if( open(stdfiles[i], O_CREAT|O_WRONLY|O_TRUNC, 0600) < 0){
227 _exit(-1);
228 }
229 }
230 break;
231 }
232 }
233 }
234 }
235
236 /* restore previous signal actions & reset signal mask, but only if
237 parent is waiting for completion (i.e., we are "attached") */
238 if( attach ){
239 sigaction(SIGINT, &saveintr, NULL);
240 sigaction(SIGQUIT, &savequit, NULL);
241 sigprocmask(SIG_SETMASK, &savemask, NULL);
242 }
243 #if LAUNCH_USE_PIPE
244 /* child closes reader -- only writes status */
245 else{
246 close(fd[0]);
247 }
248 #endif
249
250 /* package up the arguments for new process */
251 t = (char *)xstrdup(cmdstring);
252 for(i=0, s=(char *)strtok(t, " \t"); s;
253 i++, s=(char *)strtok(NULL," \t")){
254 if( i < LAUNCH_ARGS ){
255 /* save argument */
256 argv[i] = xstrdup(s);
257 /* change back special char to spaces, if necessary */
258 len = strlen(argv[i]);
259 for(j=0; j<len; j++){
260 if( argv[i][j] == LAUNCH_SPACE){
261 argv[i][j] = ' ';
262 }
263 }
264 argv[i+1] = NULL;
265 /* save program name */
266 if( i == 0 ) path = argv[i];
267 }
268 }
269 if( t ) xfree(t);
270 #ifndef HAVE_CYGWIN
271 /* this call is broken in cygwin */
272 /* for unattached processes, start a new session (with new process id),
273 so that we do not inherit signals from parent (particularly SIGTERM) */
274 if( !attach )
275 setsid();
276 #endif
277 /* start up the new program */
278 if( execvp(path, argv) ){
279 status = 127;
280 #if LAUNCH_USE_PIPE
281 if( !attach ){
282 write(fd[1], &status, 4);
283 close(fd[1]);
284 }
285 #endif
286 _exit(status); /* exec error */
287 }
288 } else { /* parent */
289 /* wait for program termination from attached process */
290 if( attach ){
291 while( waitpid(pid, &status, 0) < 0 ){
292 if( xerrno != EINTR ){
293 status = -1; /* error other than EINTR from waitpid() */
294 break;
295 }
296 }
297 }
298 else{
299 #if LAUNCH_USE_WAITPID
300 status = launch_waitstart(pid);
301 #endif
302 #if LAUNCH_USE_PIPE
303 close(fd[1]);
304 if( read(fd[0], &status, 4) == 0 ){
305 status = 0;
306 }
307 close(fd[0]);
308 #endif
309 }
310 }
311
312 /* cleanup temp ipc pipes and move into user space */
313 if( pipes ){
314 cleanup_pipes(tpipes);
315 pipes[0] = tpipes[0];
316 pipes[1] = tpipes[1];
317 }
318
319 /* restore previous signal actions & reset signal mask */
320 if( sigaction(SIGINT, &saveintr, NULL) < 0 ) return -1;
321 if( sigaction(SIGQUIT, &savequit, NULL) < 0 ) return -1;
322 if( sigprocmask(SIG_SETMASK, &savemask, NULL) < 0 ) return -1;
323
324 /* return the news */
325 return status;
326 }
327
328 #endif
329
330 #if HAVE_POSIX_SPAWN
331
332 #if defined(HAVE__NSGETENVIRON) && defined(HAVE_CRT_EXTERNS_H)
333 #include <crt_externs.h>
334 #define environ (*_NSGetEnviron())
335 #else
336 extern char **environ;
337 #endif
338
339 /* spawn calls POSIX posix_spawn */
340 #ifdef ANSI_FUNC
341 static int launch_posix_spawn(char *cmdstring, int attach,
342 char **stdfiles, int *pipes)
343 #else
344 static int launch_posix_spawn(cmdstring, attach, stdfiles, pipes)
345 char *cmdstring;
346 int attach;
347 char **stdfiles;
348 int *pipes;
349 #endif
350 {
351 int i, j, len;
352 int status=0;
353 int got=0;
354 int tpipes[4];
355 char *argv[LAUNCH_ARGS+1];
356 char *path=NULL;
357 char *s=NULL, *t=NULL;
358 posix_spawn_file_actions_t act;
359 posix_spawn_file_actions_t *pact=NULL;
360
361 /* return false if no command is specified */
362 if( !cmdstring || !*cmdstring )
363 return -1;
364
365 /* create temp ipc pipes if necessary */
366 if( pipes ){
367 if( launch_pipes(tpipes, 1) < 0 ) return -1;
368 }
369
370 /* package up the arguments for new process */
371 t = (char *)xstrdup(cmdstring);
372 for(i=0, s=(char *)strtok(t, " \t"); s;
373 i++, s=(char *)strtok(NULL," \t")){
374 if( i < LAUNCH_ARGS ){
375 /* save argument */
376 argv[i] = xstrdup(s);
377 /* change back special char to spaces, if necessary */
378 len = strlen(argv[i]);
379 for(j=0; j<len; j++){
380 if( argv[i][j] == LAUNCH_SPACE){
381 argv[i][j] = ' ';
382 }
383 }
384 /* last arg is always a NULL */
385 argv[i+1] = NULL;
386 /* save program name */
387 if( i == 0 ) path = argv[i];
388 /* inc arg count */
389 got++;
390 }
391 }
392 if( t ) xfree(t);
393 /* arrange stdfiles files, if necessary */
394 if( stdfiles ){
395 if( posix_spawn_file_actions_init(&act) != 0)
396 return -1;
397 /* stdin */
398 if(stdfiles[0] &&
399 posix_spawn_file_actions_addopen(&act, 0, stdfiles[0], O_RDONLY, 0))
400 return -1;
401 /* stdout */
402 if(stdfiles[1] &&
403 posix_spawn_file_actions_addopen(&act, 1, stdfiles[1], O_CREAT|O_WRONLY|O_TRUNC, 0600))
404 return -1;
405 /* stderr */
406 if(stdfiles[2] &&
407 posix_spawn_file_actions_addopen(&act, 2, stdfiles[2], O_CREAT|O_WRONLY|O_TRUNC, 0600))
408 return -1;
409 pact = &act;
410 }
411 /* start the new process */
412 if( (status = posix_spawnp(&pid, path, pact, NULL, argv, environ)) )
413 return status;
414 /* wait for program termination from attached process */
415 if( attach ){
416 while( waitpid(pid, &status, 0) < 0 ){
417 if( xerrno != EINTR ){
418 status = -1; /* error other than EINTR from waitpid() */
419 break;
420 }
421 }
422 }
423 #if BIG_DELAY_WHEN_USING_THIS
424 /* wait for child process to start */
425 else{
426 status = launch_waitstart(pid);
427 }
428 #endif
429 /* clean up */
430 if( stdfiles ) posix_spawn_file_actions_destroy(&act);
431 /* cleanup temp ipc pipes and move into user space */
432 if( pipes ){
433 cleanup_pipes(tpipes);
434 pipes[0] = tpipes[0];
435 pipes[1] = tpipes[1];
436 }
437 for(i=0; i<got; i++){
438 if( argv[i] ) xfree((char *)argv[i]);
439 }
440 /* return status */
441 return status;
442 }
443
444 #endif
445
446 #if HAVE_SPAWNVP
447
448 #ifdef ANSI_FUNC
449 static int launch_spawnvp(char *cmdstring, int attach,
450 char **stdfiles, int *pipes)
451 #else
452 static int launch_spawnvp(cmdstring, attach, stdfiles, pipes)
453 char *cmdstring;
454 int attach;
455 char **stdfiles;
456 int *pipes;
457 #endif
458 {
459 int i, j;
460 int len;
461 int got;
462 int status;
463 int tpipes[4];
464 char *argv[LAUNCH_ARGS+1];
465 char *path=NULL;
466 char *s=NULL, *t=NULL;
467 struct timeval tv;
468
469 /* return false if no command is specified */
470 if( !cmdstring || !*cmdstring ) return -1;
471
472 /* for now, we can't support stdfiles */
473 if( stdfiles ) return -1;
474
475 /* create temp ipc pipes if necessary */
476 if( pipes ){
477 #if HAVE_MINGW32==0
478 if( launch_pipes(tpipes, 1) < 0 ) return -1;
479 #else
480 fprintf(stderr, "ERROR: launch_pipes() not available in mingw\n");
481 exit(1);
482 #endif
483 }
484
485
486 /* package up the arguments for new process */
487 t = (char *)xstrdup(cmdstring);
488 for(i=0, got=0, s=(char *)strtok(t, " \t"); s;
489 i++, s=(char *)strtok(NULL," \t")){
490 if( i < LAUNCH_ARGS ){
491 /* save argument */
492 argv[i] = xstrdup(s);
493 /* change back special char to spaces, if necessary */
494 len = strlen(argv[i]);
495 for(j=0; j<len; j++){
496 if( argv[i][j] == LAUNCH_SPACE){
497 argv[i][j] = ' ';
498 }
499 }
500 /* last arg is always a NULL */
501 argv[i+1] = NULL;
502 /* save program name */
503 if( i == 0 ) path = (char *)argv[i];
504 /* inc arg count */
505 got++;
506 }
507 }
508 if( t ) xfree(t);
509 if( attach )
510 i = _P_WAIT;
511 else
512 i = _P_NOWAIT;
513 if((status = spawnvp(i, path, (void *)argv)) != -1){
514 status = 0;
515 /* wait for child to start */
516 tv.tv_sec = 0;
517 tv.tv_usec = LAUNCH_WAIT_MSEC;
518 xselect(1, NULL, NULL, NULL, &tv);
519 }
520 /* clean up */
521 for(i=0; i<got; i++){
522 if( argv[i] ) xfree((char *)argv[i]);
523 }
524 /* cleanup temp ipc pipes and move into user space */
525 if( pipes ){
526 #if HAVE_MINGW32==0
527 cleanup_pipes(tpipes);
528 pipes[0] = tpipes[0];
529 pipes[1] = tpipes[1];
530 #else
531 fprintf(stderr, "ERROR: launch_pipes() not available in mingw\n");
532 exit(1);
533 #endif
534 }
535 return status;
536 }
537
538 #endif
539
540 /*
541 *----------------------------------------------------------------------------
542 *
543 *
544 * Public Routines and Data
545 *
546 *
547 *----------------------------------------------------------------------------
548 */
549
550 /*
551 *
552 * LaunchPid() -- return pid of last launched process
553 *
554 */
555 #ifdef ANSI_FUNC
556 pid_t LaunchPid(void)
557 #else
558 pid_t LaunchPid()
559 #endif
560 {
561 return pid;
562 }
563
564 #ifdef ANSI_FUNC
565 int Launch(char *cmdstring, int attach, char **stdfiles, int *pipes)
566 #else
567 int Launch(cmdstring, attach, stdfiles, piles)
568 char *cmdstring;
569 int attach;
570 char **stdfiles;
571 int *pipes;
572 #endif
573 {
574 static int which_launch=0;
575 static int which_debug=0;
576 char *s=NULL;
577
578 /* return false if no command is specified */
579 if( !cmdstring || !*cmdstring ) return -1;
580
581 /* sanity check: don't specify stdfiles and pipes simultaneously */
582 if( stdfiles && pipes ){
583 fprintf(stderr,
584 "ERROR: stdfiles and pipes are mutually exclusive in Launch()\n");
585 return -1;
586 }
587
588 /* if pipes are specified, we don't attach */
589 if( pipes ) attach = 0;
590
591 /* determine launch method */
592 if( !which_launch ){
593 which_launch = LAUNCH_DEFAULT_WHICH;
594 if( (s=getenv("LAUNCH_ROUTINE")) ){
595 /* fork_exec */
596 if( !strncasecmp(s, "f", 1) ){
597 which_launch = 1;
598 if( *s == 'F' ) which_debug = 1;
599 }
600 /* posix_spawn */
601 else if( !strncasecmp(s, "p", 1) ){
602 which_launch = 2;
603 if( *s == 'P' ) which_debug = 1;
604 }
605 /* spawnvp */
606 else if( !strncasecmp(s, "s", 1) ){
607 which_launch = 3;
608 if( *s == 'S' ) which_debug = 1;
609 }
610 else if( *s == 'V' ) {
611 which_debug = 1;
612 }
613 }
614 }
615 /* call the correct launch method */
616 switch(which_launch){
617 case 1:
618 #if HAVE_MINGW32==0
619 if( which_debug ) fprintf(stderr, "launch_fork_exec: %s\n", cmdstring);
620 return launch_fork_exec(cmdstring, attach, stdfiles, pipes);
621 #else
622 fprintf(stderr, "ERROR: fork_exec() not available on this host\n");
623 exit(1);
624 #endif
625 break;
626 case 2:
627 #if HAVE_POSIX_SPAWN
628 if( which_debug ) fprintf(stderr, "launch_posix_spawn: %s\n", cmdstring);
629 return launch_posix_spawn(cmdstring, attach, stdfiles, pipes);
630 #else
631 fprintf(stderr, "ERROR: posix_spawn() not available on this host\n");
632 exit(1);
633 #endif
634 break;
635 case 3:
636 #if HAVE_SPAWNVP
637 if( which_debug ) fprintf(stderr, "launch_spawnvp: %s\n", cmdstring);
638 return launch_spawnvp(cmdstring, attach, stdfiles, pipes);
639 #else
640 fprintf(stderr, "ERROR: spawnvp() not available on this host\n");
641 exit(1);
642 #endif
643 break;
644 default:
645 #if HAVE_MINGW32==0
646 if( which_debug ) fprintf(stderr, "launch_fork_exec: %s\n", cmdstring);
647 return launch_fork_exec(cmdstring, attach, stdfiles, pipes);
648 #else
649 fprintf(stderr, "ERROR: no launch techniques available on this host\n");
650 exit(1);
651 #endif
652 break;
653 }
654 /* can't happen */
655 return -1;
656 }
657