-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
568 lines (527 loc) · 19 KB
/
main.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
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
/*
Task Spooler - a task queue system for the unix user
Copyright (C) 2007-2009 Lluís Batlle i Rossell
Please find the license in the provided COPYING file.
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <sys/time.h>
#include "main.h"
extern char *optarg;
extern int optind, opterr, optopt;
/* Globals */
struct Command_line command_line;
int server_socket;
/* Globals for the environment of getopt */
static char getopt_env[] = "POSIXLY_CORRECT=YES";
static char *old_getopt_env;
static char version[] = "Task Spooler v1.1 - a task queue system for the unix user.\n"
"Copyright (C) 2007-2016 Lluis Batlle i Rossell";
static void default_command_line()
{
command_line.request = c_LIST;
command_line.need_server = 0;
command_line.store_output = 1;
command_line.should_go_background = 1;
command_line.should_keep_finished = 1;
command_line.gzip = 0;
command_line.send_output_by_mail = 0;
command_line.label = 0;
command_line.do_depend = 0;
command_line.depend_on = -1; /* -1 means depend on previous */
command_line.max_slots = 1;
command_line.wait_enqueuing = 1;
command_line.stderr_apart = 0;
command_line.num_slots = 1;
command_line.max_ram = 1;
}
void get_command(int index, int argc, char **argv)
{
command_line.command.array = &(argv[index]);
command_line.command.num = argc - index;
}
static int get_two_jobs(const char *str, int *j1, int *j2)
{
char tmp[50];
char *tmp2;
if(strlen(str) >= 50)
return 0;
strcpy(tmp, str);
tmp2 = strchr(tmp , '-');
if (tmp2 == NULL)
return 0;
/* We change the '-' to '\0', so we have a delimiter,
* and we can access the two strings for the ids */
*tmp2 = '\0';
/* Skip the '\0', and point tmp2 to the second id */
++tmp2;
*j1 = atoi(tmp);
*j2 = atoi(tmp2);
return 1;
}
void parse_opts(int argc, char **argv)
{
int c;
int res;
/* Parse options */
while(1) {
c = getopt(argc, argv, ":VhKgClnfmBEr:t:c:o:p:w:k:u:s:U:i:N:L:dS:D:R:M:");
if (c == -1)
break;
switch(c)
{
case 'K':
command_line.request = c_KILL_SERVER;
command_line.should_go_background = 0;
break;
case 'k':
command_line.request = c_KILL_JOB;
command_line.jobid = atoi(optarg);
break;
case 'l':
command_line.request = c_LIST;
break;
case 'h':
command_line.request = c_SHOW_HELP;
break;
case 'd':
command_line.do_depend = 1;
command_line.depend_on = -1;
break;
case 'V':
command_line.request = c_SHOW_VERSION;
break;
case 'C':
command_line.request = c_CLEAR_FINISHED;
break;
case 'c':
command_line.request = c_CAT;
command_line.jobid = atoi(optarg);
break;
case 'o':
command_line.request = c_SHOW_OUTPUT_FILE;
command_line.jobid = atoi(optarg);
break;
case 'n':
command_line.store_output = 0;
break;
case 'L':
command_line.label = optarg;
break;
case 'g':
command_line.gzip = 1;
break;
case 'f':
command_line.should_go_background = 0;
break;
case 'm':
command_line.send_output_by_mail = 1;
break;
case 't':
command_line.request = c_TAIL;
command_line.jobid = atoi(optarg);
break;
case 'p':
command_line.request = c_SHOW_PID;
command_line.jobid = atoi(optarg);
break;
case 'i':
command_line.request = c_INFO;
command_line.jobid = atoi(optarg);
break;
case 'N':
command_line.num_slots = atoi(optarg);
if (command_line.num_slots < 0)
command_line.num_slots = 0;
break;
case 'R':
command_line.amt_ram = atoi(optarg);
if (command_line.amt_ram < 0)
command_line.amt_ram = 0;
break;
case 'r':
command_line.request = c_REMOVEJOB;
command_line.jobid = atoi(optarg);
break;
case 'w':
command_line.request = c_WAITJOB;
command_line.jobid = atoi(optarg);
break;
case 'u':
command_line.request = c_URGENT;
command_line.jobid = atoi(optarg);
break;
case 's':
command_line.request = c_GET_STATE;
command_line.jobid = atoi(optarg);
break;
case 'S':
command_line.request = c_SET_MAX_SLOTS;
command_line.max_slots = atoi(optarg);
if (command_line.max_slots < 1)
{
fprintf(stderr, "You should set at minimum 1 slot.\n");
exit(-1);
}
break;
case 'M':
command_line.request = c_SET_MAX_RAM;
command_line.max_ram = atoi(optarg);
if (command_line.max_ram < 1)
{
fprintf(stderr, "You should set at minimum 1 slot.\n");
exit(-1);
}
break;
case 'D':
command_line.do_depend = 1;
command_line.depend_on = atoi(optarg);
break;
case 'U':
command_line.request = c_SWAP_JOBS;
res = get_two_jobs(optarg, &command_line.jobid,
&command_line.jobid2);
if (!res)
{
fprintf(stderr, "Wrong <id-id> for -U.\n");
exit(-1);
}
if (command_line.jobid == command_line.jobid2)
{
fprintf(stderr, "Wrong <id-id> for -U. "
"Use different ids.\n");
exit(-1);
}
break;
case 'B':
/* I picked 'B' quite at random among the letters left */
command_line.wait_enqueuing = 0;
break;
case 'E':
command_line.stderr_apart = 1;
break;
case ':':
switch(optopt)
{
case 't':
command_line.request = c_TAIL;
command_line.jobid = -1; /* This means the 'last' job */
break;
case 'c':
command_line.request = c_CAT;
command_line.jobid = -1; /* This means the 'last' job */
break;
case 'o':
command_line.request = c_SHOW_OUTPUT_FILE;
command_line.jobid = -1; /* This means the 'last' job */
break;
case 'p':
command_line.request = c_SHOW_PID;
command_line.jobid = -1; /* This means the 'last' job */
break;
case 'i':
command_line.request = c_INFO;
command_line.jobid = -1; /* This means the 'last' job */
break;
case 'r':
command_line.request = c_REMOVEJOB;
command_line.jobid = -1; /* This means the 'last'
added job */
break;
case 'w':
command_line.request = c_WAITJOB;
command_line.jobid = -1; /* This means the 'last'
added job */
break;
case 'u':
command_line.request = c_URGENT;
command_line.jobid = -1; /* This means the 'last'
added job */
break;
case 's':
command_line.request = c_GET_STATE;
command_line.jobid = -1; /* This means the 'last'
added job */
break;
case 'k':
command_line.request = c_KILL_JOB;
command_line.jobid = -1; /* This means the 'last' job */
break;
case 'S':
command_line.request = c_GET_MAX_SLOTS;
break;
case 'M':
command_line.request = c_GET_MAX_RAM;
break;
default:
fprintf(stderr, "Option %c missing argument.\n",
optopt);
exit(-1);
}
break;
case '?':
fprintf(stderr, "Wrong option %c.\n", optopt);
exit(-1);
}
}
command_line.command.num = 0;
/* if the request is still the default option...
* (the default values should be centralized) */
if (optind < argc && command_line.request == c_LIST)
{
command_line.request = c_QUEUE;
get_command(optind, argc, argv);
}
if (command_line.request != c_SHOW_HELP &&
command_line.request != c_SHOW_VERSION)
command_line.need_server = 1;
if ( ! command_line.store_output && ! command_line.should_go_background )
command_line.should_keep_finished = 0;
if ( command_line.send_output_by_mail && ((! command_line.store_output) ||
command_line.gzip) )
{
fprintf(stderr,
"For e-mail, you should store the output (not through gzip)\n");
exit(-1);
}
}
static void fill_first_3_handles()
{
int tmp_pipe1[2];
int tmp_pipe2[2];
/* This will fill handles 0 and 1 */
pipe(tmp_pipe1);
/* This will fill handles 2 and 3 */
pipe(tmp_pipe2);
close(tmp_pipe2[1]);
}
static void go_background()
{
int pid;
pid = fork();
switch(pid)
{
case -1:
error("fork failed");
break;
case 0:
close(0);
close(1);
close(2);
/* This is a weird thing. But we will later want to
* allocate special files to the 0, 1 or 2 fds. It's
* almost impossible, if other important things got
* allocated here. */
fill_first_3_handles();
setsid();
break;
default:
exit(0);
}
}
static void print_help(const char *cmd)
{
printf("usage: %s [action] [-ngfmdE] [-L <lab>] [-D <id>] [cmd...]\n", cmd);
printf("Env vars:\n");
printf(" TS_SOCKET the path to the unix socket used by the ts command.\n");
printf(" TS_MAILTO where to mail the result (on -m). Local user by default.\n");
printf(" TS_MAXFINISHED maximum finished jobs in the queue.\n");
printf(" TS_MAXCONN maximum number of ts connections at once.\n");
printf(" TS_ONFINISH binary called on job end (passes jobid, error, outfile, command).\n");
printf(" TS_ENV command called on enqueue. Its output determines the job information.\n");
printf(" TS_SAVELIST filename which will store the list, if the server dies.\n");
printf(" TS_SLOTS amount of jobs which can run at once, read on server start.\n");
printf(" TMPDIR directory where to place the output files and the default socket.\n");
printf("Actions:\n");
printf(" -K kill the task spooler server\n");
printf(" -C clear the list of finished jobs\n");
printf(" -l show the job list (default action)\n");
printf(" -S [num] get/set the number of max simultaneous jobs of the server.\n");
printf(" -M [num] get/set the number of max RAM (GB) of the server.\n");
printf(" -t [id] \"tail -n 10 -f\" the output of the job. Last run if not specified.\n");
printf(" -c [id] like -t, but shows all the lines. Last run if not specified.\n");
printf(" -p [id] show the pid of the job. Last run if not specified.\n");
printf(" -o [id] show the output file. Of last job run, if not specified.\n");
printf(" -i [id] show job information. Of last job run, if not specified.\n");
printf(" -s [id] show the job state. Of the last added, if not specified.\n");
printf(" -r [id] remove a job. The last added, if not specified.\n");
printf(" -w [id] wait for a job. The last added, if not specified.\n");
printf(" -k [id] send SIGTERM to the job process group. The last run, if not specified.\n");
printf(" -u [id] put that job first. The last added, if not specified.\n");
printf(" -U <id-id> swap two jobs in the queue.\n");
printf(" -B in case of full queue on the server, quit (2) instead of waiting.\n");
printf(" -h show this help\n");
printf(" -V show the program version\n");
printf("Options adding jobs:\n");
printf(" -n don't store the output of the command.\n");
printf(" -E Keep stderr apart, in a name like the output file, but adding '.e'.\n");
printf(" -g gzip the stored output (if not -n).\n");
printf(" -f don't fork into background.\n");
printf(" -m send the output by e-mail (uses sendmail).\n");
printf(" -d the job will be run only if the job before ends well\n");
printf(" -D <id> the job will be run only if the job of given id ends well.\n");
printf(" -L <lab> name this task with a label, to be distinguished on listing.\n");
printf(" -N <num> number of slots required by the job (1 default).\n");
printf(" -R <ram> RAM size (GB) required by the job (0 GB required default).\n");
}
static void print_version()
{
puts(version);
}
static void set_getopt_env()
{
old_getopt_env = getenv("POSIXLY_CORRECT");
putenv(getopt_env);
}
static void unset_getopt_env()
{
if (old_getopt_env == NULL)
{
/* Wipe the string from the environment */
putenv("POSIXLY_CORRECT");
}
else
sprintf(getopt_env, "POSIXLY_CORRECT=%s", old_getopt_env);
}
int main(int argc, char **argv)
{
int errorlevel = 0;
process_type = CLIENT;
set_getopt_env();
/* This is needed in a gnu system, so getopt works well */
default_command_line();
parse_opts(argc, argv);
unset_getopt_env();
/* This will be inherited by the server, if it's run */
ignore_sigpipe();
if (command_line.need_server)
{
ensure_server_up();
c_check_version();
}
switch(command_line.request)
{
case c_SHOW_VERSION:
print_version(argv[0]);
break;
case c_SHOW_HELP:
print_help(argv[0]);
break;
case c_QUEUE:
if (command_line.command.num <= 0)
error("Tried to queue a void command. parameters: %i",
command_line.command.num);
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_new_job();
command_line.jobid = c_wait_newjob_ok();
if (command_line.store_output)
{
printf("%i\n", command_line.jobid);
fflush(stdout);
}
if (command_line.should_go_background)
{
go_background();
c_wait_server_commands();
} else
{
errorlevel = c_wait_server_commands();
}
break;
case c_LIST:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_list_jobs();
c_wait_server_lines();
break;
case c_KILL_SERVER:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_shutdown_server();
break;
case c_CLEAR_FINISHED:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_clear_finished();
break;
case c_TAIL:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
errorlevel = c_tail();
/* This will not return! */
break;
case c_CAT:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
errorlevel = c_cat();
/* This will not return! */
break;
case c_SHOW_OUTPUT_FILE:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_show_output_file();
break;
case c_SHOW_PID:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_show_pid();
break;
case c_KILL_JOB:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_kill_job();
break;
case c_INFO:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_show_info();
break;
case c_REMOVEJOB:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_remove_job();
break;
case c_WAITJOB:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
errorlevel = c_wait_job();
break;
case c_URGENT:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_move_urgent();
break;
case c_SET_MAX_SLOTS:
c_send_max_slots(command_line.max_slots);
break;
case c_SET_MAX_RAM:
c_send_max_ram(command_line.max_ram);
break;
case c_GET_MAX_SLOTS:
c_get_max_slots();
break;
case c_GET_MAX_RAM:
c_get_max_ram();
break;
case c_SWAP_JOBS:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
c_swap_jobs();
break;
case c_GET_STATE:
if (!command_line.need_server)
error("The command %i needs the server", command_line.request);
/* This will also print the state into stdout */
c_get_state();
break;
}
if (command_line.need_server)
{
close(server_socket);
}
return errorlevel;
}