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