4 * Copyright (c) 2004-2006 by Michal Čihař <michal@cihar.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2.
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
29 #include <sys/types.h>
33 #include <sys/ioctl.h>
37 /* These are usually defined by make: */
39 # define CONFIGFILE "/etc/polld"
42 # define PIDFILE "/var/run/polld.pid"
48 # define VERSION "unknown"
52 #define BLKRRPART _IO(0x12,95) /* re-read partition table */
54 /* Commandline params */
57 int sleeptime
= SLEEPTIME
;
60 char **filelist
= NULL
;
65 volatile int shutdown
= 0;
66 volatile int reload
= 0;
68 #define ERRORPREFIX "polld: "
70 void show_version(void) {
71 printf("polld version " VERSION
"\n"
72 "Copyright (C) 2004-2006 Michal Cihar <michal@cihar.com>\n"
73 "This is free software. You may redistribute copies of it under the terms of\n"
74 "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
75 "There is NO WARRANTY, to the extent permitted by law.\n");
79 void show_help(void) {
80 printf( "Usage: polld [params]\n"
82 " -h --help show this help\n"
83 " -v --version show version information\n"
84 " -c --config file configuration file to use\n"
85 " -p --pid file lock file for storing pid, empty for no locking\n"
86 " -s --sleep seconds number of seconds to sleep in sleep cycle\n"
91 void parse_params(int argc
, char **argv
) {
92 struct option long_options
[] = {
94 {"version" , 0, 0, 'v'},
95 {"config" , 1, 0, 'c'},
96 {"sleep" , 1, 0, 's'},
103 /* No error messages from getopt */
106 while ((c
= getopt_long (argc
, argv
, "hvc:s:p:", long_options
, &option_index
)) != -1) {
115 if (config
!= NULL
) free(config
);
116 config
= strdup(optarg
);
119 if (pid
!= NULL
) free(pid
);
120 pid
= strdup(optarg
);
123 sleeptime
= atoi(optarg
);
124 if (sleeptime
<= 0) {
125 fprintf(stderr
, ERRORPREFIX
"Invalid sleep time!\n");
130 fprintf(stderr
, ERRORPREFIX
"Invalid parameters, use --help for help.\n");
137 fprintf(stderr
, ERRORPREFIX
"Invalid parameters, use --help for help.\n");
140 if (config
== NULL
) {
141 config
= strdup(CONFIGFILE
);
144 pid
= strdup(PIDFILE
);
153 /* Open configuration */
154 file
= fopen(config
, "r");
156 fprintf(stderr
, ERRORPREFIX
"Failed to open configuration %s (%s)\n", config
, strerror(errno
));
160 /* Read config file lines */
161 while (getline(&line
, &len
, file
) != -1) {
162 /* Ignore comments */
163 if (line
[0] == '#') continue;
167 /* Remove trailing \n */
168 if (len
> 0 && line
[len
- 1] == '\n') {
173 /* Ignore empty lines */
180 /* Okay, we seem to have valid line */
183 /* Do we need to reallocate list? */
184 if (listlen
> alloclen
) {
185 filelist
= (char **)realloc(filelist
, (listlen
+ 4 ) * sizeof(char *));
186 if (filelist
== NULL
) {
187 fprintf(stderr
, "polld: not enough memory\n");
190 alloclen
= listlen
+ 4;
194 filelist
[listlen
- 1] = line
;
196 /* Zero to force getline to malloc this for us */
201 /* Close config file */
205 void check_lock(void) {
209 if (strlen(pid
) == 0) return;
211 /* Read existing pid */
212 file
= fopen(pid
, "r");
214 if (fscanf(file
, "%d", &other
) == 1) {
215 if (kill(other
, 0) == 0) {
216 fprintf(stderr
, ERRORPREFIX
"Another instance is running, please stop it first!\n");
220 fprintf(stderr
, ERRORPREFIX
"Can not parse pidfile, ignoring!\n");
230 if (strlen(pid
) == 0) return;
233 file
= fopen(pid
, "w");
235 fprintf(file
, "%d\n", getpid());
238 fprintf(stderr
, ERRORPREFIX
"Can not create pidfile!\n");
244 /* Remove lock file */
245 if (strlen(pid
) != 0) {
252 /* Signal handlers */
253 void interrupt(int sign
)
255 signal(sign
, SIG_IGN
);
266 int main(int argc
, char **argv
) {
270 /* Parse parameters is optionally config file name */
271 parse_params(argc
, argv
);
273 /* Load configuration */
276 /* Check for locking while we have terminal */
279 /* Fake lock while we have terminal */
282 /* Disconnect terminal.. */
289 signal(SIGINT
, interrupt
);
290 signal(SIGQUIT
, interrupt
);
291 signal(SIGTERM
, interrupt
);
298 /* Should we reload? */
300 for (i
= 0; i
< listlen
; i
++) {
312 /* Do the main work */
313 for (i
= 0; i
< listlen
; i
++) {
314 file
= open(filelist
[i
], O_RDONLY
);
317 ioctl(file
, BLKRRPART
);
327 /* Just to make gcc happy */