qubes-rpc: fix code style - indent with spaces

This commit is contained in:
Marek Marczykowski-Górecki 2018-05-26 00:54:09 +02:00
parent 6a088a3992
commit 5deac1802f
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
5 changed files with 415 additions and 415 deletions

View File

@ -10,58 +10,58 @@
static void fix_display(void) static void fix_display(void)
{ {
setenv("DISPLAY", ":0", 1); setenv("DISPLAY", ":0", 1);
} }
static void produce_message(const char * type, const char *fmt, va_list args) static void produce_message(const char * type, const char *fmt, va_list args)
{ {
char *dialog_msg; char *dialog_msg;
char buf[1024]; char buf[1024];
(void) vsnprintf(buf, sizeof(buf), fmt, args); (void) vsnprintf(buf, sizeof(buf), fmt, args);
if (asprintf(&dialog_msg, "%s: %s: %s (error type: %s)", if (asprintf(&dialog_msg, "%s: %s: %s (error type: %s)",
program_invocation_short_name, type, buf, strerror(errno)) < 0) { program_invocation_short_name, type, buf, strerror(errno)) < 0) {
fprintf(stderr, "Failed to allocate memory for error message :(\n"); fprintf(stderr, "Failed to allocate memory for error message :(\n");
return; return;
} }
fprintf(stderr, "%s\n", dialog_msg); fprintf(stderr, "%s\n", dialog_msg);
switch (fork()) { switch (fork()) {
case -1: case -1:
exit(1); //what else exit(1); //what else
case 0: case 0:
if (geteuid() == 0) if (geteuid() == 0)
if (setuid(getuid()) != 0) if (setuid(getuid()) != 0)
perror("setuid failed, calling kdialog/zenity as root"); perror("setuid failed, calling kdialog/zenity as root");
fix_display(); fix_display();
#ifdef USE_KDIALOG #ifdef USE_KDIALOG
execlp("/usr/bin/kdialog", "kdialog", "--sorry", dialog_msg, NULL); execlp("/usr/bin/kdialog", "kdialog", "--sorry", dialog_msg, NULL);
#else #else
execlp("/usr/bin/zenity", "zenity", "--error", "--text", dialog_msg, NULL); execlp("/usr/bin/zenity", "zenity", "--error", "--text", dialog_msg, NULL);
#endif #endif
exit(1); exit(1);
default:; default:;
} }
free(dialog_msg); free(dialog_msg);
} }
void gui_fatal(const char *fmt, ...) void gui_fatal(const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
produce_message("Fatal error", fmt, args); produce_message("Fatal error", fmt, args);
va_end(args); va_end(args);
exit(1); exit(1);
} }
void qfile_gui_fatal(const char *fmt, va_list args) { void qfile_gui_fatal(const char *fmt, va_list args) {
produce_message("Fatal error", fmt, args); produce_message("Fatal error", fmt, args);
exit(1); exit(1);
} }
void gui_nonfatal(const char *fmt, ...) void gui_nonfatal(const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
produce_message("Information", fmt, args); produce_message("Information", fmt, args);
va_end(args); va_end(args);
} }

View File

@ -13,107 +13,107 @@
#include <libqubes-rpc-filecopy.h> #include <libqubes-rpc-filecopy.h>
enum { enum {
PROGRESS_FLAG_NORMAL, PROGRESS_FLAG_NORMAL,
PROGRESS_FLAG_INIT, PROGRESS_FLAG_INIT,
PROGRESS_FLAG_DONE PROGRESS_FLAG_DONE
}; };
void do_notify_progress(long long total, int flag) void do_notify_progress(long long total, int flag)
{ {
const char *du_size_env = getenv("FILECOPY_TOTAL_SIZE"); const char *du_size_env = getenv("FILECOPY_TOTAL_SIZE");
const char *progress_type_env = getenv("PROGRESS_TYPE"); const char *progress_type_env = getenv("PROGRESS_TYPE");
const char *saved_stdout_env = getenv("SAVED_FD_1"); const char *saved_stdout_env = getenv("SAVED_FD_1");
int ignore; int ignore;
if (!progress_type_env) if (!progress_type_env)
return; return;
if (!strcmp(progress_type_env, "console") && du_size_env) { if (!strcmp(progress_type_env, "console") && du_size_env) {
char msg[256]; char msg[256];
snprintf(msg, sizeof(msg), "sent %lld/%lld KB\r", snprintf(msg, sizeof(msg), "sent %lld/%lld KB\r",
total / 1024, strtoull(du_size_env, NULL, 0)); total / 1024, strtoull(du_size_env, NULL, 0));
ignore = write(2, msg, strlen(msg)); ignore = write(2, msg, strlen(msg));
if (flag == PROGRESS_FLAG_DONE) if (flag == PROGRESS_FLAG_DONE)
ignore = write(2, "\n", 1); ignore = write(2, "\n", 1);
} }
if (!strcmp(progress_type_env, "gui") && saved_stdout_env) { if (!strcmp(progress_type_env, "gui") && saved_stdout_env) {
char msg[256]; char msg[256];
snprintf(msg, sizeof(msg), "%lld\n", total); snprintf(msg, sizeof(msg), "%lld\n", total);
ignore = write(strtoul(saved_stdout_env, NULL, 0), msg, ignore = write(strtoul(saved_stdout_env, NULL, 0), msg,
strlen(msg)); strlen(msg));
} }
if (ignore < 0) { if (ignore < 0) {
/* silence gcc warning */ /* silence gcc warning */
} }
} }
void notify_progress(int size, int flag) void notify_progress(int size, int flag)
{ {
static long long total = 0; static long long total = 0;
static long long prev_total = 0; static long long prev_total = 0;
total += size; total += size;
if (total > prev_total + PROGRESS_NOTIFY_DELTA if (total > prev_total + PROGRESS_NOTIFY_DELTA
|| (flag != PROGRESS_FLAG_NORMAL)) { || (flag != PROGRESS_FLAG_NORMAL)) {
// check for possible error from qfile-unpacker; if error occured, // check for possible error from qfile-unpacker; if error occured,
// exit() will be called, so don't bother with current state // exit() will be called, so don't bother with current state
// (notify_progress can be called as callback from copy_file()) // (notify_progress can be called as callback from copy_file())
if (flag == PROGRESS_FLAG_NORMAL) if (flag == PROGRESS_FLAG_NORMAL)
wait_for_result(); wait_for_result();
do_notify_progress(total, flag); do_notify_progress(total, flag);
prev_total = total; prev_total = total;
} }
} }
char *get_abs_path(const char *cwd, const char *pathname) char *get_abs_path(const char *cwd, const char *pathname)
{ {
char *ret; char *ret;
if (pathname[0] == '/') if (pathname[0] == '/')
return strdup(pathname); return strdup(pathname);
if (asprintf(&ret, "%s/%s", cwd, pathname) < 0) if (asprintf(&ret, "%s/%s", cwd, pathname) < 0)
return NULL; return NULL;
else else
return ret; return ret;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i; int i;
char *entry; char *entry;
char *cwd; char *cwd;
char *sep; char *sep;
int ignore_symlinks = 0; int ignore_symlinks = 0;
qfile_pack_init(); qfile_pack_init();
register_error_handler(qfile_gui_fatal); register_error_handler(qfile_gui_fatal);
register_notify_progress(&notify_progress); register_notify_progress(&notify_progress);
notify_progress(0, PROGRESS_FLAG_INIT); notify_progress(0, PROGRESS_FLAG_INIT);
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "--ignore-symlinks")==0) { if (strcmp(argv[i], "--ignore-symlinks")==0) {
ignore_symlinks = 1; ignore_symlinks = 1;
continue; continue;
} }
entry = get_abs_path(cwd, argv[i]); entry = get_abs_path(cwd, argv[i]);
do { do {
sep = rindex(entry, '/'); sep = rindex(entry, '/');
if (!sep) if (!sep)
gui_fatal gui_fatal
("Internal error: nonabsolute filenames not allowed"); ("Internal error: nonabsolute filenames not allowed");
*sep = 0; *sep = 0;
} while (sep[1] == 0); } while (sep[1] == 0);
if (entry[0] == 0) { if (entry[0] == 0) {
if (chdir("/") < 0) { if (chdir("/") < 0) {
gui_fatal("Internal error: chdir(\"/\") failed?!"); gui_fatal("Internal error: chdir(\"/\") failed?!");
} }
} else if (chdir(entry)) } else if (chdir(entry))
gui_fatal("chdir to %s", entry); gui_fatal("chdir to %s", entry);
do_fs_walk(sep + 1, ignore_symlinks); do_fs_walk(sep + 1, ignore_symlinks);
free(entry); free(entry);
} }
notify_end_and_wait_for_result(); notify_end_and_wait_for_result();
notify_progress(0, PROGRESS_FLAG_DONE); notify_progress(0, PROGRESS_FLAG_DONE);
return 0; return 0;
} }

View File

@ -17,81 +17,81 @@
#define INCOMING_DIR_ROOT "/home/user/QubesIncoming" #define INCOMING_DIR_ROOT "/home/user/QubesIncoming"
int prepare_creds_return_uid(const char *username) int prepare_creds_return_uid(const char *username)
{ {
const struct passwd *pwd; const struct passwd *pwd;
pwd = getpwnam(username); pwd = getpwnam(username);
if (!pwd) { if (!pwd) {
perror("getpwnam"); perror("getpwnam");
exit(1); exit(1);
} }
setenv("HOME", pwd->pw_dir, 1); setenv("HOME", pwd->pw_dir, 1);
setenv("USER", username, 1); setenv("USER", username, 1);
if (setgid(pwd->pw_gid) < 0) if (setgid(pwd->pw_gid) < 0)
gui_fatal("Error setting group permissions"); gui_fatal("Error setting group permissions");
if (initgroups(username, pwd->pw_gid) < 0) if (initgroups(username, pwd->pw_gid) < 0)
gui_fatal("Error initializing groups"); gui_fatal("Error initializing groups");
if (setfsuid(pwd->pw_uid) < 0) if (setfsuid(pwd->pw_uid) < 0)
gui_fatal("Error setting filesystem level permissions"); gui_fatal("Error setting filesystem level permissions");
return pwd->pw_uid; return pwd->pw_uid;
} }
int main(int argc __attribute((__unused__)), char ** argv __attribute__((__unused__))) int main(int argc __attribute((__unused__)), char ** argv __attribute__((__unused__)))
{ {
char *incoming_dir; char *incoming_dir;
int uid, ret; int uid, ret;
pid_t pid; pid_t pid;
const char *remote_domain; const char *remote_domain;
char *procdir_path; char *procdir_path;
int procfs_fd; int procfs_fd;
uid = prepare_creds_return_uid("user"); uid = prepare_creds_return_uid("user");
remote_domain = getenv("QREXEC_REMOTE_DOMAIN"); remote_domain = getenv("QREXEC_REMOTE_DOMAIN");
if (!remote_domain) { if (!remote_domain) {
gui_fatal("Cannot get remote domain name"); gui_fatal("Cannot get remote domain name");
exit(1); exit(1);
} }
mkdir(INCOMING_DIR_ROOT, 0700); mkdir(INCOMING_DIR_ROOT, 0700);
if (asprintf(&incoming_dir, "%s/%s", INCOMING_DIR_ROOT, remote_domain) < 0) if (asprintf(&incoming_dir, "%s/%s", INCOMING_DIR_ROOT, remote_domain) < 0)
gui_fatal("Error allocating memory"); gui_fatal("Error allocating memory");
mkdir(incoming_dir, 0700); mkdir(incoming_dir, 0700);
if (chdir(incoming_dir)) if (chdir(incoming_dir))
gui_fatal("Error chdir to %s", incoming_dir); gui_fatal("Error chdir to %s", incoming_dir);
if (mount(".", ".", NULL, MS_BIND | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL) < 0) if (mount(".", ".", NULL, MS_BIND | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL) < 0)
gui_fatal("Failed to mount a directory %s", incoming_dir); gui_fatal("Failed to mount a directory %s", incoming_dir);
/* parse the input in unprivileged child process, parent will hold root /* parse the input in unprivileged child process, parent will hold root
* access to unmount incoming dir */ * access to unmount incoming dir */
switch (pid=fork()) { switch (pid=fork()) {
case -1: case -1:
gui_fatal("Failed to create new process"); gui_fatal("Failed to create new process");
case 0: case 0:
if (asprintf(&procdir_path, "/proc/%d/fd", getpid()) < 0) { if (asprintf(&procdir_path, "/proc/%d/fd", getpid()) < 0) {
gui_fatal("Error allocating memory"); gui_fatal("Error allocating memory");
} }
procfs_fd = open(procdir_path, O_DIRECTORY | O_RDONLY); procfs_fd = open(procdir_path, O_DIRECTORY | O_RDONLY);
if (procfs_fd < 0) if (procfs_fd < 0)
perror("Failed to open /proc"); perror("Failed to open /proc");
else else
set_procfs_fd(procfs_fd); set_procfs_fd(procfs_fd);
free(procdir_path); free(procdir_path);
if (chroot(".")) if (chroot("."))
gui_fatal("Error chroot to %s", incoming_dir); gui_fatal("Error chroot to %s", incoming_dir);
if (setuid(uid) < 0) { if (setuid(uid) < 0) {
/* no kdialog inside chroot */ /* no kdialog inside chroot */
perror("setuid"); perror("setuid");
exit(1); exit(1);
} }
return do_unpack(); return do_unpack();
} }
if (waitpid(pid, &ret, 0) < 0) { if (waitpid(pid, &ret, 0) < 0) {
gui_fatal("Failed to wait for child process"); gui_fatal("Failed to wait for child process");
} }
if (umount2(".", MNT_DETACH) < 0) if (umount2(".", MNT_DETACH) < 0)
gui_fatal("Cannot umount incoming directory"); gui_fatal("Cannot umount incoming directory");
if (!WIFEXITED(ret)) { if (!WIFEXITED(ret)) {
gui_fatal("Child process exited abnormally"); gui_fatal("Child process exited abnormally");
} }
return WEXITSTATUS(ret); return WEXITSTATUS(ret);
} }

View File

@ -14,95 +14,95 @@
void send_file(const char *fname) void send_file(const char *fname)
{ {
const char *base; const char *base;
char sendbuf[DVM_FILENAME_SIZE]; char sendbuf[DVM_FILENAME_SIZE];
int fd = open(fname, O_RDONLY); int fd = open(fname, O_RDONLY);
if (fd < 0) if (fd < 0)
gui_fatal("open %s", fname); gui_fatal("open %s", fname);
base = rindex(fname, '/'); base = rindex(fname, '/');
if (!base) if (!base)
base = fname; base = fname;
else else
base++; base++;
if (strlen(base) >= DVM_FILENAME_SIZE) if (strlen(base) >= DVM_FILENAME_SIZE)
base += strlen(base) - DVM_FILENAME_SIZE + 1; base += strlen(base) - DVM_FILENAME_SIZE + 1;
strncpy(sendbuf,base,DVM_FILENAME_SIZE - 1); /* fills out with NULs */ strncpy(sendbuf,base,DVM_FILENAME_SIZE - 1); /* fills out with NULs */
sendbuf[DVM_FILENAME_SIZE - 1] = '\0'; sendbuf[DVM_FILENAME_SIZE - 1] = '\0';
if (!write_all(1, sendbuf, DVM_FILENAME_SIZE)) if (!write_all(1, sendbuf, DVM_FILENAME_SIZE))
gui_fatal("send filename to dispVM"); gui_fatal("send filename to dispVM");
if (!copy_fd_all(1, fd)) if (!copy_fd_all(1, fd))
gui_fatal("send file to dispVM"); gui_fatal("send file to dispVM");
close(1); close(1);
close(fd); close(fd);
} }
int copy_and_return_nonemptiness(int tmpfd) int copy_and_return_nonemptiness(int tmpfd)
{ {
struct stat st; struct stat st;
if (!copy_fd_all(tmpfd, 0)) if (!copy_fd_all(tmpfd, 0))
gui_fatal("receiving file from dispVM"); gui_fatal("receiving file from dispVM");
if (fstat(tmpfd, &st)) if (fstat(tmpfd, &st))
gui_fatal("fstat"); gui_fatal("fstat");
close(tmpfd); close(tmpfd);
return st.st_size > 0; return st.st_size > 0;
} }
void recv_file_nowrite(const char *fname) void recv_file_nowrite(const char *fname)
{ {
char *tempfile; char *tempfile;
char *errmsg; char *errmsg;
int tmpfd = -1; int tmpfd = -1;
if (asprintf(&tempfile, "/tmp/file_edited_in_dvm.XXXXXX") != -1) if (asprintf(&tempfile, "/tmp/file_edited_in_dvm.XXXXXX") != -1)
tmpfd = mkstemp(tempfile); tmpfd = mkstemp(tempfile);
if (tmpfd < 0) if (tmpfd < 0)
gui_fatal("unable to create any temporary file, aborting"); gui_fatal("unable to create any temporary file, aborting");
if (!copy_and_return_nonemptiness(tmpfd)) { if (!copy_and_return_nonemptiness(tmpfd)) {
unlink(tempfile); unlink(tempfile);
return; return;
} }
if (asprintf(&errmsg, if (asprintf(&errmsg,
"The file %s has been edited in Disposable VM and the modified content has been received, " "The file %s has been edited in Disposable VM and the modified content has been received, "
"but this file is in nonwritable directory and thus cannot be modified safely. The edited file has been " "but this file is in nonwritable directory and thus cannot be modified safely. The edited file has been "
"saved to %s", fname, tempfile) != -1) "saved to %s", fname, tempfile) != -1)
gui_nonfatal(errmsg); gui_nonfatal(errmsg);
} }
void actually_recv_file(const char *fname, const char *tempfile, int tmpfd) void actually_recv_file(const char *fname, const char *tempfile, int tmpfd)
{ {
if (!copy_and_return_nonemptiness(tmpfd)) { if (!copy_and_return_nonemptiness(tmpfd)) {
unlink(tempfile); unlink(tempfile);
return; return;
} }
if (rename(tempfile, fname)) if (rename(tempfile, fname))
gui_fatal("rename"); gui_fatal("rename");
} }
void recv_file(const char *fname) void recv_file(const char *fname)
{ {
int tmpfd = -1; int tmpfd = -1;
char *tempfile; char *tempfile;
if (asprintf(&tempfile, "%s.XXXXXX", fname) != -1) { if (asprintf(&tempfile, "%s.XXXXXX", fname) != -1) {
tmpfd = mkstemp(tempfile); tmpfd = mkstemp(tempfile);
} }
if (tmpfd < 0) if (tmpfd < 0)
recv_file_nowrite(fname); recv_file_nowrite(fname);
else else
actually_recv_file(fname, tempfile, tmpfd); actually_recv_file(fname, tempfile, tmpfd);
} }
void talk_to_daemon(const char *fname) void talk_to_daemon(const char *fname)
{ {
send_file(fname); send_file(fname);
recv_file(fname); recv_file(fname);
} }
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
if (argc!=2) if (argc!=2)
gui_fatal("OpenInVM - no file given?"); gui_fatal("OpenInVM - no file given?");
talk_to_daemon(argv[1]); talk_to_daemon(argv[1]);
return 0; return 0;
} }

View File

@ -19,217 +19,217 @@ static const char *cleanup_dirname = NULL;
static void cleanup_file(void) static void cleanup_file(void)
{ {
if (cleanup_filename) { if (cleanup_filename) {
if (unlink(cleanup_filename) < 0) if (unlink(cleanup_filename) < 0)
fprintf(stderr, "Failed to remove file at exit\n"); fprintf(stderr, "Failed to remove file at exit\n");
cleanup_filename = NULL; cleanup_filename = NULL;
} }
if (cleanup_dirname) { if (cleanup_dirname) {
if (rmdir(cleanup_dirname) < 0) if (rmdir(cleanup_dirname) < 0)
fprintf(stderr, "Failed to remove directory at exit\n"); fprintf(stderr, "Failed to remove directory at exit\n");
cleanup_dirname = NULL; cleanup_dirname = NULL;
} }
} }
const char *gettime(void) const char *gettime(void)
{ {
static char retbuf[60]; static char retbuf[60];
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
snprintf(retbuf, sizeof(retbuf), "%lld.%06lld", snprintf(retbuf, sizeof(retbuf), "%lld.%06lld",
(long long) tv.tv_sec, (long long) tv.tv_usec); (long long) tv.tv_sec, (long long) tv.tv_usec);
return retbuf; return retbuf;
} }
static char *get_directory(void) static char *get_directory(void)
{ {
const char *remote_domain; const char *remote_domain;
char *dir; char *dir;
size_t len; size_t len;
char *ret; char *ret;
remote_domain = getenv("QREXEC_REMOTE_DOMAIN"); remote_domain = getenv("QREXEC_REMOTE_DOMAIN");
if (!remote_domain) { if (!remote_domain) {
fprintf(stderr, "Cannot get remote domain name\n"); fprintf(stderr, "Cannot get remote domain name\n");
exit(1); exit(1);
} }
if (!*remote_domain || index(remote_domain, '/')) if (!*remote_domain || index(remote_domain, '/'))
goto fail; goto fail;
if (!strcmp(remote_domain, ".") || !strcmp(remote_domain, "..")) if (!strcmp(remote_domain, ".") || !strcmp(remote_domain, ".."))
goto fail; goto fail;
len = strlen("/tmp/-XXXXXX")+strlen(remote_domain)+1; len = strlen("/tmp/-XXXXXX")+strlen(remote_domain)+1;
dir = malloc(len); dir = malloc(len);
if (!dir) { if (!dir) {
fprintf(stderr, "Cannot allocate memory\n"); fprintf(stderr, "Cannot allocate memory\n");
exit(1); exit(1);
} }
snprintf(dir, len, "/tmp/%s-XXXXXX", remote_domain); snprintf(dir, len, "/tmp/%s-XXXXXX", remote_domain);
ret = mkdtemp(dir); ret = mkdtemp(dir);
if (ret == NULL) { if (ret == NULL) {
perror("mkdtemp"); perror("mkdtemp");
exit(1); exit(1);
} }
cleanup_dirname = strdup(ret); cleanup_dirname = strdup(ret);
return ret; return ret;
fail: fail:
fprintf(stderr, "Invalid remote domain name: %s\n", remote_domain); fprintf(stderr, "Invalid remote domain name: %s\n", remote_domain);
exit(1); exit(1);
} }
char *get_filename(void) char *get_filename(void)
{ {
char buf[DVM_FILENAME_SIZE]; char buf[DVM_FILENAME_SIZE];
static char *retname; static char *retname;
int i; int i;
char *directory; char *directory;
size_t len; size_t len;
directory = get_directory(); directory = get_directory();
if (!read_all(0, buf, sizeof(buf))) if (!read_all(0, buf, sizeof(buf)))
exit(1); exit(1);
buf[DVM_FILENAME_SIZE-1] = 0; buf[DVM_FILENAME_SIZE-1] = 0;
if (index(buf, '/')) { if (index(buf, '/')) {
fprintf(stderr, "filename contains /"); fprintf(stderr, "filename contains /");
exit(1); exit(1);
} }
for (i=0; buf[i]!=0; i++) { for (i=0; buf[i]!=0; i++) {
// replace some characters with _ (eg mimeopen have problems with some of them) // replace some characters with _ (eg mimeopen have problems with some of them)
if (index(" !?\"#$%^&*()[]<>;`~|", buf[i])) if (index(" !?\"#$%^&*()[]<>;`~|", buf[i]))
buf[i]='_'; buf[i]='_';
} }
len = strlen(directory)+1+strlen(buf)+1; len = strlen(directory)+1+strlen(buf)+1;
retname = malloc(len); retname = malloc(len);
if (!retname) { if (!retname) {
fprintf(stderr, "Cannot allocate memory\n"); fprintf(stderr, "Cannot allocate memory\n");
exit(1); exit(1);
} }
snprintf(retname, len, "%s/%s", directory, buf); snprintf(retname, len, "%s/%s", directory, buf);
free(directory); free(directory);
return retname; return retname;
} }
void copy_file_by_name(const char *filename) void copy_file_by_name(const char *filename)
{ {
int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600); int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
if (fd < 0) { if (fd < 0) {
perror("open file"); perror("open file");
exit(1);
}
/* we now have created a new file, ensure we delete it at the end */
cleanup_filename = strdup(filename);
atexit(cleanup_file);
if (!copy_fd_all(fd, 0))
exit(1); exit(1);
close(fd); }
/* we now have created a new file, ensure we delete it at the end */
cleanup_filename = strdup(filename);
atexit(cleanup_file);
if (!copy_fd_all(fd, 0))
exit(1);
close(fd);
} }
void send_file_back(const char * filename) void send_file_back(const char * filename)
{ {
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
if (fd < 0) { if (fd < 0) {
perror("open file"); perror("open file");
exit(1); exit(1);
} }
if (!copy_fd_all(1, fd)) if (!copy_fd_all(1, fd))
exit(1); exit(1);
close(fd); close(fd);
close(1); close(1);
} }
int int
main() main()
{ {
struct stat stat_pre, stat_post, session_stat; struct stat stat_pre, stat_post, session_stat;
char *filename = get_filename(); char *filename = get_filename();
int child, status, log_fd, null_fd; int child, status, log_fd, null_fd;
FILE *waiter_pidfile; FILE *waiter_pidfile;
copy_file_by_name(filename); copy_file_by_name(filename);
if (stat(filename, &stat_pre)) { if (stat(filename, &stat_pre)) {
perror("stat pre"); perror("stat pre");
exit(1); exit(1);
} }
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "time=%s, waiting for qubes-session\n", gettime()); fprintf(stderr, "time=%s, waiting for qubes-session\n", gettime());
#endif #endif
// wait for X server to starts (especially in DispVM) // wait for X server to starts (especially in DispVM)
if (stat("/tmp/qubes-session-env", &session_stat)) { if (stat("/tmp/qubes-session-env", &session_stat)) {
switch (child = fork()) { switch (child = fork()) {
case -1: case -1:
perror("fork"); perror("fork");
exit(1); exit(1);
case 0: case 0:
waiter_pidfile = fopen("/tmp/qubes-session-waiter", "a"); waiter_pidfile = fopen("/tmp/qubes-session-waiter", "a");
if (waiter_pidfile == NULL) { if (waiter_pidfile == NULL) {
perror("fopen waiter_pidfile"); perror("fopen waiter_pidfile");
exit(1); exit(1);
} }
fprintf(waiter_pidfile, "%d\n", getpid()); fprintf(waiter_pidfile, "%d\n", getpid());
fclose(waiter_pidfile); fclose(waiter_pidfile);
// check the second time, to prevent race // check the second time, to prevent race
if (stat("/tmp/qubes-session-env", &session_stat)) { if (stat("/tmp/qubes-session-env", &session_stat)) {
// wait for qubes-session notify // wait for qubes-session notify
pause(); pause();
} }
exit(0); exit(0);
default: default:
waitpid(child, &status, 0); waitpid(child, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
//propagate exit code from child //propagate exit code from child
exit(WEXITSTATUS(status)); exit(WEXITSTATUS(status));
} }
} }
} }
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "time=%s, starting editor\n", gettime()); fprintf(stderr, "time=%s, starting editor\n", gettime());
#endif #endif
switch (child = fork()) { switch (child = fork()) {
case -1: case -1:
perror("fork"); perror("fork");
exit(1); exit(1);
case 0: case 0:
null_fd = open("/dev/null", O_RDONLY); null_fd = open("/dev/null", O_RDONLY);
dup2(null_fd, 0); dup2(null_fd, 0);
close(null_fd); close(null_fd);
log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666); log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666);
if (log_fd == -1) { if (log_fd == -1) {
perror("open /tmp/mimeopen.log"); perror("open /tmp/mimeopen.log");
exit(1); exit(1);
} }
dup2(log_fd, 1); dup2(log_fd, 1);
close(log_fd); close(log_fd);
setenv("HOME", USER_HOME, 1); setenv("HOME", USER_HOME, 1);
setenv("DISPLAY", ":0", 1); setenv("DISPLAY", ":0", 1);
execl("/usr/bin/qubes-open", "qubes-open", filename, (char*)NULL); execl("/usr/bin/qubes-open", "qubes-open", filename, (char*)NULL);
perror("execl"); perror("execl");
exit(1); exit(1);
default: default:
waitpid(child, &status, 0); waitpid(child, &status, 0);
if (status != 0) { if (status != 0) {
char cmd[512]; char cmd[512];
#ifdef USE_KDIALOG #ifdef USE_KDIALOG
snprintf(cmd, sizeof(cmd), snprintf(cmd, sizeof(cmd),
"HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status); "HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
("HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status); ("HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
#else #else
snprintf(cmd, sizeof(cmd), snprintf(cmd, sizeof(cmd),
"HOME=/home/user DISPLAY=:0 /usr/bin/zenity --error --text 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status); "HOME=/home/user DISPLAY=:0 /usr/bin/zenity --error --text 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
#endif #endif
status = system(cmd); status = system(cmd);
} }
} }
if (stat(filename, &stat_post)) { if (stat(filename, &stat_post)) {
perror("stat post"); perror("stat post");
exit(1); exit(1);
} }
if (stat_pre.st_mtime != stat_post.st_mtime) if (stat_pre.st_mtime != stat_post.st_mtime)
send_file_back(filename); send_file_back(filename);
free(filename); free(filename);
return 0; return 0;
} }