File: lsmod.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 (137 lines) | stat: -rw-r--r-- 3,157 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
/*
 * List the currently loaded modules.
 *
 * New re-implementation by Bjrn Ekwall <bj0rn@blox.se> February 1999.
 * Inspired by work contributed by Richard Henderson <rth@tamu.edu>
 *
 * This file is part of the Linux modutils.
 *
 * 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 of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ident "$Id: lsmod.c 1.3 Thu, 13 Apr 2000 18:17:59 +1000 kaos $"

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <getopt.h>

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

#ifdef COMBINE_lsmod
#define main lsmod_main
#endif

void lsmod_usage(void)
{
	fputs("Usage:\n"
	      "lsmod [-Vh]\n"
	      "\n"
	      " -V, --version    Print the release version\n"
	      " -h, --help       Print this message\n"
	      ,stderr);
}

int main(int argc, char **argv)
{
	struct module_stat *m;
	int i;
	int j;

	struct option long_opts[] = {
	  {"version", 0, 0, 'V'},
	  {"help", 0, 0, 'h'},
	  {0, 0, 0, 0}
	};

	error_file = "lsmod";

	while ((i = getopt_long(argc, argv, "Vh",
				&long_opts[0], NULL)) != EOF)
		switch(i) {
		case 'V':
			fputs("lsmod version " MODUTILS_VERSION "\n",stderr);
			return 0;
		case 'h':
			lsmod_usage();
			return 0;
		default:
			lsmod_usage();
			return 1;
		}

	/* A header.  */
	puts("Module                  Size  Used by");

	if (getuid() != 0) {
		int fd;
		char line[100];

		fflush(stdout);

		/*FIXME: non-root format... */
		if ((fd = open("/proc/modules", O_RDONLY)) >= 0 ) {
			while ((i = read(fd, line, 100)) > 0)
				write(1, line, i);
			close(fd);
			return 0;
		}
		perror("/proc/modules");
		return 1;
	}

	if (!get_kernel_info(K_INFO | K_REFS))
		return 1;

	for (i = 0, m = module_stat; i < n_module_stat; ++i, ++m) {
		printf("%-20s%8lu%4ld ", m->name, m->size, m->usecount);

		if (m->flags & NEW_MOD_DELETED)
			fputs(" (deleted)", stdout);
		else if (m->flags & NEW_MOD_INITIALIZING)
			fputs(" (initializing)", stdout);
		else if (!(m->flags & NEW_MOD_RUNNING))
			fputs(" (uninitialized)", stdout);
		else {
			if (m->flags & NEW_MOD_AUTOCLEAN)
				fputs(" (autoclean)", stdout);
			if (!(m->flags & NEW_MOD_USED_ONCE))
				fputs(" (unused)", stdout);
		}

		if (m->nrefs > 0) {
			struct module_stat *mr;

			putchar(' ');
			putchar('[');
			for (j = 0; j < m->nrefs; ++j) {
				mr = m->refs[j];
				fputs(mr->name, stdout);
				if (j != m->nrefs - 1)
					putchar(' ');
			}
			putchar(']');
		}
		putchar('\n');
	}

	return 0;
}