File: modclean.c

package info (click to toggle)
modutils 2.3.11-13.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,156 kB
  • ctags: 1,038
  • sloc: ansic: 10,567; sh: 2,120; lex: 484; makefile: 479; yacc: 362
file content (115 lines) | stat: -rw-r--r-- 2,463 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>

#include "module.h"
#include "version.h"
#include "util.h"
#include "modstat.h"

int sleep_interval = 300;
volatile int selfdestruct = 0;

void usage() {
    printf("Usage: modclean [-i seconds] [-V] [-h]\n"
	   "\n"
	   " -i sec       change sleep interval to sec seconds (default is 300)\n"
	   " -V           show version and exit\n"
	   " -h           show this text and exit\n");
}


void sighandler(int sig) {
    selfdestruct=1;
}


int main(int argc, char** argv) {
    size_t	modules_count;
    char*	module_names = NULL;
    int		i;

    while ((i=getopt(argc, argv, "i:V"))!=-1)
	switch (i) {
	    case 'i':
		{
		    char* end;
		    sleep_interval=strtol(optarg, &end, 10);
		    if (end[0]!=0) {
			fprintf(stderr, "invalid parameter for -i");
			return 1;
		    }
		}
		break;

	    case 'V':
		fprintf(stderr, "modclean version " MODUTILS_VERSION "\n");
		return 0;

	    default:
		fprintf(stderr, "Invalid option: %c", i);
		usage();
		return 1;
	}

    if (optind!=argc) {
	fprintf(stderr, "No parameters allowed\n");
	usage();
	return 1;
    }

    /* Check if the kernel supports modules */
    if ((query_module(NULL, QM_MODULES, NULL, 0, &modules_count)==-1) && (errno!=ENOSPC)) {
	printf("kernel does not support modules, not starting");
	return 0;
    }

    /* Register a signal handler for TERM */
    if (signal(SIGTERM, sighandler)==SIG_ERR) {
	perror("error registering signal handler for SIGTERM: ");
	return 1;
    }

    /* Do the usual daemon startup */
    openlog("modclean", LOG_PID, LOG_DAEMON);
    if (daemon(0, 0)==-1) {
	perror("failed to initialize daemon");
	return 1;
    }

    while (!selfdestruct) {
	/* Get the list of loaded kernel modules */
	get_kernel_info(0);
	modules_count=n_module_stat;
	module_names=xmalloc(l_module_name_list);
	memcpy(module_names, module_name_list, l_module_name_list);

	/* Try to delete modules */
	if (delete_module(NULL)) {
	    syslog(LOG_ERR, "delete_module called failed: %s", strerror(errno));
	    break;
	}
	/* Make a snapshot if necessary */
	snap_shot(module_names, modules_count);

	/* And clean up again */
	free(module_names);
	module_names=NULL;

	sleep(sleep_interval);
    }

    if (module_names!=NULL)
    	free(module_names);
    closelog();

    return (errno==0);
}

/* vim: ts=8 sw=4 ai si cindent
 */