File: crm_expr_syscall.c

package info (click to toggle)
crm114 20050415-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,288 kB
  • ctags: 2,574
  • sloc: ansic: 25,130; sh: 9,439; makefile: 452; lisp: 208; python: 36; sed: 16
file content (439 lines) | stat: -rwxr-xr-x 13,588 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//  crm_expr_syscall.c  - Controllable Regex Mutilator,  version v1.0
//  Copyright 2001-2004  William S. Yerazunis, all rights reserved.
//  
//  This software is licensed to the public under the Free Software
//  Foundation's GNU GPL, version 2.  You may obtain a copy of the
//  GPL by visiting the Free Software Foundations web site at
//  www.fsf.org, and a copy is included in this distribution.  
//
//  Other licenses may be negotiated; contact the 
//  author for details.  
//
//  include some standard files
#include "crm114_sysincludes.h"

//  include any local crm114 configuration file
#include "crm114_config.h"

//  include the crm114 data structures file
#include "crm114_structs.h"

//  and include the routine declarations file
#include "crm114.h"

//    the command line argc, argv
extern int prog_argc;
extern char **prog_argv;

//    the auxilliary input buffer (for WINDOW input)
extern char *newinputbuf;

//    the globals used when we need a big buffer  - allocated once, used 
//    wherever needed.  These are sized to the same size as the data window.
extern char *inbuf;
extern char *outbuf;
extern char *tempbuf;

int crm_expr_syscall ( CSL_CELL *csl, ARGPARSE_BLOCK *apb)
{
  //           Go off and fork a process, sending that process
  //           one pattern evaluated as input, and then accepting
  //           all the returns from that process as the new value
  //           for a variable.
  //           
  //           syntax is:
  //               exec (:to:) (:from:) (:ctl:) /commandline/
  long inlen;
  long outlen;
  char from_var [MAX_VARNAME];
  char sys_cmd [MAX_PATTERN];
  long cmd_len;
  char keep_buf [MAX_PATTERN];
  long keep_len;
  char exp_keep_buf[MAX_PATTERN];
  long exp_keep_len;
  long vstart;
  long vlen;
  long done, charsread;
  int keep_proc;
  int async_mode;
  int to_minion[2];
  int from_minion[2];
  pid_t minion;
  int minion_exit_status;
  pid_t pusher;
  pid_t sucker;
  pid_t random_child;
  int status;
  long timeout;	
  
#ifdef WIN32
  fatalerror ("Sorry, but SYSCALL isn't supported (yet) under Win32.  ",
	      "If you can write code to do this, please submit it!  ");
  exit (EXIT_FAILURE)
#endif

#ifdef POSIX

  if (user_trace)
    fprintf (stderr, "executing an SYSCALL statement");

  timeout = MINION_SLEEP_USEC;


  //  clean up any prior processes - note that
  //  we don't keep track of these.  For that matter, we have
  //  no context to keep track of 'em.
  //
  while ( (random_child = waitpid ( 0, &status, WNOHANG)) > 0 );
  
  
  //    get the flags
  //
  keep_proc = 0;
  if (apb->sflags & CRM_KEEP)
    {
      if (user_trace)
	fprintf (stderr, "Keeping the process around if possible\n");
      keep_proc = 1;
    };
  async_mode = 0;
  if (apb->sflags & CRM_ASYNC)
    {
      if (user_trace)
	fprintf (stderr, "Letting the process go off on it's own");
      async_mode = 1;
    };
  
  //     Sanity check - <async> is incompatible with <keep>
  //
  if (keep_proc && async_mode)
    {
      nonfatalerror ("This syscall uses both async and keep, but async is "
		     "incompatible with keep.  Since keep is safer"
		     "we will use that.\n",
		     "You need to fix this program.");
      async_mode = 0;
    };

  //    get the input variable(s)
  //
  crm_get_pgm_arg (inbuf, data_window_size, apb->p1start, apb->p1len);
  inlen = crm_nexpandvar (inbuf, apb->p1len, data_window_size);
  if (user_trace)
    fprintf (stderr, "  command's input wil be: ***%s***\n", inbuf);
  
  //    now get the name of the variable where the return will be 
  //    placed... this is a crock and should be fixed someday.
  //    the output goes only into a single var (the first one)
  //    so we extract that
  //
  crm_get_pgm_arg (from_var, MAX_PATTERN, apb->p2start, apb->p2len);
  outlen = crm_nexpandvar (from_var, apb->p2len, MAX_PATTERN);
  done = 0;
  vstart = 0;
  while (from_var[vstart] < 0x021 && from_var[vstart] > 0x0 )
    vstart++;
  vlen = 0;
  while (from_var[vstart+vlen] >= 0x021)
    vlen++;
  memmove (from_var, &from_var[vstart], vlen);
  from_var[vlen] = '\000';
  if (user_trace)
    fprintf (stderr, "   command output will overwrite var ***%s***\n",
	     from_var);
  
  
  //    now get the name of the variable (if it exists) where
  //    the kept-around minion process's pipes and pid are stored.
  crm_get_pgm_arg (keep_buf, MAX_PATTERN, apb->p3start, apb->p3len);
  keep_len = crm_nexpandvar (keep_buf, apb->p3len, MAX_PATTERN);
  if (user_trace)
    fprintf (stderr, "   command status kept in var ***%s***\n", 
	     keep_buf);
  
  //      get the command to execute
  //
  crm_get_pgm_arg (sys_cmd, MAX_PATTERN, apb->s1start, apb->s1len);
  cmd_len = crm_nexpandvar (sys_cmd, apb->s1len, MAX_PATTERN);
  if (user_trace)
    fprintf (stderr, "   command will be ***%s***\n", sys_cmd);
  
  //     Do we reuse an already-existing process?  Check to see if the 
  //     keeper variable has it... note that we have to :* prefix it
  //     and expand it again.
  minion = 0;
  to_minion[0] = 0;
  from_minion[1] = 0;
  exp_keep_buf [0] = '\000';
  //  this is 8-bit-safe because vars are never wchars. 
  strcat (exp_keep_buf, ":*");
  strncat (exp_keep_buf, keep_buf, keep_len);
  exp_keep_len = crm_nexpandvar (exp_keep_buf, keep_len+2, MAX_PATTERN);
  sscanf (exp_keep_buf, "MINION PROC PID: %d from-pipe: %d to-pipe: %d",
	  &minion,
	  &from_minion[0],
	  &to_minion[1]);
  
  
  //      if, no minion already existing, we create
  //      communications pipes and launch the subprocess.  This
  //      code borrows concepts from both liblaunch and from
  //      netcat (thanks, *Hobbit*!)
  //
  if (minion == 0)
    {
      if (user_trace)
	fprintf (stderr, "  Must start a new minion.\n");
      status = pipe (to_minion);
      status = pipe (from_minion);
      minion = fork();

      if (minion < 0)
	{
	  fatalerror ("Tried to fork your minion, but it failed.",
		      "Your system may have run out of process slots");
	  exit (EXIT_FAILURE);
	};

      if (minion == 0)
	{
	  //
	  //    if minion == 0, then We're in the minion here
	  int retcode;
	  long vstart, vlen;
	  long varline;
	  //    close the ends of the pipes we don't need.
	  //
	  //    NOTE: if this gets messed up, you end up with a race
	  //    condition, because both master and minion processes
	  //    can both read and write both pipes (effectively a
	  //    process could write something out, then read it again
	  //    right back out of the pipe)!  So, it's REALLY REALLY
	  //    IMPORTANT that you use two pipe structures, (one for
	  //    each direction) and you keep track of which process
	  //    should write to which pipe!!!
	  //
	  close (to_minion[1]);
	  close (from_minion[0]);
	  dup2 (to_minion[0], fileno(stdin));
	  dup2 (from_minion[1], fileno(stdout));

	  //     Are we a syscall to a :label:, or should we invoke the
	  //     shell on an external command?
	  //
	  crm_nextword (sys_cmd, strlen (sys_cmd), 0, &vstart, &vlen);
	  varline = crm_lookupvarline (vht, sys_cmd, vstart, vlen);
	  if (varline > 0)
	    {
	      sys_cmd[vstart+vlen] = '\0';
	      if (user_trace)
		fprintf (stderr, "FORK transferring control to %s\n",
			 &sys_cmd[vstart]);
	      csl->cstmt = varline;
	      //   and note that this isn't a failure.
	      csl->aliusstk [ csl->mct[csl->cstmt]->nest_level ] = 1;   
	      //   The minion's real work should now start; get out of 
	      //   the syscall code and go run something real.  :)
	      return (0);
	    }
	  else
	    {
	      if (user_trace)
		fprintf (stderr, "systemcalling on shell command %s\n", 
			 sys_cmd);
	      retcode = system (sys_cmd);
	      //  
	      //       This code only ever happens if an error occurs...   
	      //  
	      if (retcode == -1 
		  || ! WIFEXITED (retcode) )
		{
		  fatalerror (" Sorry, the command I tried to run couldn't run."
			      " The command was: ", sys_cmd);
		  exit ( EXIT_FAILURE );
		}
	      exit ( WEXITSTATUS (retcode) );
	    };
	};
    }
  else
    {
      if (user_trace)
	fprintf (stderr, "  reusing old minion PID: %d\n", minion);
    };
  //      Now, we're out of the minion for sure.
  //    so we close the pipe ends we know we won't be using.
  if (to_minion[0] != 0) 
    {
      close (to_minion[0]);
      close (from_minion[1]);
    };
  //  
  //   launch "pusher" process to send the buffer to the minion
  //    (this hint from Dave Soderberg).  This avoids the deadly
  //   embrace situation where both processes are waiting to read
  //   (or, equally, both processes have written and filled up
  //   their buffers, and are now held up waiting for the other
  //   process to empty some space in the output buffer)
  //
  if (strlen (inbuf) > 0)
    {
      pusher = fork ();
      //    we're in the "input pusher" process if we got here.
      //    shove the input buffer out to the minion
      if (pusher == 0)
	{
	  write (to_minion[1], inbuf, inlen );
	  if (internal_trace) 
	    fprintf (stderr, "pusher: input sent to minion.\n");
	  close (to_minion[1]);
	  if (internal_trace)
	    fprintf (stderr, "pusher: minion input pipe closed\n");
	  if (internal_trace)
	    fprintf (stderr, "pusher: exiting pusher\n");
	  exit ( EXIT_SUCCESS );
	};
    };
  //    now we're out of the pusher process.
  //    if we don't want to keep this proc, we close it's input, and
  //    wait for it to exit.
  if (! keep_proc)
    {
      close (to_minion[1]);
      if (internal_trace)
	fprintf (stderr, "minion input pipe closed\n");
    }
  
  //   and see what is in the pipe for us.
  outbuf[0] = '\000';
  done = 0;
  outlen = 0;
  //   grot grot grot this only works if varnames are not widechars
  if (strlen (from_var) > 0)
    {
      if (async_mode == 0 && keep_proc == 0)
	{
	  usleep (timeout);
	  //   synchronous read- read till we hit EOF, which is read 
	  //   returning a char count of zero.
	readloop:
	  if (internal_trace) fprintf (stderr, "SYNCH READ ");
	  usleep (timeout);
	  charsread = 
	    read (from_minion[0], 
		  &outbuf[done], 
		  (data_window_size >> SYSCALL_WINDOW_RATIO) - done - 2);
	  done = done + charsread; 
	  if ( charsread > 0 
	       && done + 2 < (data_window_size >> SYSCALL_WINDOW_RATIO))
	    goto readloop;
	  if (done < 0) done = 0;
	  outbuf [done] = '\000';
	  outlen = done ;
	};
      if (keep_proc == 1 || async_mode == 1)
	{
	  //   we're in either 'keep' 'async' mode.  Set nonblocking mode, then
	  //   read it once; then put it back in regular mode.
	  //fcntl (from_minion[0], F_SETFL, O_NONBLOCK);
	  //	  usleep (timeout);
	  charsread = read (from_minion[0], 
			    &outbuf[done], 
			    (data_window_size >> SYSCALL_WINDOW_RATIO));
	  done = charsread;
	  if (done < 0) done = 0;
	  outbuf [done] = '\000';
	  outlen = done ;
	  //fcntl (from_minion[0], F_SETFL, 0);
	};

      //   If the minion process managed to fill our buffer, and we
      //   aren't "keep"ing it around, OR if the process is "async",
      //   then we should also launch a sucker process to
      //   asynchronously eat all of the stuff we couldn't get into
      //   the buffer.  The sucker proc just reads stuff and throws it
      //   away asynchronously... and exits when it gets EOF.
      //  
      if ( async_mode ||
	   (outlen >= ((data_window_size >> SYSCALL_WINDOW_RATIO) - 2 )
	    && keep_proc == 0))
	{
	  sucker = fork (); 
	  if (sucker == 0)
	    {
	      //  we're in the sucker process here- just throw away
	      //  everything till we get EOF, then exit.
	      while (1) 
		{
		  usleep (timeout);
		  charsread = read (from_minion[0],
				    &outbuf[0],
				    data_window_size >> SYSCALL_WINDOW_RATIO );
		  if (charsread == 0) exit (EXIT_SUCCESS);
		};
	    };
	};
      
      //  and set the returned value into from_var.
      if (user_trace)
	fprintf (stderr, "SYSCALL output: %ld chars ---%s---.\n ", 
		 outlen, outbuf);
      if (internal_trace)
	fprintf (stderr, "  storing return str in var %s\n", from_var);
      
      crm_destructive_alter_nvariable ( from_var, vlen, outbuf, outlen);
    };
  
  //  Record useful minion data, if possible.
  if (strlen (keep_buf) > 0)
    {
      sprintf (exp_keep_buf, 
	       "MINION PROC PID: %d from-pipe: %d to-pipe: %d",
	       minion,
	       from_minion[0],
	       to_minion[1]);
      if (internal_trace)
	fprintf (stderr, "   saving minion state: %s \n", 
		 exp_keep_buf);
      crm_destructive_alter_nvariable (keep_buf, keep_len,
				       exp_keep_buf, 
				       strlen (exp_keep_buf));
    };
  //      If we're keeping this minion process around, record the useful
  //      information, like pid, in and out pipes, etc.
  if (keep_proc || async_mode)
    {
    }
  else
    {
      if (internal_trace)
	fprintf (stderr, "No keep, no async, so not keeping minion, closing everything.\n");

      //   no, we're not keeping it around, so close the pipe.
      //
      close (from_minion [0]);

      //    and de-zombify any dead minions;
      //
      waitpid ( minion, &minion_exit_status, 0);
      if ( crm_vht_lookup (vht, keep_buf, strlen (keep_buf)))
	{
	  char exit_value_string[MAX_VARNAME];
	  if (internal_trace) 
	    fprintf (stderr, "minion waitpid result :%d; whacking %s\n", 
		     minion_exit_status, 
		     keep_buf);
	  sprintf (exit_value_string, "DEAD MINION, EXIT CODE: %d", 
		   WEXITSTATUS (minion_exit_status)); 
	  if (keep_len > 0)
	    crm_destructive_alter_nvariable (keep_buf, keep_len,
					   exit_value_string,
					   strlen (exit_value_string));
	};
    };
  return (0);

#endif    //  end of the POSIX SYSCALL code.

};