vm-file-editor.c 4.1 KB

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