vm-file-editor.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <sys/stat.h>
  2. #include <sys/wait.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <ioall.h>
  9. #include "dvm2.h"
  10. #define USER_HOME "/home/user"
  11. #define MIMEINFO_DATABASES "/usr/share/mime:/usr/local/share:" USER_HOME "/.local/share:/usr/share/qubes/mime-override"
  12. char *gettime()
  13. {
  14. static char retbuf[60];
  15. struct timeval tv;
  16. gettimeofday(&tv, NULL);
  17. snprintf(retbuf, sizeof(retbuf), "%lld.%06lld",
  18. (long long) tv.tv_sec, (long long) tv.tv_usec);
  19. return retbuf;
  20. }
  21. char *get_filename()
  22. {
  23. char buf[DVM_FILENAME_SIZE];
  24. static char retname[sizeof(buf) + sizeof("/tmp/")];
  25. int i;
  26. if (!read_all(0, buf, sizeof(buf)))
  27. exit(1);
  28. buf[DVM_FILENAME_SIZE-1] = 0;
  29. if (index(buf, '/')) {
  30. fprintf(stderr, "filename contains /");
  31. exit(1);
  32. }
  33. for (i=0; buf[i]!=0; i++) {
  34. // replace some characters with _ (eg mimeopen have problems with some of them)
  35. if (index(" !?\"#$%^&*()[]<>;`~", buf[i]))
  36. buf[i]='_';
  37. }
  38. snprintf(retname, sizeof(retname), "/tmp/%s", buf);
  39. return retname;
  40. }
  41. void copy_file(char *filename)
  42. {
  43. int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
  44. if (fd < 0) {
  45. perror("open file");
  46. exit(1);
  47. }
  48. if (!copy_fd_all(fd, 0))
  49. exit(1);
  50. close(fd);
  51. }
  52. void send_file_back(char * filename)
  53. {
  54. int fd = open(filename, O_RDONLY);
  55. if (fd < 0) {
  56. perror("open file");
  57. exit(1);
  58. }
  59. if (!copy_fd_all(1, fd))
  60. exit(1);
  61. close(fd);
  62. close(1);
  63. }
  64. int
  65. main()
  66. {
  67. struct stat stat_pre, stat_post, session_stat;
  68. char *filename = get_filename();
  69. int child, status, log_fd, null_fd;
  70. char var[1024], val[4096];
  71. FILE *env_file;
  72. FILE *waiter_pidfile;
  73. copy_file(filename);
  74. if (stat(filename, &stat_pre)) {
  75. perror("stat pre");
  76. exit(1);
  77. }
  78. fprintf(stderr, "time=%s, waiting for qubes-session\n", gettime());
  79. // wait for X server to starts (especially in DispVM)
  80. if (stat("/tmp/qubes-session-env", &session_stat)) {
  81. switch (child = fork()) {
  82. case -1:
  83. perror("fork");
  84. exit(1);
  85. case 0:
  86. waiter_pidfile = fopen("/tmp/qubes-session-waiter", "a");
  87. if (waiter_pidfile == NULL) {
  88. perror("fopen waiter_pidfile");
  89. exit(1);
  90. }
  91. fprintf(waiter_pidfile, "%d\n", getpid());
  92. fclose(waiter_pidfile);
  93. // check the second time, to prevent race
  94. if (stat("/tmp/qubes-session-env", &session_stat)) {
  95. // wait for qubes-session notify
  96. pause();
  97. }
  98. exit(0);
  99. default:
  100. waitpid(child, &status, 0);
  101. if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
  102. //propagate exit code from child
  103. exit(WEXITSTATUS(status));
  104. }
  105. }
  106. }
  107. fprintf(stderr, "time=%s, starting editor\n", gettime());
  108. switch (child = fork()) {
  109. case -1:
  110. perror("fork");
  111. exit(1);
  112. case 0:
  113. null_fd = open("/dev/null", O_RDONLY);
  114. dup2(null_fd, 0);
  115. close(null_fd);
  116. env_file = fopen("/tmp/qubes-session-env", "r");
  117. while(fscanf(env_file, "%1024[^=]=%4096[^\n]\n", var, val) == 2) {
  118. setenv(var, val, 1);
  119. }
  120. fclose(env_file);
  121. log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666);
  122. if (log_fd == -1) {
  123. perror("open /tmp/mimeopen.log");
  124. exit(1);
  125. }
  126. dup2(log_fd, 1);
  127. close(log_fd);
  128. setenv("HOME", USER_HOME, 1);
  129. setenv("DISPLAY", ":0", 1);
  130. execl("/usr/bin/mimeopen", "mimeopen", "-n",
  131. "--database", MIMEINFO_DATABASES, filename, (char*)NULL);
  132. perror("execl");
  133. exit(1);
  134. default:
  135. waitpid(child, &status, 0);
  136. if (status != 0) {
  137. char cmd[512];
  138. #ifdef USE_KDIALOG
  139. snprintf(cmd, sizeof(cmd),
  140. "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);
  141. ("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);
  142. #else
  143. snprintf(cmd, sizeof(cmd),
  144. "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);
  145. #endif
  146. system(cmd);
  147. }
  148. }
  149. if (stat(filename, &stat_post)) {
  150. perror("stat post");
  151. exit(1);
  152. }
  153. if (stat_pre.st_mtime != stat_post.st_mtime)
  154. send_file_back(filename);
  155. return 0;
  156. }