vm-file-editor.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 *get_filename()
  11. {
  12. char buf[DVM_FILENAME_SIZE];
  13. static char retname[sizeof(buf) + sizeof("/tmp/")];
  14. if (!read_all(0, buf, sizeof(buf)))
  15. exit(1);
  16. if (index(buf, '/')) {
  17. fprintf(stderr, "filename contains /");
  18. exit(1);
  19. }
  20. snprintf(retname, sizeof(retname), "/tmp/%s", buf);
  21. return retname;
  22. }
  23. void copy_file(char *filename)
  24. {
  25. int fd = open(filename, O_WRONLY | O_CREAT, 0600);
  26. if (fd < 0) {
  27. perror("open file");
  28. exit(1);
  29. }
  30. if (!copy_fd_all(fd, 0))
  31. exit(1);
  32. close(fd);
  33. }
  34. void send_file_back(char * filename)
  35. {
  36. int fd = open(filename, O_RDONLY);
  37. if (fd < 0) {
  38. perror("open file");
  39. exit(1);
  40. }
  41. if (!copy_fd_all(1, fd))
  42. exit(1);
  43. close(fd);
  44. }
  45. int
  46. main()
  47. {
  48. struct stat stat_pre, stat_post, session_stat;
  49. char *filename = get_filename();
  50. int child, status, log_fd;
  51. FILE *waiter_pidfile;
  52. copy_file(filename);
  53. if (stat(filename, &stat_pre)) {
  54. perror("stat pre");
  55. exit(1);
  56. }
  57. // wait for X server to starts (especially in DispVM)
  58. if (stat("/tmp/qubes-session-env", &session_stat)) {
  59. switch (child = fork()) {
  60. case -1:
  61. perror("fork");
  62. exit(1);
  63. case 0:
  64. waiter_pidfile = fopen("/tmp/qubes-session-waiter", "a");
  65. if (waiter_pidfile == NULL) {
  66. perror("fopen waiter_pidfile");
  67. exit(1);
  68. }
  69. fprintf(waiter_pidfile, "%d\n", getpid());
  70. fclose(waiter_pidfile);
  71. // check the second time, to prevent race
  72. if (stat("/tmp/qubes-session-env", &session_stat)) {
  73. // wait for qubes-session notify
  74. pause();
  75. }
  76. exit(0);
  77. default:
  78. waitpid(child, &status, 0);
  79. if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
  80. //propagate exit code from child
  81. exit(WEXITSTATUS(status));
  82. }
  83. }
  84. }
  85. switch (child = fork()) {
  86. case -1:
  87. perror("fork");
  88. exit(1);
  89. case 0:
  90. close(0);
  91. log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666);
  92. if (log_fd == -1) {
  93. perror("open /tmp/mimeopen.log");
  94. exit(1);
  95. }
  96. dup2(log_fd, 1);
  97. dup2(log_fd, 2);
  98. close(log_fd);
  99. setenv("HOME", "/home/user", 1);
  100. setenv("DISPLAY", ":0", 1);
  101. execl("/usr/bin/mimeopen", "mimeopen", "-n", "-M", filename, (char*)NULL);
  102. perror("execl");
  103. exit(1);
  104. default:
  105. waitpid(child, &status, 0);
  106. if (status != 0) {
  107. char cmd[512];
  108. #ifdef USE_KDIALOG
  109. snprintf(cmd, sizeof(cmd),
  110. "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);
  111. ("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);
  112. #else
  113. snprintf(cmd, sizeof(cmd),
  114. "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);
  115. #endif
  116. system(cmd);
  117. }
  118. }
  119. if (stat(filename, &stat_post)) {
  120. perror("stat post");
  121. exit(1);
  122. }
  123. if (stat_pre.st_mtime != stat_post.st_mtime)
  124. send_file_back(filename);
  125. return 0;
  126. }