summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libunix/pipe.c
blob: 7f69040b88f1f527c640fc7117d1da13199322ab (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
/* pipe.c
   The pipe port communication routines for Unix.
   Contributed by Marc Boucher <marc@CAM.ORG>.

   Copyright (C) 1993 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucp.h"

#if USE_RCS_ID
const char pipe_rcsid[] = "$Id: pipe.c,v 1.6 1995/06/21 19:19:57 ian Rel $";
#endif

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

#include <errno.h>

#if HAVE_FCNTL_H
#include <fcntl.h>
#else
#if HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#endif

/* Local functions.  */

static void uspipe_free P((struct sconnection *qconn));
static boolean fspipe_open P((struct sconnection *qconn, long ibaud,
			      boolean fwait));
static boolean fspipe_close P((struct sconnection *qconn,
			       pointer puuconf,
			       struct uuconf_dialer *qdialer,
			       boolean fsuccess));
static boolean fspipe_dial P((struct sconnection *qconn, pointer puuconf,
			      const struct uuconf_system *qsys,
			      const char *zphone,
			      struct uuconf_dialer *qdialer,
			      enum tdialerfound *ptdialer));

/* The command table for standard input ports.  */

static const struct sconncmds spipecmds =
{
  uspipe_free,
  NULL, /* pflock */
  NULL, /* pfunlock */
  fspipe_open,
  fspipe_close,
  fspipe_dial,
  fsdouble_read,
  fsdouble_write,
  fsysdep_conn_io,
  NULL, /* pfbreak */
  NULL, /* pfset */
  NULL, /* pfcarrier */
  fsdouble_chat,
  NULL  /* pibaud */
};

/* Initialize a pipe connection.  */

boolean
fsysdep_pipe_init (qconn)
     struct sconnection *qconn;
{
  struct ssysdep_conn *q;

  q = (struct ssysdep_conn *) xmalloc (sizeof (struct ssysdep_conn));
  q->o = -1;
  q->ord = -1;
  q->owr = -1;
  q->zdevice = NULL;
  q->iflags = -1;
  q->iwr_flags = -1;
  q->fterminal = FALSE;
  q->ftli = FALSE;
  q->ibaud = 0;
  q->ipid = -1;
  qconn->psysdep = (pointer) q;
  qconn->qcmds = &spipecmds;
  return TRUE;
}

static void
uspipe_free (qconn)
     struct sconnection *qconn;
{
  xfree (qconn->psysdep);
}

/* Open a pipe port.  */

/*ARGSUSED*/
static boolean
fspipe_open (qconn, ibaud, fwait)
     struct sconnection *qconn;
     long ibaud;
     boolean fwait;
{
  /* We don't do incoming waits on pipes.  */
  if (fwait)
    return FALSE;

  return TRUE;
}

/* Close a pipe port.  */

/*ARGSUSED*/
static boolean
fspipe_close (qconn, puuconf, qdialer, fsuccess)
     struct sconnection *qconn;
     pointer puuconf;
     struct uuconf_dialer *qdialer;
     boolean fsuccess;
{
  struct ssysdep_conn *qsysdep;
  boolean fret;

  qsysdep = (struct ssysdep_conn *) qconn->psysdep;
  fret = TRUE;

  /* Close our sides of the pipe.  */
  if (qsysdep->ord >= 0 && close (qsysdep->ord) < 0)
    {
      ulog (LOG_ERROR, "fspipe_close: close read fd: %s", strerror (errno));
      fret = FALSE;
    }
  if (qsysdep->owr != qsysdep->ord
      && qsysdep->owr >= 0
      && close (qsysdep->owr) < 0)
    {
      ulog (LOG_ERROR, "fspipe_close: close write fd: %s", strerror (errno));
      fret = FALSE;
    }
  qsysdep->ord = -1;
  qsysdep->owr = -1;

  /* Kill dangling child process.  */
  if (qsysdep->ipid >= 0)
    {
      if (kill (qsysdep->ipid, SIGHUP) == 0)
        usysdep_sleep (2);
#ifdef SIGPIPE
      if (kill (qsysdep->ipid, SIGPIPE) == 0)
        usysdep_sleep (2);
#endif
      if (kill (qsysdep->ipid, SIGKILL) < 0 && errno == EPERM)
	{
	  ulog (LOG_ERROR, "fspipe_close: Cannot kill child pid %lu: %s",
		(unsigned long) qsysdep->ipid, strerror (errno));
	  fret = FALSE;
	}
      else
	(void) ixswait ((unsigned long) qsysdep->ipid, (const char *) NULL);
    }
  qsysdep->ipid = -1;
  return fret;
}

/* Dial out on a pipe port, so to speak: launch connection program
   under us.  The code alternates q->o between q->ord and q->owr as
   appropriate.  It is always q->ord before any call to fsblock.  */

/*ARGSUSED*/
static boolean
fspipe_dial (qconn, puuconf, qsys, zphone, qdialer, ptdialer)
     struct sconnection *qconn;
     pointer puuconf;
     const struct uuconf_system *qsys;
     const char *zphone;
     struct uuconf_dialer *qdialer;
     enum tdialerfound *ptdialer;
{
  struct ssysdep_conn *q;
  int aidescs[3];
  const char **pzprog;

  q = (struct ssysdep_conn *) qconn->psysdep;

  *ptdialer = DIALERFOUND_FALSE;

  pzprog = (const char **) qconn->qport->uuconf_u.uuconf_spipe.uuconf_pzcmd;

  if (pzprog == NULL)
    {
      ulog (LOG_ERROR, "No command for pipe connection");
      return FALSE;
    }

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

  /* Pass fkeepuid, fkeepenv and fshell as TRUE.  This puts the
     responsibility of security on the connection program.  */
  q->ipid = ixsspawn (pzprog, aidescs, TRUE, TRUE, (const char *) NULL,
		      FALSE, TRUE, (const char *) NULL,
		      (const char *) NULL, (const char *) NULL);
  if (q->ipid < 0)
    {
      ulog (LOG_ERROR, "ixsspawn (%s): %s", pzprog[0], strerror (errno));
      return FALSE;
    }

  q->owr = aidescs[0];
  q->ord = aidescs[1];
  q->o = q->ord;

  q->iflags = fcntl (q->ord, F_GETFL, 0);
  q->iwr_flags = fcntl (q->owr, F_GETFL, 0);
  if (q->iflags < 0 || q->iwr_flags < 0)
    {
      ulog (LOG_ERROR, "fspipe_dial: fcntl: %s", strerror (errno));
      (void) fspipe_close (qconn, puuconf, qdialer, FALSE);
      return FALSE;
    }

  return TRUE;
}

#if 0

/* Marc Boucher's contributed code used an alarm to avoid waiting too
   long when closing the pipe.  However, I believe that it is not
   possible for the kernel to sleep when closing a pipe; it is only
   possible when closing a device.  Therefore, I have removed the
   code, but am preserving it in case I am wrong.  To reenable it, the
   two calls to close in fspipe_close should be changed to call
   fspipe_alarmclose.  */

static RETSIGTYPE
usalarm (isig)
     int isig;
{
#if ! HAVE_SIGACTION && ! HAVE_SIGVEC && ! HAVE_SIGSET
  (void) signal (isig, usalarm);
#endif

#if HAVE_RESTARTABLE_SYSCALLS
  longjmp (sSjmp_buf, 1);
#endif
}

static int
fspipe_alarmclose (fd)
     int fd;
{
  int iret = -1;
  int ierrno = 0;

  if (fsysdep_catch ())
    {
      usysdep_start_catch ();
      usset_signal (SIGALRM, usalarm, TRUE, (boolean *) NULL);
      (void) alarm (30);

      iret = close (fd);
      ierrno = errno;
    }

  usset_signal (SIGALRM, SIG_IGN, TRUE, (boolean *) NULL);
  (void) alarm (0);
  usysdep_end_catch ();

  errno = ierrno;
  return iret;
}

#endif /* 0 */
OpenPOWER on IntegriCloud