2011-03-15 13:00:12 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2013-12-30 11:35:46 +01:00
|
|
|
static void fix_display(void)
|
2011-03-15 13:00:12 +01:00
|
|
|
{
|
|
|
|
setenv("DISPLAY", ":0", 1);
|
|
|
|
}
|
|
|
|
|
2013-12-30 11:35:46 +01:00
|
|
|
static void produce_message(const char * type, const char *fmt, va_list args)
|
2011-03-15 13:00:12 +01:00
|
|
|
{
|
2011-03-31 13:26:41 +02:00
|
|
|
char *dialog_msg;
|
2011-03-15 13:00:12 +01:00
|
|
|
char buf[1024];
|
|
|
|
(void) vsnprintf(buf, sizeof(buf), fmt, args);
|
2014-04-22 00:56:52 +02:00
|
|
|
if (asprintf(&dialog_msg, "%s: %s: %s (error type: %s)",
|
|
|
|
program_invocation_short_name, type, buf, strerror(errno)) < 0) {
|
|
|
|
fprintf(stderr, "Failed to allocate memory for error message :(\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-12-29 14:06:21 +01:00
|
|
|
fprintf(stderr, "%s\n", dialog_msg);
|
2011-03-15 13:00:12 +01:00
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
exit(1); //what else
|
|
|
|
case 0:
|
2014-09-20 01:20:11 +02:00
|
|
|
if (geteuid() == 0)
|
|
|
|
setuid(getuid());
|
2011-03-15 13:00:12 +01:00
|
|
|
fix_display();
|
2011-03-31 13:26:41 +02:00
|
|
|
#ifdef USE_KDIALOG
|
2013-12-29 14:06:59 +01:00
|
|
|
execlp("/usr/bin/kdialog", "kdialog", "--sorry", dialog_msg, NULL);
|
2011-03-31 13:26:41 +02:00
|
|
|
#else
|
|
|
|
|
2013-12-29 14:06:59 +01:00
|
|
|
execlp("/usr/bin/zenity", "zenity", "--error", "--text", dialog_msg, NULL);
|
2011-03-31 13:26:41 +02:00
|
|
|
#endif
|
2011-03-15 13:00:12 +01:00
|
|
|
exit(1);
|
|
|
|
default:;
|
|
|
|
}
|
2013-12-29 14:07:17 +01:00
|
|
|
free(dialog_msg);
|
2011-03-15 13:00:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void gui_fatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
produce_message("Fatal error", fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gui_nonfatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
produce_message("Information", fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|