gui-fatal.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. setuid(getuid());
  31. fix_display();
  32. #ifdef USE_KDIALOG
  33. execlp("/usr/bin/kdialog", "kdialog", "--sorry", dialog_msg, NULL);
  34. #else
  35. execlp("/usr/bin/zenity", "zenity", "--error", "--text", dialog_msg, NULL);
  36. #endif
  37. exit(1);
  38. default:;
  39. }
  40. free(dialog_msg);
  41. }
  42. void gui_fatal(const char *fmt, ...)
  43. {
  44. va_list args;
  45. va_start(args, fmt);
  46. produce_message("Fatal error", fmt, args);
  47. va_end(args);
  48. exit(1);
  49. }
  50. void gui_nonfatal(const char *fmt, ...)
  51. {
  52. va_list args;
  53. va_start(args, fmt);
  54. produce_message("Information", fmt, args);
  55. va_end(args);
  56. }