-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun4.c
189 lines (162 loc) · 4.54 KB
/
run4.c
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
/*
* Please see the README.textile for info about this tool.
*
* -----------------------------------------------------------------
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar
* 14 rue de Plaisance, 75014 Paris, France
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
* -----------------------------------------------------------------
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
* -----------------------------------------------------------------
*
* Enjoy,
* jazzyb
*/
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <ctype.h>
#define ARG_MAX 10000 // totally arbitrary
pid_t cpid;
void
usage (void)
{
fprintf(stderr, "usage: run4 <number> {seconds|minutes|hours} <command>\n"
" * note that if command is more than one word, it should be quoted\n"
" ex: run4 30 minutes 'tshark -i eth0' > sniff.log\n");
}
int
convert_to_seconds (char *number, char *measure)
{
int secs = (int)strtol(number, (char **)NULL, 10);
if (secs < 0) {
fprintf(stderr, "error: number cannot be negative\n");
usage();
exit(1);
}
if (strcmp(measure, "hours") == 0) {
secs *= 3600;
} else if (strcmp(measure, "minutes") == 0) {
secs *= 60;
} else if (strcmp(measure, "seconds") != 0) {
fprintf(stderr, "error: unknown measure of time `%s'\n", measure);
usage();
exit(1);
}
return secs;
}
inline int isquote (char c) { return (c == '\'' || c == '"'); }
inline int isseparator (char c) { return (isspace(c) || isquote(c) || c == '\0'); }
/*
* Sets the given char in the string to an '\0' char to end the previous string
* in the argument list. Then adds the next non-empty string to the argument
* list.
*/
inline int
add_arg_to_list (char *cmd, int i, char **args, int *count)
{
cmd[i] = '\0';
if (!isseparator(cmd[i+1])) { // the next string isn't empty
args[++(*count)] = cmd + i + 1;
}
}
/*
* Set each of the char*'s in args to point to each of the words in cmd,
* ignoring empty strings and preserving quoted strings.
*/
void
convert_to_argument_list (char **args, char *cmd)
{
int i;
int count;
int size;
char within_quotes;
args[0] = cmd;
within_quotes = 0;
size = strlen(cmd);
for (count = i = 0; i < size; i++) {
if (isquote(cmd[i])) {
if (cmd[i] == within_quotes) {
within_quotes = 0;
add_arg_to_list(cmd, i, args, &count);
} else if (!within_quotes) {
within_quotes = cmd[i];
add_arg_to_list(cmd, i, args, &count);
}
} else if (isspace(cmd[i]) && !within_quotes) {
add_arg_to_list(cmd, i, args, &count);
if (count > ARG_MAX) {
fprintf(stderr, "warning: the command exceeds the maximum number of\n"
" arguments (%d) allowed by run4; truncating\n",
ARG_MAX);
break;
}
}
}
args[count+1] = NULL;
}
void
kill_child (void)
{
int dummy;
kill(cpid, SIGINT);
sleep(1);
kill(cpid, SIGKILL);
waitpid(cpid, &dummy, WNOHANG);
}
void
sighandler (int sig)
{
kill_child();
exit(0);
}
int
main (int argc, char *argv[])
{
int i;
int time;
char *args[ARG_MAX+1];
if (argc != 4) {
usage();
if (argc == 2 && strcmp(argv[1], "-h") == 0) {
exit(0);
} else {
exit(1);
}
}
time = convert_to_seconds(argv[1], argv[2]);
convert_to_argument_list(args, argv[3]);
cpid = fork();
if (cpid == 0) {
execvp(args[0], args);
// if execvp() returns, then something failed:
fprintf(stderr, "error: unable to exec the command\n");
exit(1);
} else if (cpid > 0) {
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
sleep(time);
kill_child();
} else {
fprintf(stderr, "error: unable to fork\n");
exit(1);
}
return 0;
}