home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 May / pcp151c.iso / misc / src / install / modutils / insmod / lsmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-06  |  3.8 KB  |  177 lines

  1. /* List the currently loaded modules.
  2.    Copyright 1996, 1997 Linux International.
  3.  
  4.    New implementation contributed by Richard Henderson <rth@tamu.edu>
  5.    Based on original work by Bjorn Eckwall <bj0rn@blox.se>
  6.  
  7.    This file is part of the Linux modutils.
  8.  
  9.    This program is free software; you can redistribute it and/or modify it
  10.    under the terms of the GNU General Public License as published by the
  11.    Free Software Foundation; either version 2 of the License, or (at your
  12.    option) any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful, but
  15.    WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.    General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software Foundation,
  21.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  22.  
  23. #ident "$Id: lsmod.c,v 1.1.1.1 1998/01/06 20:51:07 ewt Exp $"
  24.  
  25. #include <sys/types.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <string.h>
  31. #include <errno.h>
  32.  
  33. #include "module.h"
  34. #include "util.h"
  35.  
  36. #include "logger.h"
  37.  
  38. /*======================================================================*/
  39.  
  40. /* If we don't have query_module ... */
  41.  
  42. static int
  43. old_lsmod(void)
  44. {
  45.   int fd, len, bufsize;
  46.   char *buffer;
  47.  
  48.   /* We've got no other option but to read /proc.  */
  49.  
  50.   if ((fd = open("/proc/modules", O_RDONLY)) < 0)
  51.     {
  52.       error("/proc/modules: %m");
  53.       return 1;
  54.     }
  55.  
  56.   buffer = xmalloc(bufsize = 8*1024);
  57. retry_read:
  58.   len = read(fd, buffer, bufsize);
  59.   if (len < 0)
  60.     {
  61.       error("/proc/modules: %m");
  62.       return 1;
  63.     }
  64.   else if (len == sizeof(buffer))
  65.     {
  66.       lseek(fd, 0, SEEK_SET);
  67.       buffer = xrealloc(buffer, bufsize *= 2);
  68.       goto retry_read;
  69.     }
  70.  
  71.   close(fd);
  72.  
  73.   /* Write it out.  */
  74.  
  75.   puts("Module         Pages    Used by");
  76.   write(1, buffer, len);
  77.  
  78.   return 0;
  79. }
  80.  
  81. /* If we do have query_module ... */
  82.  
  83. static int
  84. new_lsmod(void)
  85. {
  86.   char *module_names, *m, *refs;
  87.   size_t bufsize, ret, nmod, i;
  88.  
  89.   /* A header.  */
  90.  
  91.   puts("Module                  Size  Used by");
  92.  
  93.   /* Fetch the list of modules.  */
  94.  
  95.   module_names = xmalloc(bufsize = 1024);
  96. retry_mod_load:
  97.   if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret))
  98.     {
  99.       if (errno == ENOSPC)
  100.     {
  101.       module_names = xrealloc(module_names, bufsize = ret);
  102.       goto retry_mod_load;
  103.     }
  104.       error("QM_MODULES: %m");
  105.       return 1;
  106.     }
  107.   nmod = ret;
  108.  
  109.   refs = xmalloc(bufsize = 1024);
  110.   for (i = 0, m = module_names; i < nmod; ++i, m += strlen(m)+1)
  111.     {
  112.       struct new_module_info info;
  113.       size_t j;
  114.       char *r;
  115.  
  116.       if (query_module(m, QM_INFO, &info, sizeof(info), &ret))
  117.     {
  118.       error("QM_INFO: %m");
  119.       return 1;
  120.     }
  121.  
  122.       printf("%-20s%8lu%4ld ", m, info.size, info.usecount);
  123.  
  124.       if (info.flags & NEW_MOD_DELETED)
  125.     fputs(" (deleted)", stdout);
  126.       else if (!(info.flags & NEW_MOD_RUNNING))
  127.     fputs(" (uninitialized)", stdout);
  128.       else 
  129.     {
  130.       if (info.flags & NEW_MOD_AUTOCLEAN)
  131.         fputs(" (autoclean)", stdout);
  132.       if (!(info.flags & NEW_MOD_USED_ONCE))
  133.         fputs(" (unused)", stdout);
  134.     }
  135.  
  136.     retry_ref_load:
  137.       if (query_module(m, QM_REFS, refs, bufsize, &ret))
  138.     {
  139.       if (errno == ENOSPC)
  140.         {
  141.           refs = xrealloc(refs, bufsize = ret);
  142.           goto retry_ref_load;
  143.         }
  144.       error("QM_REFS: %m");
  145.       return 1;
  146.     }
  147.  
  148.       if (ret > 0)
  149.     {
  150.       putchar(' ');
  151.       putchar('[');
  152.       j = 0, r = refs;
  153.       fputs(r, stdout);
  154.       while (r += strlen(r)+1, ++j < ret)
  155.         {
  156.           putchar(' ');
  157.           fputs(r, stdout);
  158.         }
  159.       putchar(']');
  160.     }
  161.  
  162.       putchar('\n');
  163.     }
  164.  
  165.   return 0;
  166. }
  167.  
  168. int
  169. main(int argc, char **argv)
  170. {
  171.   error_file = "lsmod";
  172.   if (query_module(NULL, 0, NULL, 0, NULL) == 0)
  173.     return new_lsmod();
  174.   else
  175.     return old_lsmod();
  176. }
  177.