updateLysKOM.c 5.7 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
/*
2
 * $Id: updateLysKOM.c,v 1.11 1999/04/18 13:11:11 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
 *
 * 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"
54
#include "pidfile.h"
Per Cederqvist's avatar
Per Cederqvist committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "kom-types.h"
#include "s-string.h"
#include "string-malloc.h"
#include "server/smalloc.h"
#include "kom-config.h"
#include "server-config.h"
#include "param.h"

static void
usage(const char *arg0)
{
    fprintf(stderr, "usage: %s [-c config-file] [ -v ] [ -V ]\n",
	    arg0);
    exit(1);
}
Per Cederqvist's avatar
Per Cederqvist committed
70

Per Cederqvist's avatar
Per Cederqvist committed
71
static void
72
73
74
checkstatus(FILE *fp,
	    long pid,
	    char *progname)
Per Cederqvist's avatar
Per Cederqvist committed
75
76
77
78
79
80
81
82
83
84
{
    char lbuf[80];
    char buf2[300];
    struct stat sbuf;

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

    if (pid != 0)
87
    {
88
	if (kill(pid, SIGHUP) != 0)
89
	{
90
91
92
93
94
95
	    if (errno != ESRCH)
	    {
		fprintf(stderr, "%s: kill(%ld, SIGHUP) failed", progname, pid);
		perror("");
		exit(1);
	    }
96
	}
97
	else
98
	{
99
	    /* The signal got through... */
Per Cederqvist's avatar
Per Cederqvist committed
100
101
	    if (ldifftime(time(NULL), sbuf.st_mtime) >
		60 * param.normal_shutdown_time)
102
	    {
Per Cederqvist's avatar
Per Cederqvist committed
103
104
105
106
107
		/* It takes very long for lyskomd to shut down.  Strange. */
		fprintf(stderr,
			"%s: Warning: it takes lyskomd a long time to die.\n"
			"Consider increasing ``Normal shutdown time'' "
			"in the config file if you get this message often.\n",
108
109
110
			progname);
		exit(1);
	    }
111
112
	}
    }
113

Per Cederqvist's avatar
Per Cederqvist committed
114
    if (ldifftime(time(NULL), sbuf.st_mtime) < 60 * param.downtime_mail_start)
Per Cederqvist's avatar
Per Cederqvist committed
115
116
117
    {
	/* NOP */
    }
Per Cederqvist's avatar
Per Cederqvist committed
118
119
    else if (ldifftime(time(NULL), sbuf.st_mtime)
	     < 60 * param.downtime_mail_end)
Per Cederqvist's avatar
Per Cederqvist committed
120
    {
Per Cederqvist's avatar
Per Cederqvist committed
121
122
	/* The first line of the file should be a mail addres to send
	   a reminder to.  */
Per Cederqvist's avatar
Per Cederqvist committed
123
124
125

	if (fgets(lbuf, 80, fp) == NULL)
	{
126
127
128
	    fprintf(stderr, 
		    "updateLysKOM: LysKOM has been down for a short while\n");
	    exit(2);
Per Cederqvist's avatar
Per Cederqvist committed
129
130
131
	}
	if (strchr(lbuf, '\n'))
	    *strchr(lbuf, '\n') = '\0';
Per Cederqvist's avatar
Per Cederqvist committed
132
	sprintf(buf2, "echo 'Reminder: LysKOM is still not running.'|mail %s",
Per Cederqvist's avatar
Per Cederqvist committed
133
134
135
136
137
138
139
140
141
		lbuf);
	if (system(buf2) != 0)
	{
	    perror("updateLysKOM: system failed");
	    exit(1);
	}
    }
    else
    {
142
143
144
	fprintf(stderr,
		"updateLysKOM: LysKOM has been down for a long time\n");
	exit(2);
Per Cederqvist's avatar
Per Cederqvist committed
145
146
147
    }
}
	
Per Cederqvist's avatar
Per Cederqvist committed
148
149
150
151
152
/* Ignore errors in this function. */
static void
savecore(void)
{
    struct stat statbuf;
153
    char *corefile;
Per Cederqvist's avatar
Per Cederqvist committed
154

155
156
157
158
159
160
    corefile = smalloc(strlen(param.core_dir) + 1 + 4 + 1);
    strcpy(corefile, param.core_dir);
    strcat(corefile, "/core");

    if (stat(corefile, &statbuf) == 0
	&& stat(param.savecore_path, &statbuf) == 0)
Per Cederqvist's avatar
Per Cederqvist committed
161
    {
162
	system(param.savecore_path);
Per Cederqvist's avatar
Per Cederqvist committed
163
    }
164
    sfree(corefile);
Per Cederqvist's avatar
Per Cederqvist committed
165
}
Per Cederqvist's avatar
Per Cederqvist committed
166
167
168
169
170
171
172

int
main (int    argc,
      char **argv)
{
    int i;
    FILE *fp;
173
    long pid;
Per Cederqvist's avatar
Per Cederqvist committed
174
175
    char *default_config_file = NULL;
    char *config_file = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
176
177
178
179
180
181
182
183

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

Per Cederqvist's avatar
Per Cederqvist committed
184
185
186
    /* Initialize the string handling package. */
    s_set_storage_management(string_malloc, string_realloc, string_free);

Per Cederqvist's avatar
Per Cederqvist committed
187
188
    /* Parse command line arguments. */
    for (i = 1; i < argc && argv[i][0] == '-'; i++)
Per Cederqvist's avatar
Per Cederqvist committed
189
190
191
192
    {
	if (argv[i][1] == '\0' || argv[i][2] != '\0')
	    usage(argv[0]);

Per Cederqvist's avatar
Per Cederqvist committed
193
194
	switch (argv[i][1])
	{
Per Cederqvist's avatar
Per Cederqvist committed
195
196
197
198
199
200
201
202
203
204
205
	case 'c':
	    if (config_file != NULL)
	    {
		fprintf(stderr, "%s: -c may only be used once\n", argv[0]);
		exit(1);
	    }
	    if (++i >= argc)
		usage(argv[0]);
	    config_file = argv[i];
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
206
207
	case 'V':
	case 'v':
208
	    fprintf(stderr, "updateLysKOM %d $Revision: 1.11 $\n",
209
210
#include "version.incl"
		    );
Per Cederqvist's avatar
Per Cederqvist committed
211
212
213
	    exit(0);

	default:
Per Cederqvist's avatar
Per Cederqvist committed
214
	    usage(argv[0]);
Per Cederqvist's avatar
Per Cederqvist committed
215
	}
Per Cederqvist's avatar
Per Cederqvist committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    }
    
    if (i < argc)
	usage(argv[0]);

    /* Read in the configuration file. */

    if (config_file == NULL)
    {
	default_config_file = smalloc(strlen(DEFAULT_DBASE_DIR) +
				      strlen(CONFIG_FILE) + 2);
	sprintf(default_config_file, "%s/%s", DEFAULT_DBASE_DIR, CONFIG_FILE);
	config_file = default_config_file;
    }

    read_configuration(config_file);
Per Cederqvist's avatar
Per Cederqvist committed
232

Per Cederqvist's avatar
Per Cederqvist committed
233
    pid = read_pid_file(param.pid_name, argv[0]);
234
    if (pid == 1)
235
    {
236
237
	fprintf(stderr, "%s: got pid %ld.\n", argv[0], pid);
	exit(1);
238
239
    }
	
Per Cederqvist's avatar
Per Cederqvist committed
240
    fp = fopen(param.status_file, "r");
Per Cederqvist's avatar
Per Cederqvist committed
241
242
    if (fp != NULL)
    {
243
	checkstatus(fp, pid, argv[0]);
Per Cederqvist's avatar
Per Cederqvist committed
244
245
246
    }
    else
    {
247
248
	errno = ESRCH;		/* Set sane default if pid==0. */
	if (pid == 0 || kill(pid, SIGUSR1) != 0)
Per Cederqvist's avatar
Per Cederqvist committed
249
250
251
252
253
254
255
	{
	    if (errno != ESRCH)
	    {
		fprintf(stderr, "%s: kill(%ld, SIGUSR1) failed", argv[0], pid);
		perror("");
		exit(1);
	    }
Per Cederqvist's avatar
Per Cederqvist committed
256
257
258

	    savecore();

Per Cederqvist's avatar
Per Cederqvist committed
259
	    execl(param.lyskomd_path, "lyskomd", (char *)0);
Per Cederqvist's avatar
Per Cederqvist committed
260
261
262
263
264
265
266
267
	    fprintf(stderr, "%s: execl() failed: ", argv[0]);
	    perror("");
	    exit(1);
	}
    }

    exit(0);
}