]> git.ipfire.org Git - thirdparty/kmod.git/blame - tools/kmod-insmod.c
tools: kmod: Add handling of compat rmmod
[thirdparty/kmod.git] / tools / kmod-insmod.c
CommitLineData
72c51a9e
GSB
1/*
2 * kmod-insmod - insert modules into linux kernel using libkmod.
3 *
4 * Copyright (C) 2011 ProFUSION embedded systems
5 *
cb451f35
LDM
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
72c51a9e
GSB
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cb451f35
LDM
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
72c51a9e 15 *
cb451f35
LDM
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
72c51a9e 18 */
cb451f35 19
72c51a9e
GSB
20#include <stdio.h>
21#include <stdlib.h>
22#include <getopt.h>
23#include <errno.h>
24#include <string.h>
25#include "libkmod.h"
26
27
28static const char cmdopts_s[] = "psfVh";
29static const struct option cmdopts[] = {
30 {"version", no_argument, 0, 'V'},
31 {"help", no_argument, 0, 'h'},
32 {NULL, 0, 0, 0}
33};
34
35static void help(const char *progname)
36{
37 fprintf(stderr,
38 "Usage:\n"
39 "\t%s [options] filename [args]\n"
40 "Options:\n"
41 "\t-V, --version show version\n"
42 "\t-h, --help show this help\n",
43 progname);
44}
45
46static const char *mod_strerror(int err)
47{
48 switch (err) {
49 case ENOEXEC:
50 return "Invalid module format";
51 case ENOENT:
52 return "Unknown symbol in module";
53 case ESRCH:
54 return "Module has wrong symbol version";
55 case EINVAL:
56 return "Invalid parameters";
57 default:
58 return strerror(err);
59 }
60}
61
62int main(int argc, char *argv[])
63{
64 struct kmod_ctx *ctx;
65 struct kmod_module *mod;
66 const char *filename;
67 char *opts = NULL;
68 size_t optslen = 0;
69 int i, err;
2411c077 70 const char *null_config = NULL;
72c51a9e
GSB
71
72 for (;;) {
73 int c, idx = 0;
74 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
75 if (c == -1)
76 break;
77 switch (c) {
78 case 'p':
79 case 's':
80 case 'f':
81 /* ignored, for compatibility only */
82 break;
83 case 'h':
84 help(argv[0]);
85 return EXIT_SUCCESS;
86 case 'V':
87 puts(PACKAGE " version " VERSION);
88 return EXIT_SUCCESS;
89 case '?':
90 return EXIT_FAILURE;
91 default:
92 fprintf(stderr,
93 "Error: unexpected getopt_long() value '%c'.\n",
94 c);
95 return EXIT_FAILURE;
96 }
97 }
98
99 if (optind >= argc) {
100 fprintf(stderr, "Error: missing filename.\n");
101 return EXIT_FAILURE;
102 }
103
104 filename = argv[optind];
105 if (strcmp(filename, "-") == 0) {
106 fputs("Error: this tool does not support loading from stdin!\n",
107 stderr);
108 return EXIT_FAILURE;
109 }
110
111 for (i = optind + 1; i < argc; i++) {
112 size_t len = strlen(argv[i]);
113 void *tmp = realloc(opts, optslen + len + 2);
114 if (tmp == NULL) {
115 fputs("Error: out of memory\n", stderr);
116 free(opts);
117 return EXIT_FAILURE;
118 }
119 opts = tmp;
120 if (optslen > 0) {
121 opts[optslen] = ' ';
122 optslen++;
123 }
124 memcpy(opts + optslen, argv[i], len);
125 optslen += len;
126 opts[optslen] = '\0';
127 }
128
2411c077 129 ctx = kmod_new(NULL, &null_config);
72c51a9e
GSB
130 if (!ctx) {
131 fputs("Error: kmod_new() failed!\n", stderr);
132 free(opts);
133 return EXIT_FAILURE;
134 }
135
136 err = kmod_module_new_from_path(ctx, filename, &mod);
137 if (err < 0) {
138 fprintf(stderr, "Error: could not load module %s: %s\n",
139 filename, strerror(-err));
140 goto end;
141 }
142
143 err = kmod_module_insert_module(mod, 0, opts);
144 if (err < 0) {
145 fprintf(stderr, "Error: could not insert module %s: %s\n",
146 filename, mod_strerror(-err));
147 }
148 kmod_module_unref(mod);
149
150end:
151 kmod_unref(ctx);
152 free(opts);
153 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
154}