File: update.c

package info (click to toggle)
update 1.3-2
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 32 kB
  • ctags: 20
  • sloc: ansic: 97; makefile: 50
file content (170 lines) | stat: -rw-r--r-- 3,591 bytes parent folder | download
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
/* -*- mode: c; c-file-style: "BSD"; -*-
 *
 * Update Daemon for Linux (and possibly other Unixen).
 * Copyright (c) 1996, 1997 Torsten Poulin.
 *
 *  This program 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 2, or (at your option)
 *  any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $RCSfile: update.c,v $
 * $Author: torsten $
 * $Date: 1997/02/13 17:32:41 $
 * $Revision: 1.3 $
 */

#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <signal.h>

/*
 * If NO_BDFLUSH is defined, make this a traditional update
 * daemon that calls sync(). This is the default for systems
 * other than Linux.
 */

#ifdef __linux__
#   include <asm/unistd.h>
#   if defined(__GLIBC__) && __GLIBC__ >= 2
#       include <sys/kdaemon.h>
#   else
        _syscall2(int, bdflush, int, func, int, data);
#   endif
#else
#   ifndef NO_BDFLUSH
#       define NO_BDFLUSH
#   endif
#endif

#ifdef NO_BDFLUSH
#   define ARGS "s:"
#else
#   define ARGS "Ss:f:"
#   include <syslog.h>
#endif

/*
 * OPEN_MAX is POSIX.1
 */
#ifndef OPEN_MAX
#   define OPEN_MAX 256		/* anybody's guess */
#endif

static const char RCS[] = "@(#) $Revision: 1.3 $";

unsigned int sync_duration = 30;
unsigned int flush_duration = 5;
int use_sync = 0;

void usage(char *name)
{
    fprintf(stderr,
#ifdef NO_BDFLUSH
	    "Usage: %s [-s secs]\n",
#else
	    "Usage: %s [-S] [-s secs] [-f secs]\n",
#endif
	    name);
    exit (1);
}

/*
 * The update() function never returns.
 * If bdflush() fails, it reverts to sync() instead.
 */

void
update(void)
{
    for (;;) {
#ifndef NO_BDFLUSH
	if (use_sync) {
#endif
	    sleep(sync_duration);
	    sync();
#ifndef NO_BDFLUSH
	} else {
	    sleep(flush_duration);
	    if (bdflush(1, 0) < 0) {
		use_sync = 1;
		openlog("update", LOG_CONS, LOG_DAEMON);
		syslog(LOG_INFO, "Switching to sync(2) instead of bdflush(2)");
		closelog();
	    }
	}
#endif
    }
}

int
main(int argc, char **argv)
{
    int f, c;

    while ((c = getopt(argc, argv, ARGS)) != -1)
	switch (c) {
	case 'S': use_sync = 1; break;
	case 's':	sync_duration = atoi(optarg); break;
	case 'f': flush_duration = atoi(optarg); break;
	case '?': usage(argv[0]);
	default: abort();
	}
    
    if (sync_duration == 0 || flush_duration == 0) {
	fprintf(stderr, "%s: argument must be positive integer.\n", argv[0]);
	exit(1);
    }

    if (optind < argc)
	usage(argv[0]);

    /*
     * Prevent people from launching more update daemons.
     * Might as well call sync().
     */
    if (geteuid() != 0) {
	sync();
	fprintf(stderr, "%s: should only be run by root.\n", argv[0]);
	exit(1);
    }

    /*
     * Ignore a few signals for good measure.
     */
    signal(SIGTERM, SIG_IGN);
    signal(SIGINT, SIG_IGN);
    
    if (fork() > 0)
	exit(0);
  
    /*
     * Become session leader (get rid of controlling terminal).
     */
    setsid();

    /*
     * Make sure we are on a file system that stays mounted.
     */
    chdir("/");
    
    for (f = 0; f < OPEN_MAX; f++)
	close(f);
    
    update();
    
    return 0;
}