updateLysKOM.c 4 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
/*
2
 * $Id: updateLysKOM.c,v 1.2 1994/04/04 18:30:16 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
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
 * Copyright (C) 1994  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */

#include <stdio.h>
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifdef HAVE_STRING_H
#  include <string.h>
#else
#  ifdef HAVE_STRINGS_H
#    include <strings.h>
#  endif
#endif
#ifndef HAVE_STRCHR
#  define strchr index
#endif
#include <time.h>
#include <unistd.h>

48
#include "ldifftime.h"
Per Cederqvist's avatar
Per Cederqvist committed
49
50

void
51
52
53
checkstatus(FILE *fp,
	    long pid,
	    char *progname)
Per Cederqvist's avatar
Per Cederqvist committed
54
55
56
57
58
59
60
61
62
63
64
{
    char lbuf[80];
    char buf2[300];
    struct stat sbuf;

    if (fstat(fileno(fp), &sbuf) < 0)
    {
	perror("updateLysKOM: stat failed");
	exit(1);
    }
    
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    if (kill(pid, SIGHUP) != 0)
    {
	if (errno != ESRCH)
	{
	    fprintf(stderr, "%s: kill(%ld, SIGHUP) failed", progname, pid);
	    perror("");
	    exit(1);
	}
    }
    else
    {
	/* The signal got through... */
	if (ldifftime(time(NULL), sbuf.st_mtime) > 1260) /* cron 10 minutes */
	{
	    /* ...more than once.  Strange. */
	    fprintf(stderr, "%s: Told lyskomd to shut down.  Twice.", 
		    progname);
	    exit(1);
	}
    }
    
    if (ldifftime(time(NULL), sbuf.st_mtime) < 3600)
Per Cederqvist's avatar
Per Cederqvist committed
87
88
89
    {
	/* NOP */
    }
90
    else if (ldifftime(time(NULL), sbuf.st_mtime) < 2*3600)
Per Cederqvist's avatar
Per Cederqvist committed
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
    {
	/* Send mail to creator of file, the first line in the file. */

	if (fgets(lbuf, 80, fp) == NULL)
	{
	    perror("updateLysKOM: fgets failed");
	    exit(1);
	}
	if (strchr(lbuf, '\n'))
	    *strchr(lbuf, '\n') = '\0';
	sprintf(buf2, "echo 'Reminder: LysKOM is still not running.'|Mail %s",
		lbuf);
	if (system(buf2) != 0)
	{
	    perror("updateLysKOM: system failed");
	    exit(1);
	}
    }
    else
    {
	if (system("echo 'Note: LysKOM is down' | Mail ceder") != 0)
	{
	    perror("updateLysKOM: system failed (2)");
	    exit(1);
	}
    }
}
	

int
main (int    argc,
      char **argv)
{
    int i;
    FILE *fp;
126
    long pid;
Per Cederqvist's avatar
Per Cederqvist committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140

    if (getuid() == 0 || geteuid() == 0)
    {
	fprintf(stderr, "%s: this program should run as lyskom, not root\n",
		argv[0]);
	exit(1);
    }

    /* Parse command line arguments. */
    for (i = 1; i < argc && argv[i][0] == '-'; i++)
	switch (argv[i][1])
	{
	case 'V':
	case 'v':
141
142
143
	    fprintf(stderr, "updateLysKOM %d $Revision: 1.2 $\n",
#include "version.incl"
		    );
Per Cederqvist's avatar
Per Cederqvist committed
144
145
146
147
148
149
	    exit(0);

	default:
	    fprintf(stderr, "usage: %s [-V]\n", argv[0]);
	}

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    fp = fopen(DEFAULT_PREFIX "/etc/pid", "r");
    if (fp == NULL)
    {
	fprintf(stderr, "%s: %s: ", argv[0], DEFAULT_PREFIX "/etc/pid");
	perror("");
	exit(1);
    }

    pid = 0;
    fscanf(fp, "%lu", &pid);
    if (pid < 2)
    {
	fprintf(stderr, "%s: got pid %ld.\n", argv[0], pid);
	exit(1);
    }
	
Per Cederqvist's avatar
Per Cederqvist committed
166
167
168
    fp = fopen(DEFAULT_PREFIX "/etc/status", "r");
    if (fp != NULL)
    {
169
	checkstatus(fp, pid, argv[0]);
Per Cederqvist's avatar
Per Cederqvist committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    }
    else
    {
	if (kill(pid, SIGUSR1) != 0)
	{
	    if (errno != ESRCH)
	    {
		fprintf(stderr, "%s: kill(%ld, SIGUSR1) failed", argv[0], pid);
		perror("");
		exit(1);
	    }
	    execl(DEFAULT_PREFIX "/bin/lyskomd", "lyskomd", (char *)0);
	    fprintf(stderr, "%s: execl() failed: ", argv[0]);
	    perror("");
	    exit(1);
	}
    }

    exit(0);
}