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 
27 Mount::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 
38 void Mount::listMounts() const
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 
72 void 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 }
ArgumentContainer::get
const char * get(const char *name) const
Get argument by name.
Definition: ArgumentContainer.cpp:49
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
Memory::Range
Memory range.
Definition: Memory.h:55
API::Result
Result
Enumeration of generic kernel API result codes.
Definition: API.h:68
FileSystemMount.h
string.h
FileSystemMount
Represents a mounted filesystem.
Definition: FileSystemMount.h:35
FileSystemClient
FileSystemClient provides a simple interface to a FileSystemServer.
Definition: FileSystemClient.h:42
PAGESIZE
#define PAGESIZE
ARM uses 4K pages.
Definition: ARMConstant.h:97
FileSystemClient::waitFileSystem
FileSystem::Result waitFileSystem(const char *path) const
Blocking wait for a mounted filesystem.
Definition: FileSystemClient.cpp:334
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
Address
unsigned long Address
A memory address.
Definition: Types.h:131
Application::Success
@ Success
Definition: Application.h:55
Application::arguments
const ArgumentContainer & arguments() const
Get program arguments.
Definition: Application.cpp:112
ArgumentParser::setDescription
void setDescription(const String &desc)
Set program description.
Definition: ArgumentParser.cpp:95
Mount::listMounts
void listMounts() const
Print currently mounted file systems.
Definition: Mount.cpp:38
FileSystem::Success
@ Success
Definition: FileSystem.h:54
API::Read
@ Read
Definition: API.h:98
ARMMap
Memory mapping for the kernel and user processes on the ARM architecture.
Definition: ARMMap.h:37
Mount::~Mount
virtual ~Mount()
Destructor.
Definition: Mount.cpp:34
printf
int printf(const char *format,...)
Output a formatted string to standard output.
Definition: printf.cpp:22
MemoryMap::range
Memory::Range range(Region region) const
Get memory range for the given region.
Definition: MemoryMap.cpp:36
stdio.h
NULL
#define NULL
NULL means zero.
Definition: Macros.h:39
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Mount::exec
virtual Result exec()
Execute the application.
Definition: Mount.cpp:86
Application::Result
Result
Result codes.
Definition: Application.h:53
ArgumentParser::registerFlag
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
Definition: ArgumentParser.cpp:100
FileSystemClient::getFileSystems
FileSystemMount * getFileSystems(Size &numberOfMounts) const
Get file system mounts table.
Definition: FileSystemClient.cpp:344
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
FileSystemClient.h
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
API::Success
@ Success
Definition: API.h:70
Mount::waitForMount
void waitForMount(const char *path) const
Blocking wait until the given path is mounted.
Definition: Mount.cpp:72
Mount.h
Memory::Range::virt
Address virt
Virtual address.
Definition: Memory.h:57
MemoryMap::UserArgs
@ UserArgs
< Used for copying program arguments and file descriptors
Definition: MemoryMap.h:61
stdlib.h
VMCopy
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
exit
C void exit(int status)
Terminate a process.
Definition: exit.cpp:21
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Mount::Mount
Mount(int argc, char **argv)
Constructor.
Definition: Mount.cpp:27
errno.h