xref: /third_party/toybox/toys/other/rmmod.c (revision 0f66f451)
10f66f451Sopenharmony_ci/* rmmod.c - Remove a module from the Linux kernel.
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_RMMOD(NEWTOY(rmmod, "<1wf", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig RMMOD
80f66f451Sopenharmony_ci  bool "rmmod"
90f66f451Sopenharmony_ci  default y
100f66f451Sopenharmony_ci  help
110f66f451Sopenharmony_ci    usage: rmmod [-wf] [MODULE]
120f66f451Sopenharmony_ci
130f66f451Sopenharmony_ci    Unload the module named MODULE from the Linux kernel.
140f66f451Sopenharmony_ci    -f	Force unload of a module
150f66f451Sopenharmony_ci    -w	Wait until the module is no longer used
160f66f451Sopenharmony_ci
170f66f451Sopenharmony_ci*/
180f66f451Sopenharmony_ci
190f66f451Sopenharmony_ci#define FOR_rmmod
200f66f451Sopenharmony_ci#include "toys.h"
210f66f451Sopenharmony_ci
220f66f451Sopenharmony_ci#include <sys/syscall.h>
230f66f451Sopenharmony_ci#define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
240f66f451Sopenharmony_ci
250f66f451Sopenharmony_civoid rmmod_main(void)
260f66f451Sopenharmony_ci{
270f66f451Sopenharmony_ci  unsigned int flags = O_NONBLOCK|O_EXCL;
280f66f451Sopenharmony_ci  char * mod_name;
290f66f451Sopenharmony_ci  int len;
300f66f451Sopenharmony_ci
310f66f451Sopenharmony_ci  // Basename
320f66f451Sopenharmony_ci  mod_name = basename(*toys.optargs);
330f66f451Sopenharmony_ci
340f66f451Sopenharmony_ci  // Remove .ko if present
350f66f451Sopenharmony_ci  len = strlen(mod_name);
360f66f451Sopenharmony_ci  if (len > 3 && !strcmp(&mod_name[len-3], ".ko" )) mod_name[len-3] = 0;
370f66f451Sopenharmony_ci
380f66f451Sopenharmony_ci  if (toys.optflags & FLAG_f) flags |= O_TRUNC;
390f66f451Sopenharmony_ci  if (toys.optflags & FLAG_w) flags &= ~O_NONBLOCK;
400f66f451Sopenharmony_ci
410f66f451Sopenharmony_ci  if (delete_module(mod_name, flags))
420f66f451Sopenharmony_ci    perror_exit("failed to unload %s", mod_name);
430f66f451Sopenharmony_ci}
44