gui-fatal.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #define _GNU_SOURCE
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <malloc.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <stdarg.h>
  10. static void fix_display(void)
  11. {
  12. setenv("DISPLAY", ":0", 1);
  13. }
  14. static void produce_message(const char * type, const char *fmt, va_list args)
  15. {
  16. char *dialog_msg;
  17. char buf[1024];
  18. (void) vsnprintf(buf, sizeof(buf), fmt, args);
  19. if (asprintf(&dialog_msg, "%s: %s: %s (error type: %s)",
  20. program_invocation_short_name, type, buf, strerror(errno)) < 0) {
  21. fprintf(stderr, "Failed to allocate memory for error message :(\n");
  22. return;
  23. }
  24. fprintf(stderr, "%s\n", dialog_msg);
  25. switch (fork()) {
  26. case -1:
  27. exit(1); //what else
  28. case 0:
  29. if (geteuid() == 0)
  30. if (setuid(getuid()) != 0)
  31. perror("setuid failed, calling kdialog/zenity as root");
  32. fix_display();
  33. #ifdef USE_KDIALOG
  34. execlp("/usr/bin/kdialog", "kdialog", "--sorry", dialog_msg, NULL);
  35. #else
  36. execlp("/usr/bin/zenity", "zenity", "--error", "--text", dialog_msg, NULL);
  37. #endif
  38. exit(1);
  39. default:;
  40. }
  41. free(dialog_msg);
  42. }
  43. void gui_fatal(const char *fmt, ...)
  44. {
  45. va_list args;
  46. va_start(args, fmt);
  47. produce_message("Fatal error", fmt, args);
  48. va_end(args);
  49. exit(1);
  50. }
  51. void qfile_gui_fatal(const char *fmt, va_list args) {
  52. produce_message("Fatal error", fmt, args);
  53. exit(1);
  54. }
  55. void gui_nonfatal(const char *fmt, ...)
  56. {
  57. va_list args;
  58. va_start(args, fmt);
  59. produce_message("Information", fmt, args);
  60. va_end(args);
  61. }