vm-file-editor.c 4.0 KB

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