File: prochandle.cpp

package info (click to toggle)
yiff 2.14.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,344 kB
  • ctags: 3,360
  • sloc: ansic: 40,505; cpp: 7,420; makefile: 425; sh: 242
file content (693 lines) | stat: -rw-r--r-- 14,724 bytes parent folder | download | duplicates (12)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <sched.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>

#ifndef _USE_BSD
# define _USE_BSD
#endif

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>

#include "../include/os.h"
#include "../include/string.h"
#include "../include/fio.h"
#include "../include/disk.h"
#include "../include/prochandle.h"


static void child_catcher(int s);
static void *add_signal(int signum, void (*handler)(int));

/* Explode commands */
char **ExecExplodeCommand(const char *cmd, int *strc);

/* Process checking */
int ExecProcessExists(pid_t pid);

/* Load monitoring */
float ExecCPUGetLoad(int cpu_num);

/* Regular execute */
void Execute(const char *cmd);

/* Non-blocking executes */
static pid_t ExecNexus(
	const char *cmd,
	const char *stdout_path, const char *stderr_path,
	const char *stdout_mode, const char *stderr_mode
);
pid_t Exec(const char *cmd);
pid_t ExecO(const char *cmd, const char *stdout_path);
pid_t ExecOE(const char *cmd, const char *stdout_path, const char *stderr_path);
pid_t ExecAO(const char *cmd, const char *stdout_path);
pid_t ExecAOE(const char *cmd, const char *stdout_path, const char *stderr_path);

/* Blocking executes */
static pid_t ExecBNexus(
	const char *cmd,
	const char *stdout_path, const char *stderr_path,
	const char *stdout_mode, const char *stderr_mode,
	int *status
);
pid_t ExecB(const char *cmd);
pid_t ExecBO(const char *cmd, const char *stdout_path);
pid_t ExecBOE(const char *cmd, const char *stdout_path, const char *stderr_path);
pid_t ExecBAO(const char *cmd, const char *stdout_path);
pid_t ExecBAOE(const char *cmd, const char *stdout_path, const char *stderr_path);


#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
#define ABSOLUTE(x)	(((x) < 0) ? -(x) : (x))
#define STRLEN(s)       (((s) != NULL) ? strlen(s) : 0)
#define STRISEMPTY(s)   (((s) != NULL) ? (*(s) == '\0') : 1)


/*
 *	Signal handler for SIGCHLD.
 */
static void child_catcher(int s)
{
	int status;
	while(wait3(&status, WNOHANG, (struct rusage *)0) > 0);
}

/*
 *	BSD style signal(2).
 *
 *	Returns the old signal action handler of type
 *	void (*handler)(int).
 */
static void *add_signal(int signum, void (*handler)(int))
{
	struct sigaction act, old_act;

	act.sa_handler = handler;
	act.sa_flags = 0;

	if(sigaction(signum, &act, &old_act) == -1)
	    return((void *)SIG_ERR);
	else
	    return((void *)old_act.sa_handler);
}

/*
 *	Explodes the command string cmd and returns an array of
 *	dynamically allocated strings.
 *
 *	Arguments inside quotes ('"') with spaces will not be exploded.
 *
 *	Arguments will be delimited by one or more spaces (not tabs).
 *
 *	The calling function must free() the returned strings and the
 *	array.
 */
char **ExecExplodeCommand(const char *cmd, int *strc)
{
	const char *s = cmd, *s_end;
	int len, total = 0;
	char *s_cur, **strv = NULL;

#define STRIP_BACKSLASHES(s)		{		\
 while(*(s) != '\0')					\
 {							\
  /* Backslash? */					\
  if(*(s) == '\\')					\
  {							\
   char *s2 = (s),					\
	c = *(s2 + 1);	/* Record escaped char */	\
							\
   /* Strip backslash */				\
   do { *s2 = *(s2 + 1); s2++; }			\
    while(*s2 != '\0');					\
							\
   /* Replace with appropriate escapee */		\
   switch(c) {						\
    case 'n':						\
     *(s) = '\n';					\
     break;						\
    case 'r':						\
     *(s) = '\r';					\
     break;						\
    case 't':						\
     *(s) = '\t';					\
     break;						\
    case 'e':						\
     *(s) = '\e';					\
     break;						\
    case 'b':						\
     *(s) = '\b';					\
     break;						\
   }							\
   if(*(s) != '\0')					\
    (s)++;						\
  }							\
  else							\
  {							\
   (s)++;						\
  }							\
 }							\
}

#define APPEND_STRING(s,l)		{		\
 int i = total;						\
 total++;						\
 strv = (char **)realloc(				\
  strv, total * sizeof(char *)				\
 );							\
 strv[i] = s_cur = (char *)malloc(			\
  ((l) + 1) * sizeof(char)				\
 );							\
 if((l) > 0)						\
  memcpy(s_cur, (s), (l) * sizeof(char));		\
 s_cur[(l)] = '\0';					\
}

	if(s == NULL)
	{
	    if(strc != NULL)
		*strc = 0;
	    return(strv);
	}

	/* Seek past initial spaces */
	while(ISBLANK(*s))
	    s++;

	/* Iterate through command string */
	while(*s != '\0')
	{
	    /* Argument in quotes? */
	    if(*s == '"')
	    {
		s++;

		/* Seek s_end to end of current argument (the next
		 * unescaped quote character)
		 */
		s_end = s;
		while(*s_end != '\0')
		{
		    if(*s_end == '\\')
		    {
			s_end++;
			if(*s_end != '\0')
			    s_end++;
		    }
		    else if(*s_end == '"')
		    {
			break;
		    }
		    else
		    {
			s_end++;
		    }
		}

		/* Calculate length of this argument and append it to
		 * the return list of strings
		 */
		len = s_end - s;
		if(len >= 0)
		{
		    APPEND_STRING(s, len);
		    STRIP_BACKSLASHES(s_cur);
		}

		/* Seek to next argument */
		s = s_end;
		if(*s == '"')
		    s++;
		while(ISBLANK(*s))
		    s++;
	    }
	    else
	    {
		/* Seek s_end to end of current argument */
		s_end = s;
		while(!ISBLANK(*s_end) && (*s_end != '\0'))
		    s_end++;

		/* Calculate length of this argument and append it to
		 * the return list of strings
		 */
		len = s_end - s;
		if(len >= 0)
		    APPEND_STRING(s, s_end - s);

		/* Seek to next argument */
		s = s_end;
		while(ISBLANK(*s))
		    s++;
	    }
	}

	/* Update return */
	if(strc != NULL)
	    *strc = total;

	return(strv);
#undef APPEND_STRING
#undef STRIP_BACKSLASHES
}


/*
 *      Returns true if the process is still running.
 */
int ExecProcessExists(pid_t pid)
{
#ifdef __linux__
	char path[PATH_MAX + NAME_MAX];
	sprintf(path, "/proc/%i", pid);
	return(access(path, F_OK) ? 0 : 1);
#else
	struct sched_param sp;

	/* The pid cannot be 0, since that means "itself" to
	 * sched_getparam()
	 */
	if(pid == 0)
	    return(0);

	if(sched_getparam(
	    pid,
	    &sp
	) == 0)
	    return(1);
	else
	    return(0);
#endif
}


/*
 *	Returns the CPU load as a coefficient value from 0.0 to 1.0.
 *
 *	If cpu_num is -1 then the default cpu is used.
 */
float ExecCPUGetLoad(int cpu_num)
{
	static int n = 0;
	static int ct[2][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
	int i, t;
	float v;
	char *s = NULL, cpu_name[40], buf[256];
	int d[4];
	FILE *fp;

	/* Get cpu name */
	if(cpu_num > -1)
	    sprintf(cpu_name, "cpu%i", cpu_num);
	else
	    strcpy(cpu_name, "cpu");

	/* Open stat file for reading */
	fp = FOpen("/proc/stat", "rb");
	if(fp == NULL)
	    return(0);

	/* Read each line until we get to the one that contains the
	 * cpu information that we want
	 */
	while(fgets(buf, sizeof(buf), fp) != NULL)
	{
	    /* This line starts with cpu_name? */
	    if(strpfx(buf, cpu_name))
	    {
		s = buf + STRLEN(cpu_name);
		while(ISBLANK(*s))
		    s++;
		break;
	    }
	}
	FClose(fp);

	if(s == NULL)
	    return(0);

	sscanf(
	    s,
	    "%u %u %u %u",
	    &ct[n][0], &ct[n][1], &ct[n][2], &ct[n][3]
	);

	t = 0;
	for(i = 0; i < 4; i++)
	    t += (d[i] = ABSOLUTE(ct[n][i] - ct[1 - n][i]));
	if(t <= 0)
	    return(0.0f);

	v = (float)(t - d[3]) / (float)t;

	n = 1 - n;

	return(v);
}


/*
 *	Old execute, fork()s off process and uses system() to execute
 *	command.
 *
 *	This function is provided for backwards compatability, but has
 *	security issues because it uses the unsafe system().
 */
void Execute(const char *cmd)
{
	if(cmd == NULL)
	    return;

	/* Set signal to catch finished child proccesses */
	add_signal(SIGCHLD, child_catcher);

	/* Fork off a process */
	switch(fork())
	{
	  /* Forking error */
	  case -1:
	    perror("fork");
	    exit(1);
	    break;

	  /* We are the child: run then command then exit */  
	  case 0:
	    if(system(cmd) == -1)
		exit(-1);
	    else
		exit(0);
	    break;

	  /* We are the parent: do nothing */        
	  default:
	    break;
	}
}


/*              
 *      Main nexus for all Exec*() (non-blocking) functions.
 *
 *	If the given stdout_path and/or stderr_path are not NULL then 
 *	their respective files will be opened and the child's stdout 
 *	and/or stderr streams will be redirected to them.
 *
 *	The current working directory will be changed to the executed
 *	file's parent directory on the child process.
 */
static pid_t ExecNexus(
	const char *cmd,
	const char *stdout_path, const char *stderr_path,
	const char *stdout_mode, const char *stderr_mode
)
{
	int argc;
	char **argv;
	FILE *stdout_fp, *stderr_fp;
	pid_t p;


	if(cmd == NULL)
	    return(0);

	/* Create stdout file (as needed) */
	if(!STRISEMPTY(stdout_path) && !STRISEMPTY(stdout_mode))
	    stdout_fp = FOpen(stdout_path, stdout_mode);
	else
	    stdout_fp = NULL;

	/* Create stderr file (as needed) */
	if(!STRISEMPTY(stderr_path) && !STRISEMPTY(stderr_mode))
	    stderr_fp = FOpen(stderr_path, stderr_mode);
	else
	    stderr_fp = NULL;

	/* Set stdout file stream options */
	if(stdout_fp != NULL)
	{
	    setlinebuf(stdout_fp);
	}
	/* Set stderr file stream options */
	if(stderr_fp != NULL)
	{
	    setlinebuf(stderr_fp);
	}

	/* Explode command string */
	argv = ExecExplodeCommand(cmd, &argc);
	if((argv == NULL) || (argc < 1))
	{
	    FClose(stdout_fp);
	    FClose(stderr_fp);
	    return(0);
	}

	/* Set last argument pointer to be NULL */
	argv = (char **)realloc(argv, (argc + 1) * sizeof(char *));
	if(argv == NULL)
	{
	    FClose(stdout_fp);
	    FClose(stderr_fp);
	    return(0);
	}
	argv[argc] = NULL;

	/* Object specified by command not executable? */
	if(access(argv[0], X_OK))
	{
	    strlistfree(argv, argc);
	    FClose(stdout_fp);
	    FClose(stderr_fp);
	    return(0);
	}

	/* Set signal to catch finished child proccesses */
	add_signal(SIGCHLD, child_catcher);

	/* Fork off a process */
	p = fork();
	switch(p)
	{
	  /* Forking error */
	  case -1:
	    perror("fork");
	    exit(1);
	    break;

	  /* We are the child: run the command then exit */
	  case 0:
	    /* Redirect child's stdout and stderr streams to our
	     * opened output files (if any)
	     */
	    if(stdout_fp != NULL)
		dup2(fileno(stdout_fp), fileno(stdout));
	    if(stderr_fp != NULL)
		dup2(fileno(stderr_fp), fileno(stderr));

	    /* Execute command and arguments */
	    execvp(argv[0], argv);
	    exit(0);
	    break;

	  /* We are the parent: Do nothing */
	  default:
	    break;
	}

	/* Free exploded argument strings and array */
	strlistfree(argv, argc);

	/* Close output files */
	FClose(stdout_fp);
	FClose(stderr_fp);

	/* Return process id of child */
	return(p);
}


pid_t Exec(const char *cmd)
{
	return(ExecNexus(cmd, NULL, NULL, NULL, NULL));
}

pid_t ExecO(const char *cmd, const char *stdout_path)
{
	return(ExecNexus(cmd, stdout_path, NULL, "w", NULL));
}

pid_t ExecOE(const char *cmd, const char *stdout_path, const char *stderr_path)
{
	return(ExecNexus(cmd, stdout_path, stderr_path, "w", "w"));
}

pid_t ExecAO(const char *cmd, const char *stdout_path)
{
	return(ExecNexus(cmd, stdout_path, NULL, "a", NULL));
}

pid_t ExecAOE(const char *cmd, const char *stdout_path, const char *stderr_path)
{
	return(ExecNexus(cmd, stdout_path, stderr_path, "a", "a"));
}


/*
 *      Main nexus for all ExecB*() (blocking) functions.
 *
 *      If the given stdout_path and/or stderr_path are not NULL then
 *      their respective files will be opened and the child's stdout
 *      and/or stderr streams will be redirected to them.
 *
 *      The current working directory will be changed to the executed
 *      file's parent directory on the child process.
 */
static pid_t ExecBNexus(
	const char *cmd,
	const char *stdout_path, const char *stderr_path,
	const char *stdout_mode, const char *stderr_mode,
	int *status
)
{
	int argc;
	char **argv;
	FILE *stdout_fp, *stderr_fp;
	pid_t p;


	if(cmd == NULL)
	    return(0);

	/* Create stdout file (as needed) */
	if(!STRISEMPTY(stdout_path) && !STRISEMPTY(stdout_mode))
	    stdout_fp = FOpen(stdout_path, stdout_mode);
	else
	    stdout_fp = NULL;

	/* Create stderr file (as needed) */
	if(!STRISEMPTY(stderr_path) && !STRISEMPTY(stderr_mode))
	    stderr_fp = FOpen(stderr_path, stderr_mode);
	else
	    stderr_fp = NULL;

	/* Set stdout file stream options */
	if(stdout_fp != NULL)
	{
	    setlinebuf(stdout_fp);
	}
	/* Set stderr file stream options */
	if(stderr_fp != NULL)
	{
	    setlinebuf(stderr_fp);
	}

	/* Explode command string */
	argv = ExecExplodeCommand(cmd, &argc);
	if((argv == NULL) || (argc < 1))
	{
	    FClose(stdout_fp);
	    FClose(stderr_fp);
	    return(0);
	}

	/* Set last argument pointer to be NULL */
	argv = (char **)realloc(argv, (argc + 1) * sizeof(char *));
	if(argv == NULL)
	{
	    FClose(stdout_fp);
	    FClose(stderr_fp);
	    return(0);
	}
	argv[argc] = NULL;

	/* Object specified by command not executable? */
	if(((*(argv[0]) == '/') || (*(argv[0]) == '.')) && 
	   access(argv[0], X_OK)
	)
	{
	    strlistfree(argv, argc);
	    FClose(stdout_fp);
	    FClose(stderr_fp);
	    return(0);
	}

	/* Fork off a process */
	p = fork();
	switch(p)
	{
	  /* Forking error */
	  case -1:
	    perror("fork");
	    exit(1);
	    break;

	  /* We are the child: run the command then exit */
	  case 0:
	    /* Redirect child's stdout and stderr streams to our
	     * opened output files (if any)
	     */
	    if(stdout_fp != NULL)
	        dup2(fileno(stdout_fp), fileno(stdout));
	    if(stderr_fp != NULL)
		dup2(fileno(stderr_fp), fileno(stderr));

	    /* Execute command and arguments */
	    execvp(argv[0], argv);
	    exit(0);
	    break;

	  /* We are the parent: wait for child to finish */
	  default:
	    waitpid(p, status, 0);
	    break;
	}

	/* Free exploded argument strings and array */
	strlistfree(argv, argc);

	/* Close output files */
	FClose(stdout_fp);
	FClose(stderr_fp);

	return(p);
}

pid_t ExecB(const char *cmd)
{
	return(ExecBNexus(cmd, NULL, NULL, NULL, NULL, NULL));
}

pid_t ExecBO(const char *cmd, const char *stdout_path)
{
	return(ExecBNexus(cmd, stdout_path, NULL, "w", NULL, NULL));
}

pid_t ExecBOE(const char *cmd, const char *stdout_path, const char *stderr_path)
{
	return(ExecBNexus(cmd, stdout_path, stderr_path, "w", "w", NULL));
}

pid_t ExecBAO(const char *cmd, const char *stdout_path)
{
	return(ExecBNexus(cmd, stdout_path, NULL, "a", NULL, NULL));
}

pid_t ExecBAOE(const char *cmd, const char *stdout_path, const char *stderr_path)
{
	return(ExecBNexus(cmd, stdout_path, stderr_path, "a", "a", NULL));
}