-
Notifications
You must be signed in to change notification settings - Fork 0
/
qdaemon.c
189 lines (151 loc) · 3.85 KB
/
qdaemon.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
/* File: qdaemon.c
Copyright (C) 2012 David Hauweele <david@hauweele.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#define _BSD_SOURCE
#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <fcntl.h>
static const char * queue;
static const char * command;
static int av_task;
static int nb_task;
static ssize_t qread(int fd, char * restrict buf)
{
ssize_t n;
if((n = read(fd, buf, 4096)) < 0)
err(1, "cannot read queue");
return n;
}
static void qwrite(int fd, const char *buf, ssize_t n)
{
static ssize_t size;
if(!buf) {
if(ftruncate(fd, size) < 0)
err(1, "cannot truncate queue");
size = 0;
} else {
size += n;
if(write(fd, buf, n) != n)
err(1, "cannot write queue");
}
}
static void exec_command(const char *line)
{
switch(fork()) {
case -1:
err(1, "cannot fork");
case 0:
execl(command, command, line, NULL);
err(1, "cannot exec command");
default:
break;
}
}
static int extract(void)
{
char line[4096];
char buf[4096];
ssize_t n;
int fd;
int i;
if(av_task == 0)
return 0;
if((fd = open(queue, O_RDWR)) < 0)
err(1, "cannot open queue");
n = qread(fd, buf);
for(i = 0 ; i < n; i++) {
if(buf[i] == '\n') {
line[i] = '\0';
break;
}
line[i] = buf[i];
}
if(i == n)
return 0;
if(lseek(fd, 0, SEEK_SET) < 0)
err(1, "cannot seek");
qwrite(fd, buf + i + 1, n - i - 1);
while((n = qread(fd, buf)))
qwrite(fd, buf + i, n - i - 1);
qwrite(fd, NULL, 0);
close(fd);
exec_command(line);
av_task--;
return 1;
}
static void sig_child(int signum)
{
while(waitpid(-1, NULL, WNOHANG) > 0) {
av_task++;
if(av_task > nb_task)
av_task = nb_task;
}
kill(getpid(), SIGUSR1);
}
static void sig_alrm(int signum)
{
while(extract());
}
static void sig_term(int signum)
{
exit(EXIT_SUCCESS);
}
static void init(int argc, char * const argv[])
{
int wakeup_time;
struct sigaction tim = { .sa_handler = sig_alrm };
struct sigaction chd = { .sa_handler = sig_child };
struct sigaction clr = { .sa_handler = sig_term,
.sa_flags = 0 };
if(argc != 5)
errx(1, "expect options [wake-up-time] [par-task] [queue] [command]");
wakeup_time = atoi(argv[1]);
nb_task = atoi(argv[2]);
queue = argv[3];
command = argv[4];
av_task = nb_task;
if(wakeup_time < 0)
errx(1, "invalid wake-up time");
if(nb_task < 1 || nb_task > 8092)
errx(1, "invalid simultaneous tasks number");
else if(nb_task > 16)
warn("too many simultaneous tasks");
sigfillset(&clr.sa_mask);
sigaction(SIGTERM, &clr, NULL);
sigaction(SIGINT, &clr, NULL);
sigaction(SIGALRM, &tim, NULL);
sigaction(SIGUSR1, &tim, NULL);
sigaction(SIGCHLD, &chd, NULL);
if(wakeup_time > 0) {
struct itimerval timer;
timer.it_value.tv_sec = timer.it_interval.tv_sec = wakeup_time;
timer.it_value.tv_usec = timer.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &timer, NULL);
}
}
int main(int argc, char * const argv[])
{
init(argc, argv);
while(1)
pause();
return 1;
}