File: kcreate.c

package info (click to toggle)
lam 7.1.4-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 56,404 kB
  • sloc: ansic: 156,541; sh: 9,991; cpp: 7,699; makefile: 5,621; perl: 488; fortran: 260; asm: 83
file content (264 lines) | stat: -rw-r--r-- 6,125 bytes parent folder | download | duplicates (11)
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
/*
 * Copyright (c) 2001-2003 The Trustees of Indiana University.  
 *                         All rights reserved.
 * Copyright (c) 1998-2001 University of Notre Dame. 
 *                         All rights reserved.
 * Copyright (c) 1994-1998 The Ohio State University.  
 *                         All rights reserved.
 * 
 * This file is part of the LAM/MPI software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * LAM/MPI source distribution.
 * 
 * $HEADER$
 *
 * $Id: kcreate.c,v 1.15 2003/11/04 02:40:26 jsquyres Exp $
 *
 *  Function: - creates an OTB process
 *      - does not make a Trollius kernel request
 *      - essentially a packaged fork and exec
 *      - redirects created process's stdio
 *  Accepts:  - child program name
 *      - argument vector
 *      - environment vector
 *      - stdio file descriptors (0 if no redirection)
 *  Returns:  - (to parent) child pid or ERROR
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "kio.h"
#include "preq.h"
#include "terror.h"
#include "typical.h"
#include "lamdebug.h"

/*
 * external functions
 */
extern void   _cipc_cleanup();
extern int    lam_pty_open();
extern int    liosattach();
extern int    liofdattach();
extern int4   stoi();

/*
 * private functions
 */
static int    redirect_stdio(int, int *);
static int    set_environment(char **);
static void   set_handler_default(int);


int
kcreate(const char *pathname, char **argv)
{
    return kcreatev(pathname, argv, NULL, NULL, NULL, 0);
}

int
kcreatev(const char *pathname, char **argv, char **envv, char *wrkdir, 
         int *fds, int umask_mode)
{
  sigset_t    sigs;
  sigset_t    newset;
  sigset_t    oldset;
  int     pid;
  int     rtf;
  int     errno_save;
  char    *p;
  
  sigemptyset(&newset);
  sigaddset(&newset, SIGTERM);
  sigaddset(&newset, SIGINT);
  sigaddset(&newset, SIGHUP);
  sigprocmask(SIG_BLOCK, &newset, &oldset);
  
  pid = fork();
  
  if (pid != 0) {
    errno_save = errno;
    sigprocmask(SIG_SETMASK, &oldset, (sigset_t *)0);
    if (pid < 0) {
      errno = errno_save;
      return LAMERROR;
    }
    else {
      return pid;
    }
  }

  if (pid == 0) {
/*
 * Set signal handlers back to the default. This must be done before unblocking
 * the signals to avoid a race where a signal delivered to the child can lead
 * to the execution of the kenyad signal handlers and accidental killing
 * of kenyad.
 */
    set_handler_default(SIGTERM);
    set_handler_default(SIGINT);
    set_handler_default(SIGHUP);
    set_handler_default(SIGCHLD);
    set_handler_default(SIGPIPE);
/*
 * Unblock all signals.
 */
    sigprocmask(0, 0, &sigs);
    sigprocmask(SIG_UNBLOCK, &sigs, 0);

  /*
   * set umask if desired
   */
    if (umask_mode > 0)
      umask(umask_mode);

/*
 * Redirect the stdio fd's
 */
    rtf = (p = getenv("TROLLIUSRTF")) ? stoi((unsigned char *) p) : 0;
    if (redirect_stdio(rtf, fds))
      exit(errno);
/*
 * Close all the rest of the file descriptors.
 */
    _cipc_cleanup();
    lamcloselog();

    if (set_environment(envv))
      exit(errno);
    
    if (wrkdir) {
      if (chdir(wrkdir) && !(rtf & RTF_TRYCWD)) {
        fprintf(stderr, "kcreatev: chdir(%s): ", wrkdir);
        terror("");
        exit(errno);
      }
    }

    execvp(pathname, argv);
    exit(errno);
  }

  /* This is never reached, but we put it here because some compilers
     can't see logic like if (x) { ...; return; } else { ...; exit(); }
     and complain because there's no return statement.  [sigh]. */

  return LAMERROR;
}

/*
 *  redirect_stdio
 *
 *  Function: - redirect process stdio
 *  Accepts:  - runtime flags
 *      - passed file descriptors or NULL
 */
static int
redirect_stdio(int rtf, int *fds)
{
  int     ionode;
  int     stdout_fd;
  int     stderr_fd;
  int     master;
  int     slave;
  char    *p;

  if (fds) {
/*
 * Redirect stdio to the passed fds.
 */
    dup2(fds[0], fileno(stdin));
    dup2(fds[1], fileno(stdout));
    dup2(fds[2], fileno(stderr));
  } else {
    if ((rtf&RTF_IO) && !(rtf&RTF_PFDIO) && (p = getenv("TROLLIUSFD"))) {
/*
 * Connect UNIX stderr and stdout to IO daemon.
 */
      ionode = stoi((unsigned char *) p);
      p = strchr(p, ':') + 1;
      p = strchr(p, ':') + 1;     /* skip stdin */
      stdout_fd = stoi((unsigned char *) p);
      p = strchr(p, ':') + 1;
      stderr_fd = stoi((unsigned char *) p);
/*
 * Attach stderr stream to iod nobuffered.
 */
      if (liosattach(stderr, ionode, stderr_fd, _IONBF))
        return(LAMERROR);
/*
 * Try to use a pty for stdout if asked for and if stdout is a tty.
 * pty support is enabled ONLY IF FD passing is supported */
#if LAM_HAVE_FD_PASSING 
      if ((rtf & RTF_PTYS) && (rtf & RTF_TTYOUT)) {
        if (lam_pty_open(&master, &slave)) {
          if (errno == ENOPTYSUPPORT) {
/*
 * No pty support on this node so fall back to no pty IO.
 */
            if (liosattach(stdout, ionode, stdout_fd, _IONBF))
              return(LAMERROR);
          }
        } else {
          if (dup2(slave, fileno(stdout)) != fileno(stdout))
            return(LAMERROR);
          if (liofdattach(master, ionode, stdout_fd))
            return(LAMERROR);
        }
      } else {
        if (liosattach(stdout, ionode, stdout_fd, _IONBF))
          return(LAMERROR);
      }
#else
/* we dont have FD passing so just connect to the IO stream */
      if (liosattach(stdout, ionode, stdout_fd, _IONBF)){
        return (LAMERROR);
      }
#endif

    }
  }

  return(0);
}

/*
 *  set_environment
 *
 *  Function: - set process environment
 *  Accepts:  - environment vector
 */
static int
set_environment(char **env)
{
  int i;

  if (env) {
    for (i = 0; env[i]; i++) {
      if (putenv(env[i]))
        return(LAMERROR);
    }
  }

  return(0);
}

static void
set_handler_default(int sig)
{
  struct sigaction act;

  act.sa_handler = SIG_DFL;
  act.sa_flags = 0;
  sigemptyset(&act.sa_mask);

  sigaction(sig, &act, (struct sigaction *)0);
}