updateLysKOM.c 4.43 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
/*
David Byers's avatar
David Byers committed
2
 * $Id: updateLysKOM.c,v 1.8 1998/12/26 22:41:11 byers Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1994, 1995  Lysator Academic Computer Association.
Per Cederqvist's avatar
Per Cederqvist committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * 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. 
 */

David Byers's avatar
David Byers committed
26
27
28
29
30

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

Per Cederqvist's avatar
Per Cederqvist committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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>

53
#include "ldifftime.h"
Per Cederqvist's avatar
Per Cederqvist committed
54

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

    if (fstat(fileno(fp), &sbuf) < 0)
    {
	perror("updateLysKOM: stat failed");
	exit(1);
    }
69
70

    if (pid != 0)
71
    {
72
	if (kill(pid, SIGHUP) != 0)
73
	{
74
75
76
77
78
79
	    if (errno != ESRCH)
	    {
		fprintf(stderr, "%s: kill(%ld, SIGHUP) failed", progname, pid);
		perror("");
		exit(1);
	    }
80
	}
81
	else
82
	{
83
84
85
86
87
88
89
90
91
	    /* The signal got through... */
	    /* cron 10 minutes */
	    if (ldifftime(time(NULL), sbuf.st_mtime) > 1260)
	    {
		/* ...more than once.  Strange. */
		fprintf(stderr, "%s: Told lyskomd to shut down.  Twice.", 
			progname);
		exit(1);
	    }
92
93
	}
    }
94

95
    if (ldifftime(time(NULL), sbuf.st_mtime) < 3600)
Per Cederqvist's avatar
Per Cederqvist committed
96
97
98
    {
	/* NOP */
    }
99
    else if (ldifftime(time(NULL), sbuf.st_mtime) < 2*3600)
Per Cederqvist's avatar
Per Cederqvist committed
100
101
102
103
104
    {
	/* Send mail to creator of file, the first line in the file. */

	if (fgets(lbuf, 80, fp) == NULL)
	{
105
106
107
	    fprintf(stderr, 
		    "updateLysKOM: LysKOM has been down for a short while\n");
	    exit(2);
Per Cederqvist's avatar
Per Cederqvist committed
108
109
110
111
112
113
114
115
116
117
118
119
120
	}
	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
    {
121
122
123
	fprintf(stderr,
		"updateLysKOM: LysKOM has been down for a long time\n");
	exit(2);
Per Cederqvist's avatar
Per Cederqvist committed
124
125
126
    }
}
	
Per Cederqvist's avatar
Per Cederqvist committed
127
128
129
130
131
132
133
134
135
136
137
138
/* Ignore errors in this function. */
static void
savecore(void)
{
    struct stat statbuf;

    if (stat(DEFAULT_PREFIX "cores/core", &statbuf) == 0
	&& stat(DEFAULT_PREFIX "bin/savecore", &statbuf) == 0)
    {
	system(DEFAULT_PREFIX "bin/savecore");
    }
}
Per Cederqvist's avatar
Per Cederqvist committed
139
140
141
142
143
144
145

int
main (int    argc,
      char **argv)
{
    int i;
    FILE *fp;
146
    long pid;
Per Cederqvist's avatar
Per Cederqvist committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160

    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':
David Byers's avatar
David Byers committed
161
	    fprintf(stderr, "updateLysKOM %d $Revision: 1.8 $\n",
162
163
#include "version.incl"
		    );
Per Cederqvist's avatar
Per Cederqvist committed
164
165
166
167
168
169
	    exit(0);

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

170
171
172
    fp = fopen(DEFAULT_PREFIX "/etc/pid", "r");
    if (fp == NULL)
    {
173
174
	fprintf(stderr, "%s: warning: %s: ", argv[0], 
		DEFAULT_PREFIX "/etc/pid");
175
	perror("");
176
	pid = 0;
177
    }
178
    else
179
    {
180
181
182
183
184
185
186
	pid = 0;
	fscanf(fp, "%lu", &pid);
	if (pid < 2)
	{
	    fprintf(stderr, "%s: got pid %ld.\n", argv[0], pid);
	    exit(1);
	}
187
188
    }
	
Per Cederqvist's avatar
Per Cederqvist committed
189
190
191
    fp = fopen(DEFAULT_PREFIX "/etc/status", "r");
    if (fp != NULL)
    {
192
	checkstatus(fp, pid, argv[0]);
Per Cederqvist's avatar
Per Cederqvist committed
193
194
195
    }
    else
    {
196
197
	errno = ESRCH;		/* Set sane default if pid==0. */
	if (pid == 0 || kill(pid, SIGUSR1) != 0)
Per Cederqvist's avatar
Per Cederqvist committed
198
199
200
201
202
203
204
	{
	    if (errno != ESRCH)
	    {
		fprintf(stderr, "%s: kill(%ld, SIGUSR1) failed", argv[0], pid);
		perror("");
		exit(1);
	    }
Per Cederqvist's avatar
Per Cederqvist committed
205
206
207

	    savecore();

Per Cederqvist's avatar
Per Cederqvist committed
208
209
210
211
212
213
214
215
216
	    execl(DEFAULT_PREFIX "/bin/lyskomd", "lyskomd", (char *)0);
	    fprintf(stderr, "%s: execl() failed: ", argv[0]);
	    perror("");
	    exit(1);
	}
    }

    exit(0);
}