updateLysKOM.c 3.84 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
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
/*
 * $Id: updateLysKOM.c,v 1.1 1994/03/06 19:59:55 ceder Exp $
 * 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>

#ifndef HAVE_DIFFTIME
#  include "tmp-difftime.h"
#endif

void
checkstatus(FILE *fp)
{
    char lbuf[80];
    char buf2[300];
    struct stat sbuf;

    if (fstat(fileno(fp), &sbuf) < 0)
    {
	perror("updateLysKOM: stat failed");
	exit(1);
    }
    
    if (difftime(time(NULL), sbuf.st_mtime) < 3600)
    {
	/* NOP */
    }
    else if (difftime(time(NULL), sbuf.st_mtime) < 2*3600)
    {
	/* 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;

    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':
	    fprintf(stderr, "updateLysKOM $Revision: 1.1 $\n");
	    exit(0);

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

    fp = fopen(DEFAULT_PREFIX "/etc/status", "r");
    if (fp != NULL)
    {
	checkstatus(fp);
    }
    else
    {
	long pid;
	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 == 0 || pid == 1)
	{
	    fprintf(stderr, "%s: got pid %ld.\n", argv[0], pid);
	    exit(1);
	}
	
	if (kill(pid, SIGUSR1) != 0)
	{
	    if (errno != ESRCH)
	    {
		fprintf(stderr, "%s: kill(%ld, SIGUSR1) failed", argv[0], pid);
		perror("");
		exit(1);
	    }
	    close(0);
	    close(1);
	    close(2);
	    if (0 != open("/dev/null", O_RDONLY)
		|| 1 != open(DEFAULT_PREFIX "etc/stdout.log", 
			     O_WRONLY|O_APPEND|O_CREAT, 0666)
		|| 2 != open(DEFAULT_PREFIX "etc/stdout.log", 
			     O_WRONLY|O_APPEND|O_CREAT, 0666))
	    {
		fprintf(stderr, "%s: open", argv[0]);
		perror("");
	    }
	    execl(DEFAULT_PREFIX "/bin/lyskomd", "lyskomd", (char *)0);
	    fprintf(stderr, "%s: execl() failed: ", argv[0]);
	    perror("");
	    exit(1);
	}
    }

    exit(0);
}