2015-12-13 16:55:52 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_modcmds.c
|
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-12-13 16:55:52 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-12-13 16:55:52 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-12-13 16:55:52 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-21 23:08:13 +01:00
|
|
|
#include <fcntl.h>
|
2015-12-13 16:55:52 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nuttx/module.h>
|
2023-02-21 23:08:13 +01:00
|
|
|
#include <system/readline.h>
|
2015-12-13 16:55:52 +01:00
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
#include "nsh_console.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_MODULE) && !defined(CONFIG_NSH_DISABLE_MODCMDS)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_insmod
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_insmod(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2015-12-13 16:55:52 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
|
2017-01-22 22:10:12 +01:00
|
|
|
FAR void *handle;
|
2015-12-13 16:55:52 +01:00
|
|
|
|
|
|
|
/* Usage: insmod <filepath> <modulename> */
|
2020-10-29 10:17:32 +01:00
|
|
|
|
2015-12-13 16:55:52 +01:00
|
|
|
/* Install the module */
|
|
|
|
|
2017-01-22 22:10:12 +01:00
|
|
|
handle = insmod(argv[1], argv[2]);
|
|
|
|
if (handle == NULL)
|
2015-12-13 16:55:52 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "insmod", NSH_ERRNO);
|
2015-12-13 16:55:52 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_rmmod
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_rmmod(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2015-12-13 16:55:52 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
|
2017-01-22 22:10:12 +01:00
|
|
|
FAR void *handle;
|
2015-12-13 16:55:52 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Usage: rmmod <modulename> */
|
2020-10-29 10:17:32 +01:00
|
|
|
|
2017-01-22 22:10:12 +01:00
|
|
|
/* Get the module handle associated with the name */
|
|
|
|
|
|
|
|
handle = modhandle(argv[1]);
|
|
|
|
if (handle == NULL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "modhandle", NSH_ERRNO);
|
2017-01-22 22:10:12 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2015-12-13 16:55:52 +01:00
|
|
|
/* Remove the module */
|
|
|
|
|
2017-01-22 22:10:12 +01:00
|
|
|
ret = rmmod(handle);
|
2015-12-13 16:55:52 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "rmmod", NSH_ERRNO);
|
2015-12-13 16:55:52 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_lsmod
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_lsmod(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2015-12-13 16:55:52 +01:00
|
|
|
{
|
2023-02-21 23:08:13 +01:00
|
|
|
int fd;
|
2022-08-22 15:17:00 +02:00
|
|
|
|
2023-02-21 23:08:13 +01:00
|
|
|
UNUSED(argc);
|
2015-12-13 16:55:52 +01:00
|
|
|
|
|
|
|
/* Usage: lsmod */
|
2020-10-29 10:17:32 +01:00
|
|
|
|
2015-12-13 16:55:52 +01:00
|
|
|
/* Open /proc/modules */
|
|
|
|
|
2023-02-21 23:08:13 +01:00
|
|
|
fd = open("/proc/modules", O_RDONLY);
|
|
|
|
if (fd < 0)
|
2015-12-13 16:55:52 +01:00
|
|
|
{
|
2023-02-21 23:08:13 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
2015-12-13 16:55:52 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output a Header */
|
|
|
|
|
2017-01-22 22:10:12 +01:00
|
|
|
nsh_output(vtbl, "%-16s %8s %8s %8s %8s %8s %8s %8s %8s\n",
|
|
|
|
"NAME", "INIT", "UNINIT", "ARG", "NEXPORTS", "TEXT", "SIZE",
|
2015-12-13 16:55:52 +01:00
|
|
|
"DATA", "SIZE");
|
|
|
|
|
|
|
|
/* Read each line from the procfs "file" */
|
|
|
|
|
2023-02-21 23:08:13 +01:00
|
|
|
while (readline_fd(vtbl->iobuffer, IOBUFFERSIZE, fd, -1) >= 0)
|
2015-12-13 16:55:52 +01:00
|
|
|
{
|
|
|
|
FAR char *modulename;
|
|
|
|
FAR char *initializer;
|
|
|
|
FAR char *uninitializer;
|
|
|
|
FAR char *arg;
|
2017-01-22 22:10:12 +01:00
|
|
|
FAR char *nexports;
|
2015-12-13 16:55:52 +01:00
|
|
|
FAR char *text;
|
|
|
|
FAR char *textsize;
|
|
|
|
FAR char *data;
|
|
|
|
FAR char *datasize;
|
|
|
|
FAR char *lasts;
|
2018-08-13 15:47:26 +02:00
|
|
|
|
2015-12-13 16:55:52 +01:00
|
|
|
/* Form of returned data is:
|
|
|
|
*
|
|
|
|
* "%s,%p,%p,%p,%p,%lu,%p,%lu\n"
|
|
|
|
*
|
2015-12-13 17:30:47 +01:00
|
|
|
* 1) Module name (string)
|
|
|
|
* 2) Initializer address (hex)
|
|
|
|
* 3) Uninitializer address (hex)
|
|
|
|
* 4) Uninitializer argument (hex)
|
|
|
|
* 5) .text address (hex)
|
|
|
|
* 6) Size of .text (decimal)
|
|
|
|
* 7) .bss/.data address (hex)
|
|
|
|
* 8) Size of .bss/.data (decimal)
|
2015-12-13 16:55:52 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
modulename = strtok_r(vtbl->iobuffer, ",\n", &lasts);
|
|
|
|
initializer = strtok_r(NULL, ",\n", &lasts);
|
|
|
|
uninitializer = strtok_r(NULL, ",\n", &lasts);
|
|
|
|
arg = strtok_r(NULL, ",\n", &lasts);
|
2017-01-22 22:10:12 +01:00
|
|
|
nexports = strtok_r(NULL, ",\n", &lasts);
|
2015-12-13 16:55:52 +01:00
|
|
|
text = strtok_r(NULL, ",\n", &lasts);
|
|
|
|
textsize = strtok_r(NULL, ",\n", &lasts);
|
|
|
|
data = strtok_r(NULL, ",\n", &lasts);
|
|
|
|
datasize = strtok_r(NULL, ",\n", &lasts);
|
|
|
|
|
2017-01-22 22:10:12 +01:00
|
|
|
nsh_output(vtbl, "%-16s %8s %8s %8s %8s %8s %8s %8s %8s\n",
|
2015-12-13 16:55:52 +01:00
|
|
|
modulename,
|
|
|
|
initializer ? initializer : "",
|
|
|
|
uninitializer ? uninitializer : "",
|
|
|
|
arg ? arg : "",
|
2017-01-22 22:10:12 +01:00
|
|
|
nexports ? nexports : "",
|
2015-12-13 16:55:52 +01:00
|
|
|
text ? text : "",
|
|
|
|
textsize ? textsize : "",
|
|
|
|
data ? data : "",
|
|
|
|
datasize ? datasize : "");
|
|
|
|
}
|
|
|
|
|
2023-02-21 23:08:13 +01:00
|
|
|
close(fd);
|
2015-12-13 16:55:52 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_FS_PROCFS && !CONFIG_FS_PROCFS_EXCLUDE_MODULE */
|
|
|
|
|
|
|
|
#endif /* CONFIG_MODULE && !CONFIG_NSH_DISABLE_MODCMDS */
|