summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libunix/run.c
blob: af9b078d3bd93aba758c524205a952fa9f87d2e9 (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
/* run.c
   Run a program.

   Copyright (C) 1992, 1993, 1994 Ian Lance Taylor

   This file is part of the Taylor UUCP package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, Building 200, 1 Kendall Square, Cambridge, MA 02139.
   */

#include "uucp.h"

#include "uudefs.h"
#include "sysdep.h"
#include "system.h"

#include <errno.h>

/* Start up a new program.  */

boolean
fsysdep_run (ffork, zprogram, zarg1, zarg2)
     boolean ffork;
     const char *zprogram;
     const char *zarg1;
     const char *zarg2;
{
  char *zlib;
  const char *azargs[4];
  int aidescs[3];
  pid_t ipid;

  /* If we are supposed to fork, fork and then spawn so that we don't
     have to worry about zombie processes.  */
  if (ffork)
    {
      ipid = ixsfork ();
      if (ipid < 0)
	{
	  ulog (LOG_ERROR, "fork: %s", strerror (errno));
	  return FALSE;
	}

      if (ipid != 0)
	{
	  /* This is the parent.  Wait for the child we just forked to
	     exit (below) and return.  */
	  (void) ixswait ((unsigned long) ipid, (const char *) NULL);

	  /* Force the log files to be reopened in case the child just
	     output any error messages and stdio doesn't handle
	     appending correctly.  */
	  ulog_close ();

	  return TRUE;
	}

      /* This is the child.  Detach from the terminal to avoid any
	 unexpected SIGHUP signals.  At this point we are definitely
	 not a process group leader, so usysdep_detach will not fork
	 again.  */
      usysdep_detach ();

      /* Now spawn the program and then exit.  */
    }

  zlib = zbufalc (sizeof SBINDIR + sizeof "/" + strlen (zprogram));
  sprintf (zlib, "%s/%s", SBINDIR, zprogram);

  azargs[0] = zlib;
  azargs[1] = zarg1;
  azargs[2] = zarg2;
  azargs[3] = NULL;

  aidescs[0] = SPAWN_NULL;
  aidescs[1] = SPAWN_NULL;
  aidescs[2] = SPAWN_NULL;

  /* We pass fsetuid and fshell as TRUE, which permits uucico and
     uuxqt to be replaced by (non-setuid) shell scripts.  */
  ipid = ixsspawn (azargs, aidescs, TRUE, FALSE, (const char *) NULL,
		   FALSE, TRUE, (const char *) NULL,
		   (const char *) NULL, (const char *) NULL);
  ubuffree (zlib);

  if (ipid < 0)
    {
      ulog (LOG_ERROR, "ixsspawn: %s", strerror (errno));
      if (ffork)
	_exit (EXIT_FAILURE);
      return FALSE;
    }

  if (ffork)
    _exit (EXIT_SUCCESS);

  return TRUE;
}
OpenPOWER on IntegriCloud