From kde-commits Tue Apr 05 20:43:20 2005 From: Adriaan de Groot Date: Tue, 05 Apr 2005 20:43:20 +0000 To: kde-commits Subject: kdeutils/klaptopdaemon Message-Id: <20050405204320.9DAB5492 () office ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=111273381106893 CVS commit by adridg: Factor out the writes to /proc/acpi/sleep M +33 -33 acpi_helper.cpp 1.16 --- kdeutils/klaptopdaemon/acpi_helper.cpp #1.15:1.16 @@ -49,14 +49,20 @@ #define MAX_TOSHIBA_STRING 64 -int -main(int argc, char **argv) +int write_to_proc_sleep(int value) { + char tmp[256]; int fd; - int i; - int toshibalcd_val = 0; - ::close(0); // we're setuid - this is just in case - for (i = 1; i < argc; i++) - if (strcmp(argv[i], "--suspend") == 0 || strcmp(argv[i], "-suspend") == 0) { + /* Sanity check value */ + if ((value<1) || (value>4)) + exit(1); + + /* Convert value to string */ + snprintf(tmp,sizeof(tmp),"%d",value); + tmp[sizeof(tmp)-1]=0; + + /* Broken imitation of typing sync sync + on the command line before shutting down the machine; + part of the lore of UNIX machines. */ sync(); sync(); @@ -64,40 +70,34 @@ main(int argc, char **argv) if (fd < 0) exit(1); - write(fd, "3", 1); + write(fd, tmp, 1); close(fd); setuid(getuid()); // drop all priority asap exit(0); +} + + +int +main(int argc, char **argv) +{ + int fd; + int i; + int toshibalcd_val = 0; + + ::close(0); // we're setuid - this is just in case + for (i = 1; i < argc; i++) + if (strcmp(argv[i], "--suspend") == 0 || strcmp(argv[i], "-suspend") == 0) { + write_to_proc_sleep(3); + exit(0); } else if (strcmp(argv[i], "--standby") == 0 || strcmp(argv[i], "-standby") == 0) { - sync(); - sync(); - fd = open("/proc/acpi/sleep", O_RDWR); - if (fd < 0) - exit(1); - write(fd, "1", 1); - close(fd); - setuid(getuid()); // drop all priority asap + write_to_proc_sleep(1); exit(0); } else if (strcmp(argv[i], "--standby2") == 0 || strcmp(argv[i], "-standby2") == 0) { - sync(); - sync(); - fd = open("/proc/acpi/sleep", O_RDWR); - if (fd < 0) - exit(1); - write(fd, "2", 1); - close(fd); - setuid(getuid()); // drop all priority asap + write_to_proc_sleep(2); exit(0); } else if (strcmp(argv[i], "--hibernate") == 0 || strcmp(argv[i], "-hibernate") == 0) { - sync(); - sync(); - fd = open("/proc/acpi/sleep", O_RDWR); - if (fd < 0) - exit(1); - write(fd, "4", 1); - close(fd); - setuid(getuid()); // drop all priority asap + write_to_proc_sleep(4); exit(0); } else