CVS commit by adridg: Factor out the code to run a helper script; this is prep-work for bug 93533 M +30 -13 acpi_helper.cpp 1.17 --- kdeutils/klaptopdaemon/acpi_helper.cpp #1.16:1.17 @@ -49,5 +49,8 @@ #define MAX_TOSHIBA_STRING 64 -int write_to_proc_sleep(int value) +/* Write a value to /proc/acpi/sleep, where value may be between + 1 and 4 (whatever they mean). Does not return; calls exit(). */ + +void write_to_proc_sleep(int value) { char tmp[256]; @@ -76,4 +79,29 @@ int write_to_proc_sleep(int value) } +/* Run the program @param path, if it exists and seems safe to do so. + Returns only if the program does not exist; if the program exists + and is unsafe, exit; if the program exists and is safe, run it + and never return. */ +void run_program(const char *path) +{ + struct stat sb; + int err; + + if (!path) exit(1); /* Bad pointer */ + if (path[0] != '/') exit(1); /* Not an absolute path */ + + if ((err = stat(path, &sb)) != 0 || sb.st_mode&S_IWOTH) { + if (err != 0) { + fprintf(stderr, "Can't find %s\n", path); + return; + } else { + fprintf(stderr, "%s is writeable by anyone - we don't trust it\n", path); + } + exit(1); + } + ::setuid(::geteuid()); // otherwise bash will throw it away + ::execl(path, NULL); // this is not KDE environment code + exit(0); +} int @@ -103,16 +131,5 @@ main(int argc, char **argv) } else if (strcmp(argv[i], "--software-suspend") == 0 || strcmp(argv[i], "-software-suspend") == 0) { - struct stat sb; - int err; - if ((err = stat("/usr/sbin/hibernate", &sb)) != 0 || sb.st_mode&S_IWOTH) { - if (err != 0) { - fprintf(stderr, "Can't find /usr/sbin/hibernate\n"); - } else { - fprintf(stderr, "/usr/sbin/hibernate is writeable by anyone - we don't trust it\n"); - } - exit(0); - } - ::setuid(::geteuid()); // otherwise bash will throw it away - ::execl("/usr/sbin/hibernate", NULL); // this is not KDE environment code + run_program("/usr/sbin/hibernate"); exit(0); } else