File: pcexec.c

package info (click to toggle)
pact 980714-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 13,096 kB
  • ctags: 26,034
  • sloc: ansic: 109,076; lisp: 9,645; csh: 7,147; fortran: 1,050; makefile: 136; lex: 95; sh: 32
file content (319 lines) | stat: -rw-r--r-- 9,251 bytes parent folder | download
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
/*
 * PCEXEC.C - Portable Process Control system exec utility
 *
 * Source Version: 2.0
 * Software Release #92-0043
 *
 */

#include "cpyright.h"

#include "ppc.h"

static PROCESS
 *pp = NULL;

static int
 SC_DECLARE(process_end, (char *name, int *pr, int quiet)),
 SC_DECLARE(interrupt_mode, (char *name, int quiet)),
 SC_DECLARE(poll_mode, (char *name, int quiet));

static void
 SC_DECLARE(child_has_txt, (int fd)),
 SC_DECLARE(tty_has_txt, (int fd)),
 SC_DECLARE(error_handler, (int sig));

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* MAIN - test PPC */

int main(argc, argv, envp)
   int argc;
   char **argv, **envp;
   {int i, interrupts, ret, quiet, access_file, log, parallel, to;
    char mode[5];

/* process the command line arguments */
    quiet       = FALSE;
    interrupts  = TRUE;
    parallel    = FALSE;
    access_file = FALSE;
    log         = FALSE;
    to          = 1000000;
    strcpy(mode, "w");
    if (argc < 2)
       {printf("\nUsage: pcexec [-pst] [-c n] [-i] <prog> [<arg1> ...]\n");
        printf("       pcexec -f <host>\n\n");
        printf("       pcexec -r [<arg1> ...]\n\n");
        printf("   The first form runs <prog> as a child process.\n");
        printf("   The second form acts as a remote file access server.\n");
        printf("   The third form acts as a parallel communications server.\n");
        printf("   The forms for <prog> are:\n");
        printf("       <name>              - run <name> on local <host>\n");
        printf("       <host>:<name>       - run <name> on remote <host>\n");
        printf("       <CPU>@<name>        - run <name> on processor <CPU>\n");
        printf("       <host>:<CPU>@<name> - run <name> on processor <CPU>\n");
        printf("   The last two are not yet completed.\n");
        printf("\n");
        printf("   The full syntax for <host> is:\n");
        printf("       <hostname>                         or\n");
        printf("       <hostname>,<username>              or\n");
        printf("       <hostname>,<username>,<passwd>\n");
        printf("   Note: whitespace is NOT allowed\n");
        printf("   Options:\n");
        printf("      c - timeout after n seconds\n");
        printf("      i - poll explicitly instead of using system call\n");
        printf("      l - when acting as a file server, log transactions to\n");
        printf("          PC_fs.log in your home directory\n");
        printf("\n");
        printf("      p - use pipes for communications\n");
        printf("      q - print only messages from the child\n");
        printf("      s - use sockets for communications\n");
        printf("      t - use pseudo terminals for communications\n");
        printf("   These only apply to processes on the same CPU.\n");
        printf("\n");

        return(1);};

    for (i = 1; argv[i] != NULL; i++)
        {if (argv[i][0] == '-')
            {switch (argv[i][1])
                {case 'c' : to = SC_stoi(argv[++i]);
                            break;
                 case 'f' : access_file = TRUE;
                            break;
                 case 'i' : interrupts = FALSE;
                            break;
                 case 'l' : log = TRUE;
                            break;
                 case 'r' : parallel = TRUE;
		            i++;
		            break;
                 case 'p' : strcpy(mode, "wp");
                            break;
                 case 'q' : quiet = TRUE;
                            break;
                 case 's' : strcpy(mode, "ws");
                            break;
                 case 't' : strcpy(mode, "wt");
                            break;};}
         else
            break;};

/* trap the following signals to restore the terminal state */

#ifdef UNIX
    SIGNAL(SIGSEGV, error_handler);
    SIGNAL(SIGABRT, error_handler);
    SIGNAL(SIGTERM, error_handler);
    SIGNAL(SIGQUIT, error_handler);
    SIGNAL(SIGILL, error_handler);
    SIGNAL(SIGINT, error_handler);
#endif

    if (access_file)
       ret = PC_file_access(log);

    else if (parallel)
       ret = PC_process_access(argv, "rsb+");

    else
       {if ((strcmp(argv[i], "ftp") == 0) ||
	    (strcmp(argv[i], "telnet") == 0))
	   {if (strcmp(mode, "wt") != 0)
	       {if (!quiet)
		   PRINT(stdout,
			 "\nWarning: run with -t flag\n\n");};};

/* print this before we potentially go into RAW mode (PTY's do this) */
	if (!quiet)
	   PRINT(stdout, "\nRunning process: %s\n\n", argv[i]);

/* set the alarm */
        PC_alarm(to, error_handler);

	pp = PC_open(argv+i, envp, mode);
	if (pp == NULL)
	   {if (!quiet)
	       {PRINT(stdout, "%s\n", PC_err);
		PRINT(stdout, "\nFailed to open: %s\n\n", argv[i]);};
	    error_handler(0);};

/* reset the alarm */
        PC_alarm(0, NULL);

	SC_setbuf(stdout, NULL);

/* set the alarm */
        PC_alarm(to, error_handler);

	if (interrupts)
	   ret = interrupt_mode(argv[i], quiet);

	else
	   ret = poll_mode(argv[i], quiet);

/* reset the alarm */
        PC_alarm(0, NULL);

	if (!quiet)
	   PRINT(stdout, "Process test %s ended\n\n", argv[i]);};

    return(ret);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* INTERRUPT_MODE - handle the child and tty I/O via interrupts */

static int interrupt_mode(name, quiet)
   char *name;
   int quiet;
   {int pi, ret;

    PC_io_interrupts_on = TRUE;

    pi  = PC_io_callback_file(stdin, tty_has_txt);
    pi &= PC_io_callback_fd(pp->in, child_has_txt);

    PC_io_interrupts_on = pi;

    if (pi)
       PC_catch_io_interrupts(PC_io_interrupts_on);

    ret = -1;
    while (TRUE)
       {if (process_end(name, &ret, quiet))
	   break;

	PC_poll_descriptors(-1);};

    return(ret);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* CHILD_HAS_TXT - handle text from the child process */

static void child_has_txt(fd)
   int fd;
   {char s[MAXLINE];

    while (PC_gets(s, MAXLINE, pp) != NULL)
       PRINT(stdout, "%s", s);

    return;}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* TTY_HAS_TXT - handle text from the tty */

static void tty_has_txt(fd)
   int fd;
   {char s[MAXLINE];

    if (fgets(s, LRG_TXT_BUFFER, stdin) != NULL)
       PC_printf(pp, "%s", s);

    return;}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* POLL_MODE - poll the process and the tty */

static int poll_mode(name, quiet)
   char *name;
   int quiet;
   {char s[LRG_TXT_BUFFER], *t;
    int ret;

    ret = -1;
    while (TRUE)
       {while (PC_gets(s, LRG_TXT_BUFFER, pp) != NULL)
           PRINT(stdout, "%s", s);

        if (process_end(name, &ret, quiet))
           break;

        PC_unblock_file(stdin);
        PC_err[0] = '\0';

        t = fgets(s, LRG_TXT_BUFFER, stdin);

        PC_block_file(stdin);
        PC_err[0] = '\0';

        if (t != NULL)
           PC_printf(pp, "%s", s);

        if (PC_err[0] != '\0')

/* close now since the TTY may be in RAW mode and we want the output to
 * look nice (PTY's do this)
 */
           {PC_close(pp);
	    if (!quiet)
               PRINT(stdout, "%s\n\n", PC_err);
            break;};};

    return(ret);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* PROCESS_END - return TRUE if the process has ended
 *             - do the PC_close now since the TTY may be in
 *             - RAW mode and we want the output to
 *             - look nice (PTY's do this)
 */

static int process_end(name, pr, quiet)
   char *name;
   int *pr, quiet;
   {int status, reason;

    if (PC_status(pp) != RUNNING)
       {status = pp->status;
        reason = pp->reason;

        *pr = reason;

/* get anything remaining from the child */
        child_has_txt(pp->in);

        PC_close(pp);

        if (!quiet)
           PRINT(stdout, "\nProcess %s terminated (%d %d)\n",
	         name, status, reason);

        return(TRUE);}

    else
       return(FALSE);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* ERROR_HANDLER - trap various signals and restore the terminal */

static void error_handler(sig)
   int sig;
   {if (PC_original_terminal_state != NULL)
       {PC_set_term_state(PC_original_terminal_state, -1);
        SFREE(PC_original_terminal_state);};

    PC_block_file(stdin);

    if (sig == SIGALRM)
       {PRINT(stdout, "PCEXEC timed out\n");
	exit(123);}
    else
       {PRINT(stdout, "PCEXEC exiting with signal %d\n", sig);
	exit(1);};}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/