2011-01-18 09:24:57 +00:00
|
|
|
/*
|
|
|
|
* imount.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007, 2008, 2009 Red Hat, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2013-01-23 17:28:19 +00:00
|
|
|
#include <glib.h>
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
#include "imount.h"
|
2013-01-23 17:28:19 +00:00
|
|
|
#include "log.h"
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
#define _(foo) foo
|
|
|
|
|
|
|
|
static int readFD(int fd, char **buf) {
|
|
|
|
char *p;
|
|
|
|
size_t size = 4096;
|
|
|
|
int s, filesize = 0;
|
|
|
|
|
|
|
|
*buf = calloc(4096, sizeof (char));
|
|
|
|
if (*buf == NULL)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
do {
|
|
|
|
p = &(*buf)[filesize];
|
|
|
|
s = read(fd, p, 4096);
|
|
|
|
if (s < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
filesize += s;
|
|
|
|
if (s == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
size += s;
|
|
|
|
*buf = realloc(*buf, size);
|
|
|
|
if (*buf == NULL)
|
|
|
|
abort();
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
if (filesize == 0 && s < 0) {
|
|
|
|
free(*buf);
|
|
|
|
*buf = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filesize;
|
|
|
|
}
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
static size_t rstrip(char *str) {
|
|
|
|
size_t len = strlen(str);
|
|
|
|
if (len > 0 && str[len-1] == '\n') {
|
|
|
|
str[len-1] = '\0';
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
int mountCommandWrapper(int mode, char *dev, char *where, char *fs,
|
|
|
|
char *options, char **err) {
|
2013-01-23 17:28:19 +00:00
|
|
|
int rc, child, status;
|
|
|
|
int stdout_pipe[2], stderr_pipe[2];
|
2011-01-18 09:24:57 +00:00
|
|
|
char *opts = NULL, *device = NULL, *cmd = NULL;
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
switch (mode) {
|
|
|
|
case IMOUNT_MODE_MOUNT:
|
|
|
|
case IMOUNT_MODE_BIND:
|
2011-01-18 09:24:57 +00:00
|
|
|
cmd = "/bin/mount";
|
2013-01-23 17:28:19 +00:00
|
|
|
if (g_mkdir_with_parents(where, 0755))
|
|
|
|
return IMOUNT_ERR_ERRNO;
|
|
|
|
break;
|
|
|
|
case IMOUNT_MODE_UMOUNT:
|
2011-01-18 09:24:57 +00:00
|
|
|
cmd = "/bin/umount";
|
2013-01-23 17:28:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-01-18 09:24:57 +00:00
|
|
|
return IMOUNT_ERR_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == IMOUNT_MODE_MOUNT) {
|
|
|
|
if (strstr(fs, "nfs")) {
|
2013-01-23 17:28:19 +00:00
|
|
|
if (options && strlen(options)) { // do not append if the options are empty
|
2011-01-18 09:24:57 +00:00
|
|
|
if (asprintf(&opts, "%s,nolock", options) == -1) {
|
|
|
|
fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
|
|
|
|
strerror(errno));
|
|
|
|
fflush(stderr);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
opts = strdup("nolock");
|
|
|
|
}
|
|
|
|
|
|
|
|
device = strdup(dev);
|
|
|
|
} else {
|
|
|
|
if ((options && strstr(options, "bind") == NULL) &&
|
|
|
|
strncmp(dev, "LABEL=", 6) && strncmp(dev, "UUID=", 5) &&
|
|
|
|
*dev != '/') {
|
|
|
|
if (asprintf(&device, "/dev/%s", dev) == -1) {
|
|
|
|
fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
|
|
|
|
strerror(errno));
|
|
|
|
fflush(stderr);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
device = strdup(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options)
|
|
|
|
opts = strdup(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
if (pipe(stdout_pipe))
|
|
|
|
return IMOUNT_ERR_ERRNO;
|
|
|
|
if (pipe(stderr_pipe))
|
2011-01-18 09:24:57 +00:00
|
|
|
return IMOUNT_ERR_ERRNO;
|
|
|
|
|
|
|
|
if (!(child = fork())) {
|
2013-01-23 17:28:19 +00:00
|
|
|
int tty_fd;
|
2011-01-18 09:24:57 +00:00
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
close(stdout_pipe[0]);
|
|
|
|
close(stderr_pipe[0]);
|
2011-01-18 09:24:57 +00:00
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
/* Pull stdin from /dev/tty5 and redirect stdout and stderr to the pipes
|
|
|
|
* so we can log the output and put error messages into exceptions.
|
|
|
|
* We'll only use these messages should mount also return an error
|
|
|
|
* code.
|
2011-01-18 09:24:57 +00:00
|
|
|
*/
|
2013-01-23 17:28:19 +00:00
|
|
|
tty_fd = open("/dev/tty5", O_RDONLY);
|
2011-01-18 09:24:57 +00:00
|
|
|
close(STDIN_FILENO);
|
2013-01-23 17:28:19 +00:00
|
|
|
dup2(tty_fd, STDIN_FILENO);
|
|
|
|
close(tty_fd);
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
close(STDOUT_FILENO);
|
2013-01-23 17:28:19 +00:00
|
|
|
dup2(stdout_pipe[1], STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
dup2(stderr_pipe[1], STDERR_FILENO);
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
if (mode == IMOUNT_MODE_MOUNT) {
|
|
|
|
if (opts) {
|
2013-01-23 17:28:19 +00:00
|
|
|
logProgramMessage(INFO, "Running... %s -n -t %s -o %s %s %s",
|
2011-01-18 09:24:57 +00:00
|
|
|
cmd, fs, opts, device, where);
|
|
|
|
rc = execl(cmd, cmd,
|
|
|
|
"-n", "-t", fs, "-o", opts, device, where, NULL);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2013-01-23 17:28:19 +00:00
|
|
|
logProgramMessage(INFO, "Running... %s -n -t %s %s %s",
|
2011-01-18 09:24:57 +00:00
|
|
|
cmd, fs, device, where);
|
|
|
|
rc = execl(cmd, cmd, "-n", "-t", fs, device, where, NULL);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-01-23 17:28:19 +00:00
|
|
|
} else if (mode == IMOUNT_MODE_BIND) {
|
|
|
|
logProgramMessage(INFO, "Running... %s --bind %s %s",
|
|
|
|
cmd, dev, where);
|
|
|
|
rc = execl(cmd, cmd, "--bind", dev, where, NULL);
|
|
|
|
exit(1);
|
2011-01-18 09:24:57 +00:00
|
|
|
} else if (mode == IMOUNT_MODE_UMOUNT) {
|
2013-01-23 17:28:19 +00:00
|
|
|
logProgramMessage(INFO, "Running... %s %s", cmd, where);
|
2011-01-18 09:24:57 +00:00
|
|
|
rc = execl(cmd, cmd, where, NULL);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2013-01-23 17:28:19 +00:00
|
|
|
logProgramMessage(ERROR, "Running... Unknown imount mode: %d\n", mode);
|
2011-01-18 09:24:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
close(stdout_pipe[1]);
|
|
|
|
close(stderr_pipe[1]);
|
|
|
|
|
|
|
|
char *buffer = NULL;
|
|
|
|
/* In case when when the stderr pipe gets enough data to fill the kernel
|
|
|
|
* buffer we might see a deadlock as this will block the mount program on
|
|
|
|
* its next write(). The buffer size is 65kB though so we should be safe.
|
|
|
|
*/
|
|
|
|
rc = readFD(stdout_pipe[0], &buffer);
|
|
|
|
if (rc > 0) {
|
|
|
|
rstrip(buffer);
|
|
|
|
logProgramMessage(INFO, buffer);
|
|
|
|
free(buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
rc = readFD(stderr_pipe[0], &buffer);
|
|
|
|
if (rc > 0) {
|
|
|
|
rstrip(buffer);
|
|
|
|
logProgramMessage(ERROR, buffer);
|
|
|
|
if (err != NULL)
|
|
|
|
*err = buffer;
|
|
|
|
else
|
|
|
|
free(buffer);
|
2011-01-18 09:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
waitpid(child, &status, 0);
|
2013-01-23 17:28:19 +00:00
|
|
|
close(stdout_pipe[0]);
|
|
|
|
close(stderr_pipe[0]);
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
if (opts) {
|
|
|
|
free(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device) {
|
|
|
|
free(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIFEXITED(status))
|
|
|
|
return IMOUNT_ERR_OTHER;
|
2013-01-23 17:28:19 +00:00
|
|
|
else if ( (rc = WEXITSTATUS(status)) )
|
|
|
|
return rc;
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
int doBindMount(char* path, char *where, char **err) {
|
|
|
|
return mountCommandWrapper(IMOUNT_MODE_BIND,
|
|
|
|
path, where, NULL, NULL, err);
|
|
|
|
}
|
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
int doPwMount(char *dev, char *where, char *fs, char *options, char **err) {
|
|
|
|
return mountCommandWrapper(IMOUNT_MODE_MOUNT,
|
|
|
|
dev, where, fs, options, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
int doPwUmount(char *where, char **err) {
|
|
|
|
return mountCommandWrapper(IMOUNT_MODE_UMOUNT,
|
|
|
|
NULL, where, NULL, NULL, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true iff it is possible that the mount command that have returned
|
|
|
|
* 'errno' might succeed at a later time (think e.g. not yet initialized USB
|
|
|
|
* device, etc.) */
|
|
|
|
int mountMightSucceedLater(int mountRc)
|
|
|
|
{
|
2013-01-23 17:28:19 +00:00
|
|
|
/* 32 is the mount exit code for "mount failure" */
|
|
|
|
if (mountRc > 0 && mountRc & 0x20)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2011-01-18 09:24:57 +00:00
|
|
|
}
|