summaryrefslogtreecommitdiffabout
path: root/src/job.c
blob: 80749c661420973d8872df817f504fafbd350f06 (plain)
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
/* wydawca - automatic release submission daemon
   Copyright (C) 2009, 2010, 2011, 2012, 2013, 2017, 2019 Sergey
   Poznyakoff

   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/>. */

#include "wydawca.h"

#define STATE_FINISHED 0x01
#define STATE_QUEUED   0x02
#define STATE_ACTIVE   0x04

#define STATE_ANY (STATE_FINISHED|STATE_QUEUED|STATE_ACTIVE)

struct job {
	struct job *next, *prev;
	int state;
	struct spool *spool;
	struct wy_triplet *triplet_list;
	uid_t uid;
	pid_t pid;
	time_t timestamp;
	int exit_status;
};

struct job *queue;
size_t jobmax;
size_t jobcnt;

struct spool fake_spool = { "all spools" }, inotify_spool = { "inotify"};

static int wakeup;

RETSIGTYPE
queue_signal(int sig)
{
	wakeup = 1;
	signal(sig, queue_signal);
}

void
set_timer(time_t interval)
{
	wakeup = 0;
	if (interval)
		alarm(interval);
}

void
triplet_jq_unlink(struct wy_triplet *tp)
{
	if (!tp || !tp->job)
		return;
	if (tp->prev)
		tp->jq_prev->jq_next = tp->jq_next;
	else
		tp->job->triplet_list = tp->jq_next;

	if (tp->jq_next)
		tp->jq_next->jq_prev = tp->jq_prev;

	tp->jq_next = tp->jq_prev = NULL;
}

void
jq_clear(struct job *job)
{
	struct wy_triplet *tp;
	
	if (!job->triplet_list)
		return;
	for (tp = job->triplet_list; tp; ) {
		struct wy_triplet *next = tp->jq_next;
		remove_triplet(tp, 1);
		tp = next;
	}
	job->triplet_list = NULL;
}
	
struct job *
job_locate(const struct spool *spool, uid_t uid, int mask)
{
	struct job *p;
	for (p = queue; p; p = p->next)
		if ((p->state & mask) && p->spool == spool && p->uid == uid)
			break;
	return p;
}

size_t
job_active_count()
{
	struct job *job;
	size_t count = 0;
	for (job = queue; job; job = job->next)
		if (job->state & STATE_ACTIVE)
			count++;
	return count;
}

static int
procspool(struct spool *spool, void *data)
{
	struct wy_triplet *trp = *(struct wy_triplet **)data;

	if (!trp)
		return 0;
	timer_start("spool");
	timer_start(spool->tag);
	spool_commit_triplets(spool, trp);
	timer_stop(spool->tag);
	timer_stop("spool");
	return 0;
}

int
wydawca_scanner(struct job *job)
{
	int rc;
	initstats();
	timer_start("wydawca");
	spool_create_timers();
	if (job->spool == &inotify_spool)
		rc = for_each_spool(procspool, &job->triplet_list);
	else if (job->spool == &fake_spool)
		rc = scan_all_spools(1, &job->uid);
	else {
		rc = scan_spool(job->spool, 1, &job->uid);
	}
	timer_stop("wydawca");
	logstats();
	notify_flush(job->spool);
	return rc;
}

int
job_start(struct job *job)
{
	pid_t pid;

	if (jobmax && jobcnt == jobmax) {
		wy_log(LOG_NOTICE, "maximum number of processes active");
		return 1;
	}

	wy_debug(1, (_("starting job: %s, %lu"),
		     job->spool->tag, (unsigned long)job->uid));

	if (single_process) {
		if (wydawca_scanner(job))
			job->state = STATE_QUEUED;
		else
			job->state = STATE_FINISHED;
		wakeup = 1;
		return 0;
	}

	pid = fork();
	if (pid == 0) {
		int i;
		signal(SIGHUP, SIG_DFL);
		signal(SIGTERM, SIG_DFL);
		signal(SIGQUIT, SIG_DFL);
		signal(SIGINT, SIG_DFL);
		signal(SIGCHLD, SIG_DFL);
		signal(SIGALRM, SIG_DFL);
		alarm(0);
		for (i = getdtablesize(); i > 2; i--)
			close(i);
		exit(wydawca_scanner(job) ? WYDAWCA_EX_AGAIN : 0);
	} else if (pid == -1) {
		wy_log(LOG_CRIT, "fork: %s", strerror(errno));
		return -1;
	} else {
		job->state = STATE_ACTIVE;
		job->pid = pid;
		jobcnt++;
	}
	return 0;
}

void
job_remove(struct job *job)
{
	struct job *p;

	wy_debug(1, (_("removing job: %s, %lu"),
		     job->spool->tag, (unsigned long)job->uid));
	p = job->prev;
	if (p)
		p->next = job->next;
	else
		queue = job->next;

	p = job->next;
	if (p)
		p->prev = job->prev;
}

void
job_insert(struct job *job, struct job *elt)
{
	struct job *p;

	job->prev = elt;
	if (!elt) {
		if (queue)
			queue->prev = job;
		job->next = queue;
		queue = job;
		return;
	}

	p = elt->next;
	elt->next = job;

	if (p)
		p->prev = job;
}

void
schedule_job(struct spool *spool, uid_t uid, struct wy_triplet *tp)
{
	struct job *job;

	if (!spool)
		spool = &fake_spool;

	wy_debug(1, (_("scheduling job: %s, %lu"),
		     spool->tag, (unsigned long)uid));

	job = job_locate(spool, uid, tp ? STATE_QUEUED : STATE_ANY);
	if (!job) {
		job = grecs_zalloc(sizeof(*job));
		job->spool = spool;
		job->uid = uid;
		job->pid = -1;
		job->triplet_list = tp;
		for (; tp; tp = tp->jq_next)
			tp->job = job;
		time(&job->timestamp);
		job_insert(job, NULL);
	}

	job->state |= STATE_QUEUED;
	job_start(job);
}

static void
print_status(struct job *job, int expect_term)
{
	struct passwd *pw = getpwuid(job->uid);
	int status = job->exit_status;

	if (WIFEXITED(status)) {
		int exit_code = WEXITSTATUS(status);
		if (exit_code == 0) {
			wy_debug(1, (_("%lu (%s, %s) exited successfully"),
				     (unsigned long)job->pid,
				     job->spool->tag,
				     pw->pw_name));
		} else if (exit_code == WYDAWCA_EX_AGAIN) {
			wy_debug(1, (_("%lu (%s, %s) reported tempfail"),
				     (unsigned long)job->pid,
				     job->spool->tag,
				     pw->pw_name));
		} else
			wy_log(LOG_ERR,
			       _("%lu (%s, %s) failed with status %d"),
			       (unsigned long)job->pid, job->spool->tag,
			       pw->pw_name, exit_code);
	} else if (WIFSIGNALED(status)) {
		int prio;
		if (expect_term && WTERMSIG(status) == SIGTERM) {
			if (!wy_debug_level)
				return;
			prio = LOG_DEBUG;
		} else
			prio = LOG_ERR;

		wy_log(prio,
		       _("%lu (%s, %s) terminated on signal %d"),
		       (unsigned long)job->pid, job->spool->tag,
		       pw->pw_name, WTERMSIG(status));
	} else if (WIFSTOPPED(status))
		wy_log(LOG_NOTICE,
		       _("%lu (%s, %s) stopped on signal %d"),
		       (unsigned long)job->pid, job->spool->tag,
		       pw->pw_name, WSTOPSIG(status));
#ifdef WCOREDUMP
	else if (WCOREDUMP(status))
		wy_log(LOG_NOTICE,
		       _("%lu (%s, %s) dumped core"),
		       (unsigned long)job->pid, job->spool->tag, pw->pw_name);
#endif
	else
		wy_log(LOG_ERR,
		       _("%lu (%s, %s) terminated with unrecognized status"),
		       (unsigned long)job->pid, job->spool->tag, pw->pw_name);
}

void
job_queue_runner(time_t min_interval)
{
	int status;
	struct job *job;
	time_t now = time(NULL);

	if (wakeup) {
		wakeup = 0;

		for (;;) {
			pid_t pid = waitpid((pid_t) - 1, &status, WNOHANG);
			if (pid <= 0)
				break;
			for (job = queue; job; job = job->next) {
				if ((job->state & STATE_ACTIVE) &&
				    job->pid == pid) {
					job->state &= ~STATE_ACTIVE;
					job->state |= STATE_FINISHED;
					jq_clear(job);
					job->exit_status = status;
					jobcnt--;
				}
			}
		}

		for (job = queue; job;) {
			struct job *next = job->next;
			if (job->state & STATE_FINISHED) {
				print_status(job, 0);
				if ((job->state &= ~STATE_FINISHED) == 0) {
					if (WIFEXITED(job->exit_status) &&
					    WEXITSTATUS(job->exit_status) ==
					       WYDAWCA_EX_AGAIN) {
						time_t interval = lock_timeout;
						if (interval == 0)
							interval =
							    lock_expire_time;
						/* Re-queue the job */
						job->state = STATE_QUEUED;
						job->timestamp = now + interval;
					} else {
						job_remove(job);
						free(job);
						job = next;
						continue;
					}
				}
			}

			if (job->state == STATE_QUEUED) {
				if (job->timestamp >= now) {
					if (job_start(job))
						pause();
					now = time(NULL);
				} else {
					time_t interval = job->timestamp - now;
					if (min_interval == 0
					    || interval < min_interval)
						min_interval = interval;
				}
			}

			job = next;
		}
	}

	if (min_interval) {
		wy_debug(2, (_("computed interval: %lu"), min_interval));
		set_timer(min_interval);
	}
}

void
job_init()
{
	signal(SIGCHLD, queue_signal);
	signal(SIGALRM, queue_signal);
}

Return to:

Send suggestions and report system problems to the System administrator.