2011-03-10 16:50:40 +01:00
|
|
|
#include <sys/stat.h>
|
2011-12-27 17:04:30 +01:00
|
|
|
#include <sys/wait.h>
|
2011-03-10 16:50:40 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2011-03-11 11:50:52 +01:00
|
|
|
#include <ioall.h>
|
2011-03-10 16:50:40 +01:00
|
|
|
#include "dvm2.h"
|
|
|
|
|
2013-11-14 21:38:27 +01:00
|
|
|
#define USER_HOME "/home/user"
|
|
|
|
#define MIMEINFO_DATABASES "/usr/share/mime:/usr/local/share:" USER_HOME "/.local/share:/usr/share/qubes/mime-override"
|
|
|
|
|
2013-02-25 06:48:29 +01:00
|
|
|
char *gettime()
|
|
|
|
{
|
|
|
|
static char retbuf[60];
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
snprintf(retbuf, sizeof(retbuf), "%lld.%lld",
|
|
|
|
(long long) tv.tv_sec, (long long) tv.tv_usec);
|
|
|
|
return retbuf;
|
|
|
|
}
|
|
|
|
|
2011-03-10 16:50:40 +01:00
|
|
|
char *get_filename()
|
|
|
|
{
|
|
|
|
char buf[DVM_FILENAME_SIZE];
|
|
|
|
static char retname[sizeof(buf) + sizeof("/tmp/")];
|
2012-01-11 19:08:15 +01:00
|
|
|
int i;
|
2011-03-10 16:50:40 +01:00
|
|
|
if (!read_all(0, buf, sizeof(buf)))
|
|
|
|
exit(1);
|
|
|
|
if (index(buf, '/')) {
|
|
|
|
fprintf(stderr, "filename contains /");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-11 19:08:15 +01:00
|
|
|
for (i=0; i < DVM_FILENAME_SIZE && buf[i]!=0; i++) {
|
|
|
|
// replace some characters with _ (eg mimeopen have problems with some of them)
|
2012-01-14 01:33:18 +01:00
|
|
|
if (index(" !?\"#$%^&*()[]<>;`~", buf[i]))
|
2012-01-11 19:08:15 +01:00
|
|
|
buf[i]='_';
|
|
|
|
}
|
2011-03-10 16:50:40 +01:00
|
|
|
snprintf(retname, sizeof(retname), "/tmp/%s", buf);
|
|
|
|
return retname;
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_file(char *filename)
|
|
|
|
{
|
|
|
|
int fd = open(filename, O_WRONLY | O_CREAT, 0600);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open file");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-11 11:54:39 +01:00
|
|
|
if (!copy_fd_all(fd, 0))
|
|
|
|
exit(1);
|
2011-03-10 16:50:40 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_file_back(char * filename)
|
|
|
|
{
|
|
|
|
int fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open file");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-11 11:54:39 +01:00
|
|
|
if (!copy_fd_all(1, fd))
|
|
|
|
exit(1);
|
2011-03-10 16:50:40 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
2012-01-02 03:59:14 +01:00
|
|
|
struct stat stat_pre, stat_post, session_stat;
|
2011-03-10 16:50:40 +01:00
|
|
|
char *filename = get_filename();
|
2012-09-27 02:06:26 +02:00
|
|
|
int child, status, log_fd, null_fd;
|
2012-07-12 03:45:20 +02:00
|
|
|
char var[1024], val[4096];
|
|
|
|
FILE *env_file;
|
2012-01-02 03:59:14 +01:00
|
|
|
FILE *waiter_pidfile;
|
2011-03-10 16:50:40 +01:00
|
|
|
|
|
|
|
copy_file(filename);
|
|
|
|
if (stat(filename, &stat_pre)) {
|
|
|
|
perror("stat pre");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-02-25 06:48:29 +01:00
|
|
|
fprintf(stderr, "time=%s, waiting for qubes-session\n", gettime());
|
2012-01-02 03:59:14 +01:00
|
|
|
// wait for X server to starts (especially in DispVM)
|
|
|
|
if (stat("/tmp/qubes-session-env", &session_stat)) {
|
|
|
|
switch (child = fork()) {
|
|
|
|
case -1:
|
|
|
|
perror("fork");
|
|
|
|
exit(1);
|
|
|
|
case 0:
|
|
|
|
waiter_pidfile = fopen("/tmp/qubes-session-waiter", "a");
|
|
|
|
if (waiter_pidfile == NULL) {
|
|
|
|
perror("fopen waiter_pidfile");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fprintf(waiter_pidfile, "%d\n", getpid());
|
|
|
|
fclose(waiter_pidfile);
|
|
|
|
// check the second time, to prevent race
|
|
|
|
if (stat("/tmp/qubes-session-env", &session_stat)) {
|
|
|
|
// wait for qubes-session notify
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
default:
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
|
|
|
|
//propagate exit code from child
|
|
|
|
exit(WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-25 06:48:29 +01:00
|
|
|
fprintf(stderr, "time=%s, starting editor\n", gettime());
|
2011-10-17 02:08:53 +02:00
|
|
|
switch (child = fork()) {
|
|
|
|
case -1:
|
|
|
|
perror("fork");
|
|
|
|
exit(1);
|
|
|
|
case 0:
|
2012-09-27 02:06:26 +02:00
|
|
|
null_fd = open("/dev/null", O_RDONLY);
|
|
|
|
dup2(null_fd, 0);
|
|
|
|
close(null_fd);
|
2012-07-12 03:45:20 +02:00
|
|
|
|
|
|
|
env_file = fopen("/tmp/qubes-session-env", "r");
|
|
|
|
while(fscanf(env_file, "%1024[^=]=%4096[^\n]\n", var, val) == 2) {
|
|
|
|
setenv(var, val, 1);
|
|
|
|
}
|
|
|
|
fclose(env_file);
|
|
|
|
|
2011-10-17 02:08:53 +02:00
|
|
|
log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666);
|
|
|
|
if (log_fd == -1) {
|
|
|
|
perror("open /tmp/mimeopen.log");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dup2(log_fd, 1);
|
|
|
|
close(log_fd);
|
|
|
|
|
2013-11-14 21:38:27 +01:00
|
|
|
setenv("HOME", USER_HOME, 1);
|
2011-10-17 02:08:53 +02:00
|
|
|
setenv("DISPLAY", ":0", 1);
|
2013-11-14 21:38:27 +01:00
|
|
|
execl("/usr/bin/mimeopen", "mimeopen", "-n",
|
|
|
|
"--database", MIMEINFO_DATABASES, filename, (char*)NULL);
|
2011-10-17 02:08:53 +02:00
|
|
|
perror("execl");
|
|
|
|
exit(1);
|
|
|
|
default:
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
if (status != 0) {
|
2012-01-02 04:00:54 +01:00
|
|
|
char cmd[512];
|
2011-03-31 13:26:41 +02:00
|
|
|
#ifdef USE_KDIALOG
|
2012-01-02 04:00:54 +01:00
|
|
|
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);
|
2011-03-31 13:26:41 +02:00
|
|
|
#else
|
2012-01-02 04:00:54 +01:00
|
|
|
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);
|
2011-03-31 13:26:41 +02:00
|
|
|
#endif
|
2012-01-02 04:00:54 +01:00
|
|
|
system(cmd);
|
2011-10-17 02:08:53 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-31 13:26:41 +02:00
|
|
|
|
2011-03-10 16:50:40 +01:00
|
|
|
if (stat(filename, &stat_post)) {
|
|
|
|
perror("stat post");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (stat_pre.st_mtime != stat_post.st_mtime)
|
|
|
|
send_file_back(filename);
|
|
|
|
return 0;
|
|
|
|
}
|