summaryrefslogtreecommitdiffstats
path: root/xlaunch.c
blob: 9f064d18b2d6300eebeec104945b359d66812cb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
 *	Copyright (c) 1999-2007 Smithsonian Astrophysical Observatory
 */

#include <xlaunch.h>

/*
 *----------------------------------------------------------------------------
 *
 *
 * 			Private Routines and Data
 *
 *
 *----------------------------------------------------------------------------
 */

static pid_t pid=0;

#if HAVE_MINGW32==0

/* wait for child process to start, using waitpid() */
#ifdef ANSI_FUNC
static int launch_pipes(int *pipes, int flag)
#else
static int launch_pipes(pipes, flag)
     int *pipes;
     int flag;
#endif
{
  int i;
  char tbuf[SZ_LINE];
  if( pipes ){
    for(i=0; i<4; i++){
      pipes[i] = -1;
    }
    if( (pipe(&pipes[0]) < 0) || (pipe(&pipes[2]) < 0) ) return -1;
    if( flag ){
#if HAVE_SETENV
      snprintf(tbuf, SZ_LINE-1, "%d,%d,%d,%d", 
	       pipes[0], pipes[1], pipes[2], pipes[3]);
      setenv("LAUNCH_PIPES", tbuf, 1);
#else
      snprintf(tbuf, SZ_LINE-1, "LAUNCH_PIPES=%d,%d,%d,%d", 
	       pipes[0], pipes[1], pipes[2], pipes[3]);
      putenv(xstrdup(tbuf));
#endif
    }
  }
  return 0;
}

#ifdef ANSI_FUNC
static int cleanup_pipes(int *pipes)
#else
static int cleanup_pipes(pipes)
     int *pipes;
#endif
{
  if( pipes ){
    /* close child pipes */
    close(pipes[1]);
    close(pipes[2]);
    /* move parent write into slot 1 */
    pipes[1] = pipes[3];
    /* set unused pipes to impossible value */
    pipes[2] = -1;
    pipes[3] = -1;
  }
  return 0;
}

#if LAUNCH_USE_WAITPID
/* wait for child process to start, using waitpid() */
#ifdef ANSI_FUNC
static int launch_waitstart(pid_t pid)
#else
static int launch_waitstart(pid)
     pid_t pid;
#endif
{
  int i, got;
  int status=0;
  struct timeval tv;
  /* wait up to LAUNCH_WAIT_TRIES millisec to make sure the child started,
     but if we get an error, we can exit immediately */
  for(i=0; i<LAUNCH_WAIT_TRIES; i++){
    errno = 0;
    got=waitpid(pid, &status, WNOHANG);
    /* look for error termination */
    if( (got < 0) || ((got == 0) && xerrno) ){
      got = -1;
      /* make sure status shows error */
      if( status == 0 )
	status = -1;
      break;
    }
    /* look for normal termination */
    else if( got > 0 ){
      break;
    }
    /* no termination, sleep and wait some more */
    else{
      tv.tv_sec = 0;
      tv.tv_usec = LAUNCH_WAIT_MSEC;
      xselect(1, NULL, NULL, NULL, &tv);
    }
  }
  /* no termination means the child is still running */
  if( got == 0 ) status = 0;
  /* return the news */
  return status;
}
#endif

/* 
 *  standard unix version of launch:
 *  adapted from the system() code in:
 *  W. Richard Stevens
 *  "Advanced Programming in the Unix Environment" 
 *  Addison-Wesley Publishing Co, 1992
 *  p. 314
 */
#ifdef ANSI_FUNC
static int launch_fork_exec(char *cmdstring, int attach, 
			    char **stdfiles, int *pipes)
#else
  static int launch_fork_exec(cmdstring, attach, stdfiles, pipes)
     char *cmdstring;
     int attach;
     char **stdfiles;
     int *pipes;
#endif
{
  int status;
  int tpipes[4];
  struct sigaction ignore, saveintr, savequit;
  sigset_t chldmask, savemask;
#if LAUNCH_USE_PIPE
  int fd[2];
#endif

  /* return false if no command is specified */
  if( !cmdstring || !*cmdstring ) return -1;

  ignore.sa_handler = SIG_IGN;	/* ignore SIGINT and SIGQUIT */
  sigemptyset(&ignore.sa_mask);
  ignore.sa_flags = 0;
  if (sigaction(SIGINT, &ignore, &saveintr) < 0)
    return -1;
  if (sigaction(SIGQUIT, &ignore, &savequit) < 0)
    return -1;
  
  sigemptyset(&chldmask);	/* now block SIGCHLD */
  sigaddset(&chldmask, SIGCHLD);
  if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0)
    return -1;
  
#if LAUNCH_USE_PIPE
  /* open a pipe so parent can hear if the child fails to exec */
  if( !attach ){
    if( pipe(fd) < 0 )
      return -1;
    xfcntl(fd[0], F_SETFD, FD_CLOEXEC);
    xfcntl(fd[1], F_SETFD, FD_CLOEXEC);
  }
#endif

  /* create temp ipc pipes if necessary */
  if( pipes ){
    if( launch_pipes(tpipes, 0) < 0 ) return -1;
  }

  /* start new process */
  if( (pid = fork()) < 0 ){
#if LAUNCH_USE_PIPE
    if( !attach ){
      close(fd[0]);
      close(fd[1]);
    }
#endif
    if( pipes ){
      close(tpipes[0]);
      close(tpipes[1]);
      close(tpipes[2]);
      close(tpipes[3]);
    }
    status = -1;		/* ERROR: probably out of processes */
  } else if( pid == 0 ){	/* child */
    int i, j, len;
    char *argv[LAUNCH_ARGS+1];
    char *path=NULL;
    char *s=NULL, *t=NULL;

    /* reset pipes, if necessary */
    if( pipes ){
      /* close parent's read/write pipes */
      close(tpipes[0]);
      close(tpipes[3]);
      /* change child's stdin/stdout to use the passed pipes to parent */
      dup2(tpipes[2], 0);  close(tpipes[2]);
      dup2(tpipes[1], 1);  close(tpipes[1]);
    }

    /* close and reopen stdio files, if necessary */
    if( stdfiles ){
      for(i=0; i<3; i++){
	if( stdfiles[i] ){
	  close(i);
	  switch(i){
	  case 0:
	    if( open(stdfiles[i], O_RDONLY) < 0){
	      _exit(-1);
	    }
	    break;
	  case 1:
	    if( open(stdfiles[i], O_CREAT|O_WRONLY|O_TRUNC, 0600) < 0){
	      _exit(-1);
	    }
	    break;
	  case 2:
	    /* if stderr is the same as stdout, just dup */
	    if( stdfiles[1] && !strcmp(stdfiles[1], stdfiles[i]) ){
	      dup(1);
	    }
	    else{
	      if( open(stdfiles[i], O_CREAT|O_WRONLY|O_TRUNC, 0600) < 0){
		_exit(-1);
	      }
	    }
	    break;
	  }
	}
      }
    }

    /* restore previous signal actions & reset signal mask, but only if
       parent is waiting for completion (i.e., we are "attached") */
    if( attach ){
      sigaction(SIGINT, &saveintr, NULL);
      sigaction(SIGQUIT, &savequit, NULL);
      sigprocmask(SIG_SETMASK, &savemask, NULL);
    }
#if LAUNCH_USE_PIPE
    /* child closes reader -- only writes status */
    else{
      close(fd[0]);
    }
#endif

    /* package up the arguments for new process */
    t = (char *)xstrdup(cmdstring);
    for(i=0, s=(char *)strtok(t, " \t"); s;
	i++, s=(char *)strtok(NULL," \t")){
      if( i < LAUNCH_ARGS ){ 
	/* save argument */
	argv[i] = xstrdup(s);
	/* change back special char to spaces, if necessary */
	len = strlen(argv[i]);
	for(j=0; j<len; j++){
	  if( argv[i][j] == LAUNCH_SPACE){
	    argv[i][j] = ' ';
	  }
	}
	argv[i+1] = NULL;
	/* save program name */
	if( i == 0 ) path = argv[i];
      }
    }
    if( t ) xfree(t);
#ifndef HAVE_CYGWIN
    /* this call is broken in cygwin */
    /* for unattached processes, start a new session (with new process id),
       so that we do not inherit signals from parent (particularly SIGTERM) */
    if( !attach )
      setsid();
#endif
    /* start up the new program */
    if( execvp(path, argv) ){
      status = 127;
#if LAUNCH_USE_PIPE
      if( !attach ){
	write(fd[1], &status, 4);
	close(fd[1]);
      }
#endif
      _exit(status);		/* exec error */
    }
  } else {			/* parent */
    /* wait for program termination from attached process */
    if( attach ){
      while( waitpid(pid, &status, 0) < 0 ){
	if( xerrno != EINTR ){
	  status = -1; /* error other than EINTR from waitpid() */
	  break;
	}
      }
    }
    else{
#if LAUNCH_USE_WAITPID
      status = launch_waitstart(pid);
#endif
#if LAUNCH_USE_PIPE
      close(fd[1]);
      if( read(fd[0], &status, 4) == 0 ){
	status = 0;
      }
      close(fd[0]);
#endif
    }
  }
  
  /* cleanup temp ipc pipes and move into user space */
  if( pipes ){
    cleanup_pipes(tpipes);
    pipes[0] = tpipes[0];
    pipes[1] = tpipes[1];
  }

  /* restore previous signal actions & reset signal mask */
  if( sigaction(SIGINT, &saveintr, NULL) < 0 )        return -1;
  if( sigaction(SIGQUIT, &savequit, NULL) < 0 )       return -1;
  if( sigprocmask(SIG_SETMASK, &savemask, NULL) < 0 ) return -1;
  
  /* return the news */
  return status;
}

#endif

#if HAVE_POSIX_SPAWN

#if defined(HAVE__NSGETENVIRON) && defined(HAVE_CRT_EXTERNS_H)
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#else
extern char **environ;
#endif

/* spawn calls POSIX posix_spawn */
#ifdef ANSI_FUNC
static int launch_posix_spawn(char *cmdstring, int attach, 
			      char **stdfiles, int *pipes)
#else
  static int launch_posix_spawn(cmdstring, attach, stdfiles, pipes)
     char *cmdstring;
     int attach;
     char **stdfiles;
     int *pipes;
#endif
{
  int i, j, len;
  int status=0;
  int got=0;
  int tpipes[4];
  char *argv[LAUNCH_ARGS+1];
  char *path=NULL;
  char *s=NULL, *t=NULL;
  posix_spawn_file_actions_t act;
  posix_spawn_file_actions_t *pact=NULL;

  /* return false if no command is specified */
  if( !cmdstring || !*cmdstring )
    return -1;

  /* create temp ipc pipes if necessary */
  if( pipes ){
    if( launch_pipes(tpipes, 1) < 0 ) return -1;
  }

  /* package up the arguments for new process */
  t = (char *)xstrdup(cmdstring);
  for(i=0, s=(char *)strtok(t, " \t"); s;
      i++, s=(char *)strtok(NULL," \t")){
    if( i < LAUNCH_ARGS ){ 
      /* save argument */
      argv[i] = xstrdup(s);
      /* change back special char to spaces, if necessary */
      len = strlen(argv[i]);
      for(j=0; j<len; j++){
	if( argv[i][j] == LAUNCH_SPACE){
	  argv[i][j] = ' ';
	}
      }
      /* last arg is always a NULL */
      argv[i+1] = NULL;
      /* save program name */
      if( i == 0 ) path = argv[i];
      /* inc arg count */
      got++;
    }
  }
  if( t ) xfree(t);
  /* arrange stdfiles files, if necessary */
  if( stdfiles ){
    if( posix_spawn_file_actions_init(&act) != 0) 
      return -1;
    /* stdin */
    if(stdfiles[0] && 
       posix_spawn_file_actions_addopen(&act, 0, stdfiles[0], O_RDONLY, 0))
      return -1;
    /* stdout */
    if(stdfiles[1] && 
       posix_spawn_file_actions_addopen(&act, 1, stdfiles[1], O_CREAT|O_WRONLY|O_TRUNC, 0600))
      return -1;
    /* stderr */
    if(stdfiles[2] && 
       posix_spawn_file_actions_addopen(&act, 2, stdfiles[2], O_CREAT|O_WRONLY|O_TRUNC, 0600))
      return -1;
    pact = &act;
  }
  /* start the new process */
  if( (status = posix_spawnp(&pid, path, pact, NULL, argv, environ)) )
    return status;
  /* wait for program termination from attached process */
  if( attach ){
    while( waitpid(pid, &status, 0) < 0 ){
      if( xerrno != EINTR ){
	status = -1; /* error other than EINTR from waitpid() */
	break;
      }
    }
  }
#if BIG_DELAY_WHEN_USING_THIS
  /* wait for child process to start */
  else{
    status = launch_waitstart(pid);
  }
#endif
  /* clean up */
  if( stdfiles ) posix_spawn_file_actions_destroy(&act);
  /* cleanup temp ipc pipes and move into user space */
  if( pipes ){
    cleanup_pipes(tpipes);
    pipes[0] = tpipes[0];
    pipes[1] = tpipes[1];
  }
  for(i=0; i<got; i++){
    if( argv[i] ) xfree((char *)argv[i]);
  }
  /* return status */
  return status;
}

#endif

#if HAVE_SPAWNVP

#ifdef ANSI_FUNC
static int launch_spawnvp(char *cmdstring, int attach, 
			  char **stdfiles, int *pipes)
#else
  static int launch_spawnvp(cmdstring, attach, stdfiles, pipes)
     char *cmdstring;
     int attach;
     char **stdfiles;
     int *pipes;
#endif
{
  int i, j;
  int len;
  int got;
  int status;
  int tpipes[4];
  char *argv[LAUNCH_ARGS+1];
  char *path=NULL;
  char *s=NULL, *t=NULL;
  struct timeval tv;

  /* return false if no command is specified */
  if( !cmdstring || !*cmdstring ) return -1;

  /* for now, we can't support stdfiles */
  if( stdfiles ) return -1;

  /* create temp ipc pipes if necessary */
  if( pipes ){
#if HAVE_MINGW32==0
    if( launch_pipes(tpipes, 1) < 0 ) return -1;
#else
    fprintf(stderr, "ERROR: launch_pipes() not available in mingw\n");
    exit(1);
#endif
  }


  /* package up the arguments for new process */
  t = (char *)xstrdup(cmdstring);
  for(i=0, got=0, s=(char *)strtok(t, " \t"); s;
      i++, s=(char *)strtok(NULL," \t")){
    if( i < LAUNCH_ARGS ){ 
      /* save argument */
      argv[i] = xstrdup(s);
      /* change back special char to spaces, if necessary */
      len = strlen(argv[i]);
      for(j=0; j<len; j++){
	if( argv[i][j] == LAUNCH_SPACE){
	  argv[i][j] = ' ';
	}
      }
      /* last arg is always a NULL */
      argv[i+1] = NULL;
      /* save program name */
      if( i == 0 ) path = (char *)argv[i];
      /* inc arg count */
      got++;
    }
  }
  if( t ) xfree(t);
  if( attach )
    i = _P_WAIT;
  else
    i = _P_NOWAIT;
  if((status = spawnvp(i, path, (void *)argv)) != -1){
    status = 0;
    /* wait for child to start */
    tv.tv_sec = 0;
    tv.tv_usec = LAUNCH_WAIT_MSEC;
    xselect(1, NULL, NULL, NULL, &tv);
  }
  /* clean up */
  for(i=0; i<got; i++){
    if( argv[i] ) xfree((char *)argv[i]);
  }
  /* cleanup temp ipc pipes and move into user space */
  if( pipes ){
#if HAVE_MINGW32==0
    cleanup_pipes(tpipes);
    pipes[0] = tpipes[0];
    pipes[1] = tpipes[1];
#else
    fprintf(stderr, "ERROR: launch_pipes() not available in mingw\n");
    exit(1);
#endif
  }
  return status;
}

#endif

/*
 *----------------------------------------------------------------------------
 *
 *
 * 			Public Routines and Data
 *
 *
 *----------------------------------------------------------------------------
 */

/*
 *
 * LaunchPid() -- return pid of last  launched process 
 *
 */
#ifdef ANSI_FUNC
pid_t LaunchPid(void)
#else
pid_t LaunchPid()
#endif
{
  return pid;
}

#ifdef ANSI_FUNC
int Launch(char *cmdstring, int attach, char **stdfiles, int *pipes)
#else
int Launch(cmdstring, attach, stdfiles, piles)
     char *cmdstring;
     int attach;
     char **stdfiles;
     int *pipes;
#endif
{
  static int which_launch=0;
  static int which_debug=0;
  char *s=NULL;

  /* return false if no command is specified */
  if( !cmdstring || !*cmdstring ) return -1;

  /* sanity check: don't specify stdfiles and pipes simultaneously */
  if( stdfiles && pipes ){
    fprintf(stderr, 
	    "ERROR: stdfiles and pipes are mutually exclusive in Launch()\n");
    return -1;
  }

  /* if pipes are specified, we don't attach */
  if( pipes ) attach = 0;

  /* determine launch method */
  if( !which_launch ){
    which_launch = LAUNCH_DEFAULT_WHICH;
    if( (s=getenv("LAUNCH_ROUTINE")) ){
      /* fork_exec */
      if( !strncasecmp(s, "f", 1) ){
	which_launch = 1;
	if( *s == 'F' ) which_debug = 1;
      }
      /* posix_spawn */
      else if( !strncasecmp(s, "p", 1) ){
	which_launch = 2;
	if( *s == 'P' ) which_debug = 1;
      }
      /* spawnvp */
      else if( !strncasecmp(s, "s", 1) ){
	which_launch = 3;
	if( *s == 'S' ) which_debug = 1;
      }
      else if( *s == 'V' ) {
	which_debug = 1;
      }
    }
  }
  /* call the correct launch method */
  switch(which_launch){
  case 1:
#if HAVE_MINGW32==0
    if( which_debug ) fprintf(stderr, "launch_fork_exec: %s\n", cmdstring);
    return launch_fork_exec(cmdstring, attach, stdfiles, pipes);
#else
    fprintf(stderr, "ERROR: fork_exec() not available on this host\n");
    exit(1);
#endif
    break;
  case 2:
#if HAVE_POSIX_SPAWN
    if( which_debug ) fprintf(stderr, "launch_posix_spawn: %s\n", cmdstring);
    return launch_posix_spawn(cmdstring, attach, stdfiles, pipes);
#else
    fprintf(stderr, "ERROR: posix_spawn() not available on this host\n");
    exit(1);
#endif
    break;
  case 3:
#if HAVE_SPAWNVP
    if( which_debug ) fprintf(stderr, "launch_spawnvp: %s\n", cmdstring);
    return launch_spawnvp(cmdstring, attach, stdfiles, pipes);
#else
    fprintf(stderr, "ERROR: spawnvp() not available on this host\n");
    exit(1);
#endif
    break;
  default:
#if HAVE_MINGW32==0
    if( which_debug ) fprintf(stderr, "launch_fork_exec: %s\n", cmdstring);
    return launch_fork_exec(cmdstring, attach, stdfiles, pipes);
#else
    fprintf(stderr, "ERROR: no launch techniques available on this host\n");
    exit(1);
#endif
    break;
  }
  /* can't happen */
  return -1;
}