blob: 2a0accbabf7d42d17d039f636850fc91fed68fce [file] [log] [blame]
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -02001/*
2 * kmod-insmod - insert modules into linux kernel using libkmod.
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -02005 *
Lucas De Marchicb451f32011-12-12 18:24:35 -02006 * 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.
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Lucas De Marchicb451f32011-12-12 18:24:35 -020013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020015 *
Lucas De Marchicb451f32011-12-12 18:24:35 -020016 * 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/>.
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020018 */
Lucas De Marchicb451f32011-12-12 18:24:35 -020019
Lucas De Marchic2e42862014-10-03 01:41:42 -030020#include <errno.h>
21#include <getopt.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020022#include <stdio.h>
23#include <stdlib.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020024#include <string.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030025
Lucas De Marchi5b0436a2015-01-14 14:22:23 -020026#include <shared/util.h>
27
Lucas De Marchif3578662015-01-02 12:38:27 -020028#include <libkmod/libkmod.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020029
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020030#include "kmod.h"
31
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020032static const char cmdopts_s[] = "psfVh";
33static const struct option cmdopts[] = {
34 {"version", no_argument, 0, 'V'},
35 {"help", no_argument, 0, 'h'},
36 {NULL, 0, 0, 0}
37};
38
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020039static void help(void)
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020040{
Lucas De Marchi34e06bf2012-11-06 17:32:41 -020041 printf("Usage:\n"
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020042 "\t%s [options] filename [args]\n"
43 "Options:\n"
44 "\t-V, --version show version\n"
45 "\t-h, --help show this help\n",
Lucas De Marchi7c04aee2012-11-06 19:20:09 -020046 program_invocation_short_name);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020047}
48
49static const char *mod_strerror(int err)
50{
51 switch (err) {
52 case ENOEXEC:
53 return "Invalid module format";
54 case ENOENT:
55 return "Unknown symbol in module";
56 case ESRCH:
57 return "Module has wrong symbol version";
58 case EINVAL:
59 return "Invalid parameters";
60 default:
61 return strerror(err);
62 }
63}
64
Lucas De Marchiad602692011-12-22 03:45:07 -020065static int do_insmod(int argc, char *argv[])
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020066{
67 struct kmod_ctx *ctx;
68 struct kmod_module *mod;
69 const char *filename;
70 char *opts = NULL;
71 size_t optslen = 0;
72 int i, err;
Lucas De Marchi2411c072011-12-12 15:41:02 -020073 const char *null_config = NULL;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020074
75 for (;;) {
76 int c, idx = 0;
77 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
78 if (c == -1)
79 break;
80 switch (c) {
81 case 'p':
82 case 's':
83 case 'f':
84 /* ignored, for compatibility only */
85 break;
86 case 'h':
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020087 help();
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020088 return EXIT_SUCCESS;
89 case 'V':
90 puts(PACKAGE " version " VERSION);
91 return EXIT_SUCCESS;
92 case '?':
93 return EXIT_FAILURE;
94 default:
Lucas De Marchie2f94782012-11-05 15:08:54 -020095 ERR("unexpected getopt_long() value '%c'.\n",
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020096 c);
97 return EXIT_FAILURE;
98 }
99 }
100
101 if (optind >= argc) {
Lucas De Marchie2f94782012-11-05 15:08:54 -0200102 ERR("missing filename.\n");
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200103 return EXIT_FAILURE;
104 }
105
106 filename = argv[optind];
Lucas De Marchi5b0436a2015-01-14 14:22:23 -0200107 if (streq(filename, "-")) {
Lucas De Marchie2f94782012-11-05 15:08:54 -0200108 ERR("this tool does not support loading from stdin!\n");
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200109 return EXIT_FAILURE;
110 }
111
112 for (i = optind + 1; i < argc; i++) {
113 size_t len = strlen(argv[i]);
114 void *tmp = realloc(opts, optslen + len + 2);
115 if (tmp == NULL) {
Lucas De Marchie2f94782012-11-05 15:08:54 -0200116 ERR("out of memory\n");
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200117 free(opts);
118 return EXIT_FAILURE;
119 }
120 opts = tmp;
121 if (optslen > 0) {
122 opts[optslen] = ' ';
123 optslen++;
124 }
125 memcpy(opts + optslen, argv[i], len);
126 optslen += len;
127 opts[optslen] = '\0';
128 }
129
Lucas De Marchi2411c072011-12-12 15:41:02 -0200130 ctx = kmod_new(NULL, &null_config);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200131 if (!ctx) {
Lucas De Marchie2f94782012-11-05 15:08:54 -0200132 ERR("kmod_new() failed!\n");
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200133 free(opts);
134 return EXIT_FAILURE;
135 }
136
137 err = kmod_module_new_from_path(ctx, filename, &mod);
138 if (err < 0) {
Lucas De Marchie2f94782012-11-05 15:08:54 -0200139 ERR("could not load module %s: %s\n", filename,
140 strerror(-err));
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200141 goto end;
142 }
143
144 err = kmod_module_insert_module(mod, 0, opts);
145 if (err < 0) {
Lucas De Marchie2f94782012-11-05 15:08:54 -0200146 ERR("could not insert module %s: %s\n", filename,
147 mod_strerror(-err));
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200148 }
149 kmod_module_unref(mod);
150
151end:
152 kmod_unref(ctx);
153 free(opts);
154 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
155}
Lucas De Marchiad602692011-12-22 03:45:07 -0200156
Lucas De Marchiad602692011-12-22 03:45:07 -0200157const struct kmod_cmd kmod_cmd_compat_insmod = {
158 .name = "insmod",
159 .cmd = do_insmod,
160 .help = "compat insmod command",
161};