2011-02-17 18:11:09 +01:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include "namespace.h"
|
|
|
|
#include <lib.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
2013-12-02 18:34:30 +01:00
|
|
|
#include <minix/paths.h>
|
2011-02-17 18:11:09 +01:00
|
|
|
#include <minix/rs.h>
|
2013-12-02 18:34:30 +01:00
|
|
|
#include <minix/syslib.h>
|
2011-11-14 12:53:05 +01:00
|
|
|
#include <unistd.h>
|
2011-02-17 18:11:09 +01:00
|
|
|
#define OK 0
|
|
|
|
|
|
|
|
#ifdef __weak_alias
|
|
|
|
__weak_alias(mount, _mount)
|
|
|
|
__weak_alias(umount, _umount)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define FSDEFAULT "mfs"
|
|
|
|
|
2011-11-14 12:53:05 +01:00
|
|
|
static char fspath[] = "/sbin/:/usr/pkg/bin/"; /* Must include trailing '/' */
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static int rs_down(char *label)
|
2011-02-17 18:11:09 +01:00
|
|
|
{
|
|
|
|
char cmd[200];
|
|
|
|
if(strlen(_PATH_SERVICE)+strlen(label)+50 >= sizeof(cmd))
|
|
|
|
return -1;
|
|
|
|
sprintf(cmd, _PATH_SERVICE " down '%s'", label);
|
|
|
|
return system(cmd);
|
|
|
|
}
|
|
|
|
|
2012-07-04 14:00:28 +02:00
|
|
|
char *find_rslabel(char *args_line);
|
2013-08-20 01:37:18 +02:00
|
|
|
int mount(special, name, mountflags, srvflags, type, args)
|
2011-02-17 18:11:09 +01:00
|
|
|
char *name, *special, *type, *args;
|
2013-08-20 01:37:18 +02:00
|
|
|
int mountflags, srvflags;
|
2011-02-17 18:11:09 +01:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
message m;
|
|
|
|
struct stat statbuf;
|
2013-11-19 15:26:47 +01:00
|
|
|
char label[MNT_LABEL_LEN];
|
2011-11-14 12:53:05 +01:00
|
|
|
char path[PATH_MAX];
|
2011-02-17 18:11:09 +01:00
|
|
|
char cmd[200];
|
|
|
|
char *p;
|
2012-07-04 14:00:28 +02:00
|
|
|
char *rslabel;
|
2011-02-17 18:11:09 +01:00
|
|
|
int reuse = 0;
|
|
|
|
int use_existing = 0;
|
|
|
|
|
|
|
|
/* Default values. */
|
2011-11-14 11:07:49 +01:00
|
|
|
if (type == NULL) type = __UNCONST(FSDEFAULT);
|
|
|
|
if (args == NULL) args = __UNCONST("");
|
2011-02-17 18:11:09 +01:00
|
|
|
reuse = 0;
|
2011-11-14 12:53:05 +01:00
|
|
|
memset(path, '\0', sizeof(path));
|
2011-02-17 18:11:09 +01:00
|
|
|
|
2013-08-20 01:37:18 +02:00
|
|
|
/* Check service flags. */
|
|
|
|
if(srvflags & MS_REUSE)
|
2011-02-17 18:11:09 +01:00
|
|
|
reuse = 1;
|
|
|
|
|
2013-08-20 01:37:18 +02:00
|
|
|
if(srvflags & MS_EXISTING)
|
2011-02-17 18:11:09 +01:00
|
|
|
use_existing = 1;
|
|
|
|
|
|
|
|
/* Make a label for the file system process. This label must be unique and
|
|
|
|
* may currently not exceed 16 characters including terminating null. For
|
|
|
|
* requests with an associated block device, we use the last path component
|
|
|
|
* name of the block special file (truncated to 12 characters, which is
|
|
|
|
* hopefully enough). For requests with no associated block device, we use
|
|
|
|
* the device number and inode of the mount point, in hexadecimal form.
|
|
|
|
*/
|
|
|
|
if (!use_existing) {
|
|
|
|
if (special) {
|
|
|
|
p = strrchr(special, '/');
|
|
|
|
p = p ? p + 1 : special;
|
|
|
|
if (strchr(p, '\'')) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-19 15:26:47 +01:00
|
|
|
snprintf(label, MNT_LABEL_LEN, "fs_%.12s", p);
|
2011-02-17 18:11:09 +01:00
|
|
|
} else {
|
2012-07-04 14:00:28 +02:00
|
|
|
/* check for a rslabel option in the arguments and try to use
|
|
|
|
* that.
|
|
|
|
*/
|
|
|
|
rslabel = find_rslabel(args);
|
|
|
|
if (rslabel != NULL){
|
2013-11-19 15:26:47 +01:00
|
|
|
snprintf(label, MNT_LABEL_LEN, "%s", rslabel);
|
2012-07-04 14:00:28 +02:00
|
|
|
free(rslabel);
|
|
|
|
} else {
|
|
|
|
if (stat(name, &statbuf) < 0) return -1;
|
2013-11-19 15:26:47 +01:00
|
|
|
snprintf(label, MNT_LABEL_LEN, "fs_%llx_%llx", statbuf.st_dev, statbuf.st_ino);
|
2012-07-04 14:00:28 +02:00
|
|
|
}
|
2011-02-17 18:11:09 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* label to long? */
|
2013-11-19 15:26:47 +01:00
|
|
|
if (strlen(type) < MNT_LABEL_LEN) {
|
|
|
|
snprintf(label, MNT_LABEL_LEN, "%s", type);
|
2011-02-17 18:11:09 +01:00
|
|
|
} else {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check on user input. */
|
|
|
|
if(strchr(args, '\'')) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* start the fs-server if not using existing one */
|
|
|
|
if (!use_existing) {
|
2011-11-14 12:53:05 +01:00
|
|
|
/* See if the given type is even remotely valid. */
|
|
|
|
|
|
|
|
char *testpath;
|
|
|
|
testpath = strtok(fspath, ":");
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (strlen(testpath) + strlen(type) >= sizeof(path)) {
|
|
|
|
errno = E2BIG;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(path, testpath);
|
|
|
|
strcat(path, type);
|
|
|
|
|
|
|
|
if (access(path, F_OK) == 0) break;
|
|
|
|
|
|
|
|
} while ((testpath = strtok(NULL, ":")) != NULL);
|
|
|
|
|
|
|
|
if (testpath == NULL) {
|
|
|
|
/* We were not able to find type somewhere in "fspath" */
|
|
|
|
errno = EINVAL;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(_PATH_SERVICE) + strlen(path) + strlen(label) +
|
|
|
|
strlen(args) + 50 >= sizeof(cmd)) {
|
2011-02-17 18:11:09 +01:00
|
|
|
errno = E2BIG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-14 12:53:05 +01:00
|
|
|
sprintf(cmd, _PATH_SERVICE " %sup %s -label '%s' -args '%s %s %s%s'",
|
|
|
|
reuse ? "-r ": "", path, label, special, name,
|
|
|
|
args[0] ? "-o " : "", args);
|
2011-02-17 18:11:09 +01:00
|
|
|
|
2011-11-14 12:53:05 +01:00
|
|
|
if ((r = system(cmd)) != 0) {
|
2011-02-17 18:11:09 +01:00
|
|
|
fprintf(stderr, "mount: couldn't run %s\n", cmd);
|
|
|
|
errno = r;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now perform mount(). */
|
2013-11-04 22:48:08 +01:00
|
|
|
memset(&m, 0, sizeof(m));
|
2013-08-20 01:37:18 +02:00
|
|
|
m.VFS_MOUNT_FLAGS = mountflags;
|
|
|
|
m.VFS_MOUNT_DEVLEN = special ? strlen(special) + 1 : 0;
|
|
|
|
m.VFS_MOUNT_PATHLEN = strlen(name) + 1;
|
|
|
|
m.VFS_MOUNT_TYPELEN = strlen(type) + 1;
|
|
|
|
m.VFS_MOUNT_LABELLEN = strlen(label) + 1;
|
|
|
|
m.VFS_MOUNT_DEV = special;
|
|
|
|
m.VFS_MOUNT_PATH = name;
|
|
|
|
m.VFS_MOUNT_TYPE = type;
|
|
|
|
m.VFS_MOUNT_LABEL = label;
|
2013-11-04 22:48:08 +01:00
|
|
|
r = _syscall(VFS_PROC_NR, VFS_MOUNT, &m);
|
2011-02-17 18:11:09 +01:00
|
|
|
|
2011-11-14 12:53:05 +01:00
|
|
|
if (r != OK && !use_existing) {
|
|
|
|
/* If mount() failed, tell RS to shutdown FS process.
|
2011-02-17 18:11:09 +01:00
|
|
|
* No error check - won't do anything with this error anyway.
|
|
|
|
*/
|
|
|
|
rs_down(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-08-20 01:37:18 +02:00
|
|
|
int umount(name, srvflags)
|
2012-03-25 20:38:37 +02:00
|
|
|
const char *name;
|
2013-08-20 01:37:18 +02:00
|
|
|
int srvflags;
|
2011-02-17 18:11:09 +01:00
|
|
|
{
|
2013-11-19 15:26:47 +01:00
|
|
|
char label[MNT_LABEL_LEN];
|
2011-02-17 18:11:09 +01:00
|
|
|
message m;
|
|
|
|
int r;
|
|
|
|
|
2013-11-04 22:48:08 +01:00
|
|
|
memset(&m, 0, sizeof(m));
|
2013-08-20 01:37:18 +02:00
|
|
|
m.VFS_UMOUNT_NAME = __UNCONST(name);
|
|
|
|
m.VFS_UMOUNT_NAMELEN = strlen(name) + 1;
|
|
|
|
m.VFS_UMOUNT_LABEL = label;
|
|
|
|
m.VFS_UMOUNT_LABELLEN = sizeof(label);
|
2013-11-04 22:48:08 +01:00
|
|
|
r = _syscall(VFS_PROC_NR, VFS_UMOUNT, &m);
|
2011-02-17 18:11:09 +01:00
|
|
|
|
|
|
|
/* don't shut down the driver when exist flag is set */
|
2013-08-20 01:37:18 +02:00
|
|
|
if (!(srvflags & MS_EXISTING)) {
|
2011-02-17 18:11:09 +01:00
|
|
|
if (r == OK) {
|
2013-08-20 01:37:18 +02:00
|
|
|
/* VFS returns the label of the unmounted file system to us. */
|
|
|
|
rs_down(label);
|
2011-02-17 18:11:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2012-07-04 14:00:28 +02:00
|
|
|
|
2012-11-15 12:06:41 +01:00
|
|
|
char *find_rslabel(char *args_line)
|
2012-07-04 14:00:28 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Find and return the rslabel as given as optional
|
|
|
|
* agument to the mount command e.g.
|
|
|
|
* mount -o rslabel=bla
|
|
|
|
* or
|
|
|
|
* mount -o rw,rslabel=bla
|
|
|
|
* or as found in fstab
|
|
|
|
**/
|
|
|
|
char *buf, *input,*saveptr;
|
|
|
|
buf = input = saveptr = NULL;
|
|
|
|
|
|
|
|
if (args_line == NULL) return NULL;
|
|
|
|
|
|
|
|
/* copy the input args_line we are going to modify it*/
|
|
|
|
input = strndup(args_line,20);
|
|
|
|
if (input == NULL) /* EOM */
|
|
|
|
return NULL; /* it is not that bad to not find a label */
|
|
|
|
|
|
|
|
/* locate rslabel= in the input */
|
|
|
|
buf = strstr(input,"rslabel=");
|
|
|
|
if (buf == NULL) {
|
|
|
|
free(input);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tokenise on "," starting from rslabel (e.g null terminate )*/
|
|
|
|
buf = strtok_r(buf,",",&saveptr);
|
|
|
|
/* tokenise the result again using = and take the second entry */
|
|
|
|
buf = strtok_r(buf,"=",&saveptr);
|
|
|
|
buf = strtok_r(NULL,"=",&saveptr);
|
|
|
|
/* buf is now either NULL if there was no second token or
|
|
|
|
* the value we are searchig for
|
|
|
|
*/
|
|
|
|
if (buf != NULL)
|
|
|
|
buf = strdup(buf);
|
|
|
|
free(input);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|