Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / bozo / test / testproc.c
CommitLineData
805e021f
CE
1/*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10#include <sys/types.h>
11#include <signal.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16static int ignore = 0;
17static int sleepTime = 10;
18static int run = 1;
19static char *file = NULL;
20
21void
22trim(char *s)
23{
24 char *p = strchr(s, '\n');
25 if (p) {
26 *p = '\0';
27 }
28}
29
30int
31readfile(void)
32{
33 FILE *fp;
34 int *bogus = NULL;
35 char buffer[256];
36 if (file) {
37 if ((fp = fopen(file, "r")) == NULL) {
38 fprintf(stderr, "Unable to open file %s\n", file);
39 exit(-1);
40 }
41 fgets(buffer, sizeof(buffer), fp);
42 trim(buffer);
43 if (strncmp(buffer, "sleep ", 6) == 0) {
44 int t = atoi(buffer + 6);
45 if (t) {
46 sleepTime = t;
47 }
48 }
49 if (strcmp(buffer, "run") == 0) {
50 run = 1;
51 }
52 if (strcmp(buffer, "return") == 0) {
53 run = 0;
54 sleepTime = 0;
55 }
56 if (strcmp(buffer, "exit") == 0) {
57 exit(1);
58 }
59 if (strcmp(buffer, "crash") == 0) {
60 *bogus = 1; /* intentional */
61 exit(2); /* should not reach */
62 }
63 fclose(fp);
64 }
65}
66
67void
68sigproc(int signo)
69{
70 printf("testproc received signal\n");
71 if (ignore)
72 return;
73 exit(0);
74}
75
76void
77sigreload(int signo)
78{
79 readfile();
80 return;
81}
82
83
84int
85main(int argc, char **argv)
86{
87 int i;
88
89#ifdef AFS_AIX31_ENV
90 /*
91 * The following signal action for AIX is necessary so that in case of a
92 * crash (i.e. core is generated) we can include the user's data section
93 * in the core dump. Unfortunately, by default, only a partial core is
94 * generated which, in many cases, isn't too useful.
95 */
96 struct sigaction nsa;
97
98 sigemptyset(&nsa.sa_mask);
99 nsa.sa_handler = SIG_DFL;
100 nsa.sa_flags = SA_FULLDUMP;
101 sigaction(SIGSEGV, &nsa, NULL);
102#endif
103 signal(SIGTERM, sigproc);
104 signal(SIGQUIT, sigproc);
105 signal(SIGHUP, sigreload);
106
107 for (i = 1; i < argc; i++) {
108 if (strcmp(argv[i], "-ignore") == 0) {
109 ignore = 1;
110 } else if (strcmp(argv[i], "-sleep") == 0) {
111 sleepTime = atoi(argv[i + 1]);
112 i++;
113 } else if (strcmp(argv[i], "-file") == 0) {
114 file = argv[i + 1];
115 i++;
116 } else {
117 printf("unrecognized option '%s', try one of\n", argv[i]);
118 printf("-ignore ignore SIGTERM signal\n");
119 printf("-sleep <n> sleep N seconds before exiting\n");
120 printf("-file <file> read file for next action\n");
121 return 1;
122 }
123 }
124
125 while (run) {
126 if (file) {
127 readfile();
128 } else {
129 run = 0;
130 }
131 if (sleepTime) {
132 sleep(sleepTime);
133 }
134 }
135
136 return 0;
137}