The Pedigree Project 0.1
modlist/main.c
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <dirent.h>
21#include <errno.h>
22#include <libgen.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <strings.h>
27#include <unistd.h>
28
29// We explicitly define the location of the modules directory to make
30// cross-system work easier - keeps consistency across all Pedigree systems.
31#define MODULE_DIR "/usr/lib/modules"
32
33extern int pedigree_module_is_loaded(char* name);
34
35int main(int argc, char** argv) {
36 int listAll = 0, listLoaded = 0, listUnloaded = 0, c = 0, err = 0;
37 while ((c = getopt(argc, argv, "aluh")) != -1) {
38 switch (c) {
39 case 'a':
40 if (listLoaded || listUnloaded)
41 err = 1;
42 else
43 listAll = 1;
44 break;
45 case 'l':
46 if (listAll || listUnloaded)
47 err = 1;
48 else
49 listLoaded = 1;
50 break;
51 case 'u':
52 if (listAll || listLoaded)
53 err = 1;
54 else
55 listUnloaded = 1;
56 break;
57 case 'h':
58 fprintf(stderr, "usage: modlist [-a] [-l] [-u]\n");
59 fprintf(stderr, "-a%-16slist all modules and show their status\n", "");
60 fprintf(stderr, "-l%-16slist all already-loaded modules\n", "");
61 fprintf(stderr, "-u%-16slist all modules that are not loaded\n", "");
62 fprintf(stderr, "-h%-16sshow this usage information\n", "");
63 fprintf(stderr, "\n");
64 fprintf(stderr,
65 "modlist without arguments defaults to listing all "
66 "modules with their status\n");
67 err = 3;
68 break;
69 default:
70 fprintf(stderr, "Unrecognized option: -%c\n", optopt);
71 err = 2;
72 }
73 }
74
75 if (!listAll && !listLoaded && !listUnloaded)
76 listAll = 1;
77
78 if (err) {
79 if (err == 1)
80 fprintf(stderr, "Only one action (-a, -l, or -u) can be specified\n");
81 else if (err == 3)
82 return 0;
83 return err;
84 }
85
86 DIR* dp = opendir(MODULE_DIR);
87 if (!dp) {
88 printf("Couldn't open the directory %s: %s.\n", MODULE_DIR, strerror(errno));
89 return -1;
90 }
91
92 struct dirent* ep;
93 while ((ep = readdir(dp))) {
94 char* filename = ep->d_name;
95
96 char* lastPeriod = strrchr(filename, '.');
97 char* suffix = lastPeriod + 1;
98 if (lastPeriod && !strcasecmp(suffix, "o")) {
99 *lastPeriod = '\0';
100 if (listAll) {
101 printf("%-32s%s\n", filename, pedigree_module_is_loaded(filename) ? "[loaded]" : "");
102 } else if (listLoaded) {
103 if (pedigree_module_is_loaded(filename)) {
104 printf("%s\n", filename);
105 }
106 } else if (listUnloaded) {
107 if (!pedigree_module_is_loaded(filename)) {
108 printf("%s\n", filename);
109 }
110 }
111 }
112 }
113 closedir(dp);
114 return 0;
115}