aboutsummaryrefslogtreecommitdiff
path: root/src/job.c
blob: 1baa0af80a569f5ed2cbd24772543e44905d5b19 (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
/* wydawca - automatic release submission daemon
   Copyright (C) 2009 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"
#include "mail.h"

#define STATE_FINISHED 0x01
#define STATE_QUEUED   0x02
#define STATE_ACTIVE   0x04
  
struct job
{
  struct job *next, *prev;
  int state;
  const struct spool *spool;
  uid_t uid;
  pid_t pid;
  time_t timestamp;
  int exit_status;
};

struct job *queue;
size_t jobmax;
size_t jobcnt;

static struct spool fake_spool = { "all spools" };

static int wakeup;

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

struct job *
job_locate (const struct spool *spool, uid_t uid)
{
  struct job *p;
  for (p = queue; p; p = p->next)
    if (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;
}

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

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

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

  if (debug_level)
    logmsg (LOG_DEBUG, _("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;
    }
  
  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);
      for (i = getdtablesize (); i > 2; i--)
	close (i);
      exit (wydawca_scanner (job) ? WYDAWCA_EX_AGAIN : 0);
    }
  else if (pid == -1)
    {
      logmsg (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;

  if (debug_level)
    logmsg (LOG_DEBUG, _("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 (const struct spool *spool, uid_t uid)
{
  struct job *job;

  if (!spool)
    spool = &fake_spool;
  
  if (debug_level)
    logmsg (LOG_DEBUG, _("scheduling job: %s, %lu"),
	    spool->tag, (unsigned long)uid);
  
  job = job_locate (spool, uid);
  if (!job)
    {
      job = xzalloc (sizeof (*job));
      job->spool = spool;
      job->uid = uid;
      job->pid = -1;
      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))
    {
      if (WEXITSTATUS (status) == 0)
	{
	  if (debug_level)
	    logmsg (LOG_DEBUG,
		    _("%lu (%s, %s) exited successfully"),
		    (unsigned long) job->pid, job->spool->tag, pw->pw_name);
	}
      else
	logmsg (LOG_ERR,
		_("%lu (%s, %s) failed with status %d"),
		(unsigned long) job->pid, job->spool->tag, pw->pw_name,
		WEXITSTATUS (status));
    }
  else if (WIFSIGNALED (status))
    {
      int prio;
      if (expect_term && WTERMSIG (status) == SIGTERM)
	{
	  if (!debug_level)
	    return;
	  prio = LOG_DEBUG;
	}
      else
	prio = LOG_ERR;

      logmsg (prio,
	      _("%lu (%s, %s) terminated on signal %d"),
	      (unsigned long)job->pid, job->spool->tag,
	      pw->pw_name, WTERMSIG (status));
    }
  else if (WIFSTOPPED (status))
    logmsg (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))
    logmsg (LOG_NOTICE,
	    _("%lu (%s, %s) dumped core"),
	    (unsigned long)job->pid, job->spool->tag, pw->pw_name);
#endif
  else
    logmsg (LOG_ERR,
	    _("%lu (%s, %s) terminated with unrecognized status"),
	    (unsigned long)job->pid, job->spool->tag, pw->pw_name);
}

void
job_queue_runner ()
{
  int status;
  struct job *job;

  if (!wakeup)
    return;
  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;
	      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)
		/* Re-queue the job */
		job->state = STATE_QUEUED;
	      else
		job_remove (job);
	    }
	}
      if (job->state == STATE_QUEUED)
	if (job_start (job))
	  pause ();
      job = next;
    }	    
}

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

Return to:

Send suggestions and report system problems to the System administrator.