FreeNOS
Mount.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <FreeNOS/System.h>
19#include <FileSystemClient.h>
20#include <FileSystemMount.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <errno.h>
25#include "Mount.h"
26
27Mount::Mount(int argc, char **argv)
28 : POSIXApplication(argc, argv)
29{
30 parser().setDescription("Mount filesystems on the system");
31 parser().registerFlag('w', "wait", "Blocking wait until given filesystem path is mounted");
32}
33
35{
36}
37
39{
40 const FileSystemClient filesystem;
41 const FileSystemMount *mounts;
42 const Arch::MemoryMap map;
43 const Memory::Range range = map.range(MemoryMap::UserArgs);
44 char cmd[PAGESIZE];
45 Size numberOfMounts = 0;
46
47 // Get mounted filesystems
48 mounts = filesystem.getFileSystems(numberOfMounts);
49 assert(mounts != NULL);
50
51 // Print header
52 printf("PATH FILESYSTEM\r\n");
53
54 // Print out
55 for (Size i = 0; i < numberOfMounts; i++)
56 {
57 if (mounts[i].path[0])
58 {
59 // Get the command
60 const API::Result result = VMCopy(mounts[i].procID, API::Read, (Address) cmd, range.virt, PAGESIZE);
61 if (result != API::Success)
62 {
63 ERROR("VMCopy failed for PID " << mounts[i].procID << ": result = " << (int) result);
64 MemoryBlock::copy(cmd, "???", sizeof(cmd));
65 }
66
67 printf("%20s %s\r\n", mounts[i].path, cmd);
68 }
69 }
70}
71
72void Mount::waitForMount(const char *path) const
73{
74 const FileSystemClient filesystem;
75 const FileSystem::Result result = filesystem.waitFileSystem(path);
76
77 if (result != FileSystem::Success)
78 {
79 ERROR("failed to wait for filesystem at " << path << ": result = " << (int) result);
80 exit(1);
81 }
82
83 assert(result == FileSystem::Success);
84}
85
87{
88 const char *waitPath = arguments().get("wait") ?
89 arguments().get("wait") : ZERO;
90
91 if (waitPath != ZERO)
92 waitForMount(waitPath);
93 else
94 listMounts();
95
96 return Success;
97}
Result
Enumeration of generic kernel API result codes.
Definition API.h:69
@ Success
Definition API.h:70
@ Read
Definition API.h:98
Memory mapping for the kernel and user processes on the ARM architecture.
Definition ARMMap.h:38
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
const char * get(const char *name) const
Get argument by name.
void setDescription(const String &desc)
Set program description.
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
FileSystemClient provides a simple interface to a FileSystemServer.
FileSystem::Result waitFileSystem(const char *path) const
Blocking wait for a mounted filesystem.
FileSystemMount * getFileSystems(Size &numberOfMounts) const
Get file system mounts table.
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
@ UserArgs
< Used for copying program arguments and file descriptors
Definition MemoryMap.h:61
Memory::Range range(Region region) const
Get memory range for the given region.
Definition MemoryMap.cpp:36
void waitForMount(const char *path) const
Blocking wait until the given path is mounted.
Definition Mount.cpp:72
virtual ~Mount()
Destructor.
Definition Mount.cpp:34
virtual Result exec()
Execute the application.
Definition Mount.cpp:86
Mount(int argc, char **argv)
Constructor.
Definition Mount.cpp:27
void listMounts() const
Print currently mounted file systems.
Definition Mount.cpp:38
POSIX-compatible application.
API::Result VMCopy(const ProcessID proc, const API::Operation how, const Address ours, const Address theirs, const Size sz)
Prototype for user applications.
Definition VMCopy.h:42
#define PAGESIZE
ARM uses 4K pages.
Definition ARMConstant.h:97
C void exit(int status)
Terminate a process.
Definition exit.cpp:21
#define assert(exp)
Insert program diagnostics.
Definition assert.h:60
C int printf(const char *format,...)
Output a formatted string to standard output.
Definition printf.cpp:22
#define NULL
NULL means zero.
Definition Macros.h:39
unsigned long Address
A memory address.
Definition Types.h:131
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
Result
Result code for filesystem Actions.
Definition FileSystem.h:53
Represents a mounted filesystem.
Memory range.
Definition Memory.h:56
Address virt
Virtual address.
Definition Memory.h:57