2012-08-04 01:47:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_mntcmds.c
|
|
|
|
*
|
2020-03-21 18:25:56 +01: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
|
2012-08-04 01:47:32 +02:00
|
|
|
*
|
2020-03-21 18:25:56 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-08-04 01:47:32 +02:00
|
|
|
*
|
2020-03-21 18:25:56 +01: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.
|
2012-08-04 01:47:32 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2020-02-27 18:39:16 +01:00
|
|
|
#include <stdlib.h>
|
2012-12-24 18:49:58 +01:00
|
|
|
#include <string.h>
|
2012-08-04 01:47:32 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2020-02-27 18:25:08 +01:00
|
|
|
#include <netdb.h>
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2012-12-24 18:49:58 +01:00
|
|
|
#include <nuttx/fs/nfs.h>
|
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
#include "nsh.h"
|
|
|
|
#include "nsh_console.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-08-04 02:37:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_df
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-21 18:25:56 +01:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && !defined(CONFIG_NSH_DISABLE_DF)
|
2017-10-26 21:28:37 +02:00
|
|
|
#ifdef NSH_HAVE_CATFILE
|
2012-08-04 02:37:25 +02:00
|
|
|
int cmd_df(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2017-10-27 00:07:42 +02:00
|
|
|
#if defined(HAVE_DF_HUMANREADBLE) && defined(HAVE_DF_BLOCKOUTPUT)
|
2013-05-01 02:03:04 +02:00
|
|
|
if (argc > 1 && strcmp(argv[1], "-h") == 0)
|
2017-10-27 00:07:42 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_DF_HUMANREADBLE
|
2013-05-01 02:03:04 +02:00
|
|
|
{
|
2017-10-26 21:28:37 +02:00
|
|
|
return nsh_catfile(vtbl, argv[0],
|
|
|
|
CONFIG_NSH_PROC_MOUNTPOINT "/fs/usage");
|
2013-05-01 02:03:04 +02:00
|
|
|
}
|
2017-10-27 00:07:42 +02:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_DF_HUMANREADBLE) && defined(HAVE_DF_BLOCKOUTPUT)
|
2013-05-01 02:03:04 +02:00
|
|
|
else
|
2017-10-27 00:07:42 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_DF_BLOCKOUTPUT
|
2013-05-01 02:03:04 +02:00
|
|
|
{
|
2017-10-26 21:28:37 +02:00
|
|
|
return nsh_catfile(vtbl, argv[0],
|
|
|
|
CONFIG_NSH_PROC_MOUNTPOINT "/fs/blocks");
|
2013-05-01 02:03:04 +02:00
|
|
|
}
|
2017-10-27 00:07:42 +02:00
|
|
|
#endif
|
2012-08-04 02:37:25 +02:00
|
|
|
}
|
|
|
|
#endif
|
2017-10-26 21:28:37 +02:00
|
|
|
#endif
|
2012-08-04 02:37:25 +02:00
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mount
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-21 18:25:56 +01:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && !defined(CONFIG_NSH_DISABLE_MOUNT)
|
2012-08-04 01:47:32 +02:00
|
|
|
int cmd_mount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2013-01-16 20:08:23 +01:00
|
|
|
FAR const char *source;
|
|
|
|
FAR char *fullsource;
|
|
|
|
FAR const char *target;
|
|
|
|
FAR char *fulltarget;
|
|
|
|
FAR const char *filesystem = NULL;
|
2015-11-25 15:47:49 +01:00
|
|
|
FAR const char *options = NULL;
|
2012-08-04 01:47:32 +02:00
|
|
|
bool badarg = false;
|
2012-09-13 21:33:47 +02:00
|
|
|
int option;
|
2012-08-04 01:47:32 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-10-26 20:17:03 +02:00
|
|
|
/* The mount command behaves differently if no parameters are provided. */
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2017-10-27 00:07:42 +02:00
|
|
|
#if defined(NSH_HAVE_CATFILE) && defined(HAVE_MOUNT_LIST)
|
2012-08-04 01:47:32 +02:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
2017-10-26 20:17:03 +02:00
|
|
|
return nsh_catfile(vtbl, argv[0],
|
|
|
|
CONFIG_NSH_PROC_MOUNTPOINT "/fs/mount");
|
2012-08-04 01:47:32 +02:00
|
|
|
}
|
2013-03-07 02:52:30 +01:00
|
|
|
#endif
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2017-10-26 20:17:03 +02:00
|
|
|
/* Get the mount options. NOTE: getopt() is not thread safe nor re-
|
|
|
|
* entrant. To keep its state proper for the next usage, it is necessary
|
|
|
|
* to parse to the end of the line even if an error occurs. If an error
|
|
|
|
* occurs, this logic just sets 'badarg' and continues.
|
2012-09-08 15:56:21 +02:00
|
|
|
*/
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2015-11-25 15:47:49 +01:00
|
|
|
while ((option = getopt(argc, argv, ":o:t:")) != ERROR)
|
2012-08-04 01:47:32 +02:00
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 't':
|
|
|
|
filesystem = optarg;
|
|
|
|
break;
|
|
|
|
|
2015-11-25 15:47:49 +01:00
|
|
|
case 'o':
|
|
|
|
options = optarg;
|
|
|
|
break;
|
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
case ':':
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2012-08-04 01:47:32 +02:00
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2012-08-04 01:47:32 +02:00
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 15:56:21 +02:00
|
|
|
/* If a bad argument was encountered, then return without processing the
|
|
|
|
* command.
|
|
|
|
*/
|
2012-08-04 01:47:32 +02:00
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2013-01-16 20:08:23 +01:00
|
|
|
/* There may be one or two required arguments after the options: the source
|
|
|
|
* and target paths. Some file systems do not require the source parameter
|
|
|
|
* so if there is only one parameter left, it must be the target.
|
2012-09-08 15:56:21 +02:00
|
|
|
*/
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2013-01-16 20:08:23 +01:00
|
|
|
if (optind >= argc)
|
2012-08-04 01:47:32 +02:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2012-08-04 01:47:32 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2013-01-16 20:08:23 +01:00
|
|
|
|
|
|
|
source = NULL;
|
|
|
|
target = argv[optind];
|
|
|
|
optind++;
|
2013-11-17 19:30:04 +01:00
|
|
|
|
2013-01-16 20:08:23 +01:00
|
|
|
if (optind < argc)
|
2012-08-04 01:47:32 +02:00
|
|
|
{
|
2013-01-16 20:08:23 +01:00
|
|
|
source = target;
|
|
|
|
target = argv[optind];
|
|
|
|
optind++;
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
|
2013-01-16 20:08:23 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
2012-08-04 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
2012-09-08 15:56:21 +02:00
|
|
|
/* While the above parsing for the -t argument looks nice, the -t argument
|
|
|
|
* not really optional.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!filesystem)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2012-09-08 15:56:21 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
/* The source and target paths might be relative to the current
|
|
|
|
* working directory.
|
|
|
|
*/
|
|
|
|
|
2013-01-16 20:08:23 +01:00
|
|
|
fullsource = NULL;
|
|
|
|
fulltarget = NULL;
|
|
|
|
|
|
|
|
if (source)
|
2012-08-04 01:47:32 +02:00
|
|
|
{
|
2013-01-16 20:08:23 +01:00
|
|
|
fullsource = nsh_getfullpath(vtbl, source);
|
|
|
|
if (!fullsource)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2012-08-04 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
2013-01-16 20:08:23 +01:00
|
|
|
fulltarget = nsh_getfullpath(vtbl, target);
|
|
|
|
if (!fulltarget)
|
2012-08-04 01:47:32 +02:00
|
|
|
{
|
2013-01-16 20:08:23 +01:00
|
|
|
ret = ERROR;
|
|
|
|
goto errout;
|
2012-08-04 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the mount */
|
|
|
|
|
2015-11-25 15:47:49 +01:00
|
|
|
ret = mount(fullsource, fulltarget, filesystem, 0, options);
|
2012-08-04 01:47:32 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "mount", NSH_ERRNO);
|
2012-08-04 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
2013-01-16 20:08:23 +01:00
|
|
|
errout:
|
|
|
|
if (fullsource)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(fullsource);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fulltarget)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(fulltarget);
|
|
|
|
}
|
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_nfsmount
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_NET) && \
|
|
|
|
defined(CONFIG_NFS) && !defined(CONFIG_NSH_DISABLE_NFSMOUNT)
|
2012-08-04 01:47:32 +02:00
|
|
|
int cmd_nfsmount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct nfs_args data;
|
|
|
|
FAR char *address;
|
|
|
|
FAR char *lpath;
|
|
|
|
FAR char *rpath;
|
|
|
|
int ret;
|
|
|
|
|
2020-03-21 18:31:48 +01:00
|
|
|
/* The fist argument on the command line should be the NFS server IP
|
|
|
|
* address in standard IPv4 (or IPv6) dot format.
|
2012-08-04 01:47:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
address = argv[1];
|
|
|
|
if (!address)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the remote mount point path */
|
|
|
|
|
|
|
|
rpath = argv[3];
|
|
|
|
|
2020-02-16 14:12:21 +01:00
|
|
|
/* Place all of the NFS arguments into the nfs_args structure */
|
|
|
|
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
|
|
|
|
/* Convert the IP address string into its binary form */
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2020-02-27 18:25:08 +01:00
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
|
|
|
if (data.addrlen == 0)
|
|
|
|
{
|
|
|
|
FAR struct addrinfo *res;
|
|
|
|
char serv[16];
|
|
|
|
|
|
|
|
itoa(NFS_PMAPPORT, serv, 10);
|
|
|
|
ret = getaddrinfo(address, serv, NULL, &res);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
data.addrlen = res->ai_addrlen;
|
|
|
|
memcpy(&data.addr, res->ai_addr, res->ai_addrlen);
|
|
|
|
freeaddrinfo(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2020-02-16 14:12:21 +01:00
|
|
|
if (data.addrlen == 0)
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in6 *sin;
|
|
|
|
|
|
|
|
sin = (FAR struct sockaddr_in6 *)&data.addr;
|
|
|
|
ret = inet_pton(AF_INET6, address, &sin->sin6_addr);
|
|
|
|
if (ret == 1)
|
|
|
|
{
|
|
|
|
sin->sin6_family = AF_INET6;
|
|
|
|
sin->sin6_port = htons(NFS_PMAPPORT);
|
|
|
|
data.addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
}
|
2012-08-04 01:47:32 +02:00
|
|
|
#endif
|
2020-02-16 14:12:21 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (data.addrlen == 0)
|
2012-08-04 01:47:32 +02:00
|
|
|
{
|
2020-02-16 14:12:21 +01:00
|
|
|
FAR struct sockaddr_in *sin;
|
|
|
|
|
|
|
|
sin = (FAR struct sockaddr_in *)&data.addr;
|
|
|
|
ret = inet_pton(AF_INET, address, &sin->sin_addr);
|
|
|
|
if (ret == 1)
|
|
|
|
{
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_port = htons(NFS_PMAPPORT);
|
|
|
|
data.addrlen = sizeof(struct sockaddr_in);
|
|
|
|
}
|
2012-08-04 01:47:32 +02:00
|
|
|
}
|
2020-02-16 14:12:21 +01:00
|
|
|
#endif
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2020-02-16 14:12:21 +01:00
|
|
|
if (data.addrlen == 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2020-02-27 18:39:16 +01:00
|
|
|
if (argc > 4 && strcmp(argv[4], "udp") == 0)
|
|
|
|
{
|
|
|
|
data.sotype = SOCK_DGRAM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data.sotype = SOCK_STREAM;
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:12:21 +01:00
|
|
|
data.path = rpath;
|
|
|
|
data.flags = 0; /* 0=Use all defaults */
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2020-03-21 18:31:48 +01:00
|
|
|
/* The local mount point path (lpath) might be relative to the current
|
|
|
|
* working directory.
|
2020-02-16 14:12:21 +01:00
|
|
|
*/
|
2012-08-04 01:47:32 +02:00
|
|
|
|
2020-02-16 14:12:21 +01:00
|
|
|
lpath = nsh_getfullpath(vtbl, argv[2]);
|
|
|
|
if (!lpath)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2012-08-04 01:47:32 +02:00
|
|
|
|
|
|
|
/* Perform the mount */
|
|
|
|
|
|
|
|
ret = mount(NULL, lpath, "nfs", 0, (FAR void *)&data);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "mount", NSH_ERRNO);
|
2012-08-04 01:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We no longer need the allocated mount point path */
|
|
|
|
|
|
|
|
nsh_freefullpath(lpath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_umount
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-21 18:25:56 +01:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && !defined(CONFIG_NSH_DISABLE_UMOUNT)
|
2012-08-04 01:47:32 +02:00
|
|
|
int cmd_umount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
/* Perform the umount */
|
|
|
|
|
|
|
|
ret = umount(fullpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "umount", NSH_ERRNO);
|
2012-08-04 01:47:32 +02:00
|
|
|
}
|
2017-10-26 21:28:37 +02:00
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
2012-08-04 02:37:25 +02:00
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|