updateLysKOM.c 4.38 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
/*
Per Cederqvist's avatar
Per Cederqvist committed
2
 * $Id: updateLysKOM.c,v 1.7 1998/07/08 17:09:19 ceder 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 *
 * 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

Per Cederqvist's avatar
Per Cederqvist committed
50
static 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
{
    char lbuf[80];
    char buf2[300];
    struct stat sbuf;

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

    if (pid != 0)
66
    {
67
	if (kill(pid, SIGHUP) != 0)
68
	{
69
70
71
72
73
74
	    if (errno != ESRCH)
	    {
		fprintf(stderr, "%s: kill(%ld, SIGHUP) failed", progname, pid);
		perror("");
		exit(1);
	    }
75
	}
76
	else
77
	{
78
79
80
81
82
83
84
85
86
	    /* 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);
	    }
87
88
	}
    }
89

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

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

int
main (int    argc,
      char **argv)
{
    int i;
    FILE *fp;
141
    long pid;
Per Cederqvist's avatar
Per Cederqvist committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155

    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':
Per Cederqvist's avatar
Per Cederqvist committed
156
	    fprintf(stderr, "updateLysKOM %d $Revision: 1.7 $\n",
157
158
#include "version.incl"
		    );
Per Cederqvist's avatar
Per Cederqvist committed
159
160
161
162
163
164
	    exit(0);

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

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

	    savecore();

Per Cederqvist's avatar
Per Cederqvist committed
203
204
205
206
207
208
209
210
211
	    execl(DEFAULT_PREFIX "/bin/lyskomd", "lyskomd", (char *)0);
	    fprintf(stderr, "%s: execl() failed: ", argv[0]);
	    perror("");
	    exit(1);
	}
    }

    exit(0);
}