aboutsummaryrefslogtreecommitdiff
path: root/contrib/ircdwatch/ircdwatch.c
blob: c3845783ab8a685dee61ce30f13c7c5a894968b1 (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
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
/*
 *   Copyright (C) 1998 Bjorn Borud <borud@guardian.no>
 * 
 *   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 1, 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, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#ifndef lint
static  char rcsid[] = "@(#)$Id: ircdwatch.c,v 1.3 1999/02/21 00:33:44 kalt Exp $";
#endif

#include <stdio.h>
#include <stdlib.h>     /* atol() */
#include <unistd.h>     /* fork() exec() */
#include <sys/types.h>
#include <sys/stat.h>   /* stat() */
#include <signal.h>
#include <syslog.h>
#include <string.h>     /* strncmp() */
#include <time.h>

#include "os.h"
#include "config.h"

/* 
 * Try and find the correct name to use with getrlimit() for setting
 * the max.  number of files allowed to be open by this process. 
 * (borrowed from ircd/s_bsd.c)
 */
#ifdef RLIMIT_FDMAX
# define RLIMIT_FD_MAX   RLIMIT_FDMAX
#else
# ifdef RLIMIT_NOFILE
#  define RLIMIT_FD_MAX RLIMIT_NOFILE
# else
#  ifdef RLIMIT_OPEN_MAX
#   define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
#  else
#   undef RLIMIT_FD_MAX
#  endif
# endif
#endif

#define PID_LEN 7 /* overkill, but hey */
#define MAX_OPEN_FILEDESCRIPTORS_WILD_GUESS 256

static int want_to_quit = 0;

void finalize(int i)
{
#ifdef IRCDWATCH_USE_SYSLOG
  syslog(LOG_NOTICE, "ircdwatch daemon exiting");
  closelog();
#endif
  exit(i);
}

int daemonize(void)
{
  pid_t pid;
  int   i;
  int   open_max;

#ifdef RLIMIT_FD_MAX
  struct rlimit rlim;

  if (getrlimit(RLIMIT_FD_MAX, &rlim) != 0) {
    perror("getrlimit");
    finalize(1);
  }

  /* if we get a lame answer we just take a wild guess */
  if (rlim.rlim_max == 0) {
    open_max = MAX_OPEN_FILEDESCRIPTORS_WILD_GUESS;
  }

  open_max = rlim.rlim_max;

#else

  /* no RLIMIT_FD_MAX?  take a wild guess */
  open_max = MAX_OPEN_FILEDESCRIPTORS_WILD_GUESS;

#endif

  pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(1);
  }

  /* parent process dies */
  if (pid != 0) {
    exit(0);
  }

  setsid();
  umask(0);

  /* close file descriptors */
  for (i=0; i < open_max; i++) {
    close(i);
  }

  return(0);
}

static void sig_handler (int signo) 
{
  if (signo == SIGHUP) {
    want_to_quit = 1;
    return;
  }

  if (signo == SIGALRM) {

#ifndef POSIX_SIGNALS
    (void)signal(SIGALRM, &sig_handler);
#endif;

    return;
  }
}


void set_up_signals(void) 
{
#ifdef POSIX_SIGNALS
  struct sigaction act;

  act.sa_handler = sig_handler;
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;

  if (sigaction(SIGHUP, &act, NULL) < 0) {
    perror("sigaction");
  }

  if (sigaction(SIGALRM, &act, NULL) < 0) {
    perror("sigaction");
  }
#else
  (void)signal(SIGHUP, &sig_handler);
  (void)signal(SIGALRM, &sig_handler);
#endif

  /* ignore it if child processes die */
  signal(SIGCHLD, SIG_IGN);
}

int write_my_pid(void)
{
  FILE *f;

  f = fopen(IRCDWATCH_PID_FILENAME, "w");
  if (f == NULL) {
    return(-1);
  }

  fprintf(f, "%d\n", getpid());
  fclose(f);

  return(0);
}


int file_modified(char *s)
{
  struct stat st;
  
  if (stat(s, &st) < 0) {
    return(-1);
  }
  return(st.st_ctime);
}


int spawn (char *cmd) 
{
  pid_t pid;

  pid = fork();

  if (pid == -1) {
#ifdef IRCDWATCH_USE_SYSLOG
    syslog(LOG_ERR, "spawn() unable to fork, errno=%d", errno);
#endif
    return(-1);
  }

  if (pid == 0) {
    execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
    _exit(127);
  }
  return(0);
}

int read_pid(char *pid_filename) 
{
  FILE *f;
  char pidbuf[PID_LEN];
  pid_t pid;

  f = fopen(pid_filename, "r");
  if (f == NULL) {
#ifdef IRCDWATCH_USE_SYSLOG
    syslog(LOG_ERR, "unable to fopen() %s: %s", pid_filename, strerror(errno));
#endif
    return(-1);
  }

  if (fgets(pidbuf, PID_LEN, f) != NULL) {
    pid = atol(pidbuf);
  } else {
#ifdef IRCDWATCH_USE_SYSLOG
    syslog(LOG_ERR, "fgets() %s: %s", pid_filename, strerror(errno));
#endif
    fclose(f);
    return(-1);
  }

  fclose(f);
  return(pid);
}

int file_exists (char *s)
{
  struct stat st;
  if ((stat(s, &st) < 0) && (errno == ENOENT)) {
    return(0);
  }
  return(1);
}

/* yeah, I'll get around to these in some later version */

int file_readable (char *s)
{
  return(access(s, R_OK) == 0);
}

int file_writable (char *s)
{
  return(access(s, W_OK) == 0);
}

int file_executable (char *s)
{
  int rc;
  rc = (access(IRCD_PATH, X_OK) == 0);
  return rc;
}

int verify_pid(int pid) 
{
  int res;

  res = kill(pid, 0);
  if (res < 0 && errno == EPERM) {
    fprintf(stderr, "Not process owner\n");
    exit(1);
  }

  return(res == 0);
}

int ircdwatch_running () {
  int pid;

  if (file_exists(IRCDWATCH_PID_FILENAME)) {
    pid = read_pid(IRCDWATCH_PID_FILENAME);
    if (pid > 0) {
      return(verify_pid(pid) == 1);
    } else {
      return(-1);
    }
  }
  return(0);
}

int ircd_running () {
  int pid;

  if (file_exists(IRCDPID_PATH)) {
    pid = read_pid(IRCDPID_PATH);
    if (pid > 0) {
      return(verify_pid(pid) == 1);
    } else {
      return(-1);
    }
  }
  return(0);
}

void hup_ircd ()
{
  int pid;
  int res;
  
  if (file_exists(IRCDPID_PATH)) {
    pid = read_pid(IRCDPID_PATH);
    if (pid > 0) {
      res = kill(pid, SIGHUP);
      if (res < 0 && errno == EPERM) {
#ifdef IRCDWATCH_USE_SYSLOG
	syslog(LOG_ERR, "not allowed to send SIGHUP to ircd");
#endif
	finalize(1);
      }
    }
  }
}


void daemon_run () 
{
  int i;
  int last_config_time = 0;

  /* is ircdwatch already running? */
  i = ircdwatch_running();
  if (i == -1) {
    /* unable to open pid file.  wrong user? */
    fprintf(stderr, "ircdwatch pid file exists but is unreadable\n");
    exit(1);
  } else if (i) {
    fprintf(stderr, "ircdwatch is already running\n");    
    exit(0);
  }

  /* is ircd running? */
  i = ircd_running();
  if (i == -1) {
    /* unable to open pid file.  wrong user? */
    fprintf(stderr, "ircdwatch pid file exists but is unreadable\n");
    exit(1);
  } else  if (!i) {
    fprintf(stderr, "ircd not running. attempting to start ircd...\n");
    if (file_exists(IRCD_PATH)) {
      if (file_executable(IRCD_PATH)) {
	spawn(IRCD_PATH);
      } else {
	fprintf(stderr, "%s not executable\n", IRCD_PATH);
	exit(1);
      }
    } else {
      fprintf(stderr, "%s does not exist\n", IRCD_PATH);
      exit(1);      
    }
  }

  set_up_signals();
  closelog();
  /*  daemonize(); */

#ifdef IRCDWATCH_USE_SYSLOG
  openlog(IRCDWATCH_SYSLOG_IDENT, 
	  IRCDWATCH_SYSLOG_OPTIONS, 
	  IRCDWATCH_SYSLOG_FACILITY);
  
  syslog(LOG_NOTICE, "starting ircdwatch daemon");
#endif

  alarm(IRCDWATCH_POLLING_INTERVAL);
  pause();

  while (!want_to_quit) {
    if (! ircd_running() ) {

#ifdef IRCDWATCH_USE_SYSLOG
      syslog(LOG_ERR, "spawning %s", IRCD_PATH);
#endif

      spawn(IRCD_PATH);
    }

#ifdef IRCDWATCH_HUP_ON_CONFIG_CHANGE
    i = file_modified(IRCDCONF_PATH);
    if (i != -1) {

      if (last_config_time == 0) {
	last_config_time = i;
      } 

      else if (i > last_config_time) {
	last_config_time = i;	
	hup_ircd();

#ifdef IRCDWATCH_USE_SYSLOG
	syslog(LOG_NOTICE, "config change, HUPing ircd");
#endif

      }
    }
#endif

    alarm(IRCDWATCH_POLLING_INTERVAL);
    pause();
  }
  return;
}

void kill_ircd ()
{
  int pid;
  int res;
  
  if (file_exists(IRCDPID_PATH)) {
    pid = read_pid(IRCDPID_PATH);
    if (pid > 0) {
      res = kill(pid, SIGTERM);
      if (res < 0) {
	perror("ircd kill");
	finalize(1);
      } else {
	fprintf(stderr, "killed ircd\n");
      }
    }
  } else {
    fprintf(stderr, "File %s does not exist\n", IRCDPID_PATH);
  }
}

void kill_ircdwatch ()
{
  int pid;
  int res;
  
  if (file_exists(IRCDWATCH_PID_FILENAME)) {
    pid = read_pid(IRCDWATCH_PID_FILENAME);
    if (pid > 0) {
      res = kill(pid, SIGHUP);
      if (res < 0) {
	perror("ircdwatch kill");
	finalize(1);
      } else {
	fprintf(stderr, "Sent HUP to ircdwatch.  it will die soon...\n");
      }
    }
  } else {
    fprintf(stderr, "File %s does not exist\n", IRCDWATCH_PID_FILENAME);
  }
}


void usage (void)
{
  fprintf(stderr,"\n\
Usage:\n\
  ircdwatch [--kill | --rest | --help]\n\
\n\
     --kill, stop both ircdwatch and ircd\n\
     --rest, stop ircdwatch but let ircd alone\n\
     --help, display this text\n\
\n\
%s\n", rcsid);
}

int
main (int argc, char **argv) {
  int i;

#ifdef IRCDWATCH_USE_SYSLOG
  openlog(IRCDWATCH_SYSLOG_IDENT, 
	  IRCDWATCH_SYSLOG_OPTIONS, 
	  IRCDWATCH_SYSLOG_FACILITY);
#endif

  if (argc > 1) {
    if (strncmp(argv[1], "--rest", 6) == 0) {
      kill_ircdwatch();
      exit(0);
    }

    if (strncmp(argv[1], "--kill", 6) == 0) {
      kill_ircdwatch();
      kill_ircd();
      exit(0);
    }

    usage();
    exit(0);
  }

  daemon_run();
  finalize(0);
}