1/* run.c
2   Run a program.
3
4   Copyright (C) 1992, 1993, 1994 Ian Lance Taylor
5
6   This file is part of the Taylor UUCP package.
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License as
10   published by the Free Software Foundation; either version 2 of the
11   License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21
22   The author of the program may be contacted at ian@airs.com.
23   */
24
25#include "uucp.h"
26
27#include "uudefs.h"
28#include "sysdep.h"
29#include "system.h"
30
31#include <errno.h>
32
33/* Start up a new program.  */
34
35boolean
36fsysdep_run (ffork, zprogram, zarg1, zarg2)
37     boolean ffork;
38     const char *zprogram;
39     const char *zarg1;
40     const char *zarg2;
41{
42  char *zlib;
43  const char *azargs[4];
44  int aidescs[3];
45  pid_t ipid;
46
47  /* If we are supposed to fork, fork and then spawn so that we don't
48     have to worry about zombie processes.  */
49  if (ffork)
50    {
51      ipid = ixsfork ();
52      if (ipid < 0)
53	{
54	  ulog (LOG_ERROR, "fork: %s", strerror (errno));
55	  return FALSE;
56	}
57
58      if (ipid != 0)
59	{
60	  /* This is the parent.  Wait for the child we just forked to
61	     exit (below) and return.  */
62	  (void) ixswait ((unsigned long) ipid, (const char *) NULL);
63
64	  /* Force the log files to be reopened in case the child just
65	     output any error messages and stdio doesn't handle
66	     appending correctly.  */
67	  ulog_close ();
68
69	  return TRUE;
70	}
71
72      /* This is the child.  Detach from the terminal to avoid any
73	 unexpected SIGHUP signals.  At this point we are definitely
74	 not a process group leader, so usysdep_detach will not fork
75	 again.  */
76      usysdep_detach ();
77
78      /* Now spawn the program and then exit.  */
79    }
80
81  zlib = zbufalc (sizeof SBINDIR + sizeof "/" + strlen (zprogram));
82  sprintf (zlib, "%s/%s", SBINDIR, zprogram);
83
84  azargs[0] = zlib;
85  azargs[1] = zarg1;
86  azargs[2] = zarg2;
87  azargs[3] = NULL;
88
89  aidescs[0] = SPAWN_NULL;
90  aidescs[1] = SPAWN_NULL;
91  aidescs[2] = SPAWN_NULL;
92
93  /* We pass fsetuid and fshell as TRUE, which permits uucico and
94     uuxqt to be replaced by (non-setuid) shell scripts.  */
95  ipid = ixsspawn (azargs, aidescs, TRUE, FALSE, (const char *) NULL,
96		   FALSE, TRUE, (const char *) NULL,
97		   (const char *) NULL, (const char *) NULL);
98  ubuffree (zlib);
99
100  if (ipid < 0)
101    {
102      ulog (LOG_ERROR, "ixsspawn: %s", strerror (errno));
103      if (ffork)
104	_exit (EXIT_FAILURE);
105      return FALSE;
106    }
107
108  if (ffork)
109    _exit (EXIT_SUCCESS);
110
111  return TRUE;
112}
113