File: lsmod.c

package info (click to toggle)
modutils 2.1.85-11
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,020 kB
  • ctags: 1,024
  • sloc: ansic: 9,324; sh: 2,180; lex: 484; yacc: 362; makefile: 309
file content (176 lines) | stat: -rw-r--r-- 3,895 bytes parent folder | download | duplicates (2)
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
/* List the currently loaded modules.
   Copyright 1996, 1997 Linux International.

   New implementation contributed by Richard Henderson <rth@tamu.edu>
   Based on original work by Bjorn Eckwall <bj0rn@blox.se>

   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,v 1.1 1997/09/10 22:14:48 rth Exp $"

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#include "module.h"
#include "util.h"

#include "logger.h"

/*======================================================================*/

/* If we don't have query_module ... */

static int
old_lsmod(void)
{
  int fd, len, bufsize;
  char *buffer;

  /* We've got no other option but to read /proc.  */

  if ((fd = open("/proc/modules", O_RDONLY)) < 0)
    {
      error("/proc/modules: %m");
      return 1;
    }

  buffer = xmalloc(bufsize = 8*1024);
retry_read:
  len = read(fd, buffer, bufsize);
  if (len < 0)
    {
      error("/proc/modules: %m");
      return 1;
    }
  else if (len == sizeof(buffer))
    {
      lseek(fd, 0, SEEK_SET);
      buffer = xrealloc(buffer, bufsize *= 2);
      goto retry_read;
    }

  close(fd);

  /* Write it out.  */

  puts("Module         Pages    Used by");
  write(1, buffer, len);

  return 0;
}

/* If we do have query_module ... */

static int
new_lsmod(void)
{
  char *module_names, *m, *refs;
  size_t bufsize, ret, nmod, i;

  /* A header.  */

  puts("Module                  Size  Used by");

  /* Fetch the list of modules.  */

  module_names = xmalloc(bufsize = 1024);
retry_mod_load:
  if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret))
    {
      if (errno == ENOSPC)
	{
	  module_names = xrealloc(module_names, bufsize = ret);
	  goto retry_mod_load;
	}
      error("QM_MODULES: %m");
      return 1;
    }
  nmod = ret;

  refs = xmalloc(bufsize = 1024);
  for (i = 0, m = module_names; i < nmod; ++i, m += strlen(m)+1)
    {
      struct new_module_info info;
      size_t j;
      char *r;

      if (query_module(m, QM_INFO, &info, sizeof(info), &ret))
	{
	  error("QM_INFO: %m");
	  return 1;
	}

      printf("%-20s%8lu%4ld ", m, info.size, info.usecount);

      if (info.flags & NEW_MOD_DELETED)
	fputs(" (deleted)", stdout);
      else if (!(info.flags & NEW_MOD_RUNNING))
	fputs(" (uninitialized)", stdout);
      else 
	{
	  if (info.flags & NEW_MOD_AUTOCLEAN)
	    fputs(" (autoclean)", stdout);
	  if (!(info.flags & NEW_MOD_USED_ONCE))
	    fputs(" (unused)", stdout);
	}

    retry_ref_load:
      if (query_module(m, QM_REFS, refs, bufsize, &ret))
	{
	  if (errno == ENOSPC)
	    {
	      refs = xrealloc(refs, bufsize = ret);
	      goto retry_ref_load;
	    }
	  error("QM_REFS: %m");
	  return 1;
	}

      if (ret > 0)
	{
	  putchar(' ');
	  putchar('[');
	  j = 0, r = refs;
	  fputs(r, stdout);
	  while (r += strlen(r)+1, ++j < ret)
	    {
	      putchar(' ');
	      fputs(r, stdout);
	    }
	  putchar(']');
	}

      putchar('\n');
    }

  return 0;
}

int
main(int argc, char **argv)
{
  error_file = "lsmod";
  if (query_module(NULL, 0, NULL, 0, NULL) == 0)
    return new_lsmod();
  else
    return old_lsmod();
}