• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

klaptopdaemon

acpi_helper.cpp

Go to the documentation of this file.
00001 /*
00002  * acpi_helper.cpp - acpi helper 
00003  *
00004  * Copyright (c) 2002 Paul Campbell <paul@taniwha.com>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 //
00022 //  README!!
00023 //
00024 //  This file contains code that is intended to be run setuid root
00025 //  (only if the end user enables it themselves, it's not set that
00026 //  way as part of a standard KDE build).
00027 //
00028 //  Because of this this code should be simple and easily visually
00029 //  inspected for security holes and/or bugs - if you feel the need
00030 //  to change this file please get someone else to review your work
00031 //  (I'll happily do it for you - mail me at paul@taniwha.com, please
00032 //  review mine!)
00033 //
00034 //  I recommend the following practices here - both for safety and
00035 //  transparency:
00036 //
00037 //      - check all array references (snprintf/strncpy etc)
00038 //
00039 //      - avoid malloc/new calls and pointers  too if possible
00040 //
00041 
00042 #include <stdio.h>
00043 #include <fcntl.h>
00044 #include <stdlib.h>
00045 #include <unistd.h>
00046 #include <sys/stat.h>
00047 #include <string.h>
00048 
00049 #define MAX_TOSHIBA_STRING 64
00050 
00051 /* Write a value to /proc/acpi/sleep, where value may be between
00052    1 and 4 (whatever they mean). Does not return; calls exit(). */
00053 
00054 void write_to_proc_sleep(int value)
00055 {
00056     char tmp[256];
00057     int fd;
00058 
00059     /* Sanity check value */
00060     if ((value<1) || (value>4))
00061         exit(1);
00062 
00063     /* Convert value to string */
00064     snprintf(tmp,sizeof(tmp),"%d",value);
00065     tmp[sizeof(tmp)-1]=0;
00066 
00067     /* Broken imitation of typing sync <enter> sync <enter>
00068        on the command line before shutting down the machine;
00069        part of the lore of UNIX machines. */
00070     sync();
00071     sync();
00072     fd = open("/proc/acpi/sleep", O_RDWR);
00073     if (fd < 0)
00074         exit(1);
00075     write(fd, tmp, 1);
00076     close(fd);
00077     setuid(getuid());   // drop all priority asap
00078     exit(0);
00079 }
00080 
00081 /* Run the program @param path, if it exists and seems safe to do so.
00082    Returns only if the program does not exist; if the program exists
00083    and is unsafe, exit; if the program exists and is safe, run it
00084    and never return. */
00085 void run_program(const char *path)
00086 {
00087     struct stat sb;
00088     int err;
00089 
00090     if (!path) exit(1); /* Bad pointer */
00091     if (path[0] != '/') exit(1); /* Not an absolute path */
00092 
00093     if ((err = stat(path, &sb)) != 0 || sb.st_mode&S_IWOTH) {
00094         if (err != 0) {
00095             fprintf(stderr, "Can't find %s\n", path);
00096             return;
00097         } else {
00098             fprintf(stderr, "%s is writeable by anyone - we don't trust it\n", path);
00099         }
00100         exit(1);
00101     }
00102     ::setuid(::geteuid());                  // otherwise bash will throw it away
00103     ::execl(path, NULL);    // this is not KDE environment code 
00104     exit(0);
00105 }
00106 
00107 int
00108 main(int argc, char **argv)
00109 {
00110     int fd;
00111     int i;
00112     int toshibalcd_val = 0;
00113 
00114     ::close(0); // we're setuid - this is just in case
00115     for (i = 1; i < argc; i++)
00116     if (strcmp(argv[i], "--suspend") == 0 || strcmp(argv[i], "-suspend") == 0) {
00117         /* Returns only if suspend does not exist. */
00118         run_program("/usr/sbin/suspend");
00119         write_to_proc_sleep(3);
00120         exit(0);
00121     } else
00122     if (strcmp(argv[i], "--standby") == 0 || strcmp(argv[i], "-standby") == 0) {
00123         write_to_proc_sleep(1);
00124         exit(0);
00125     } else
00126     if (strcmp(argv[i], "--standby2") == 0 || strcmp(argv[i], "-standby2") == 0) {
00127         write_to_proc_sleep(2);
00128         exit(0);
00129     } else
00130     if (strcmp(argv[i], "--hibernate") == 0 || strcmp(argv[i], "-hibernate") == 0) {
00131         run_program("/usr/sbin/hibernate");
00132         write_to_proc_sleep(4);
00133         exit(0);
00134     } else
00135     if (strcmp(argv[i], "--software-suspend") == 0 || strcmp(argv[i], "-software-suspend") == 0) {
00136         run_program("/usr/sbin/hibernate");
00137         exit(0);
00138     } else
00139     if (strcmp(argv[i], "--throttling") == 0 || strcmp(argv[i], "-throttling") == 0) {
00140         int val;
00141         char tmp[256];
00142 
00143         i++;
00144         if (i >= argc) 
00145             break;
00146         if (strlen(argv[i]) > 50 || strchr(argv[i], '.'))
00147             break;
00148         snprintf(tmp, sizeof(tmp), "/proc/acpi/processor/%s/throttling", argv[i]);
00149         tmp[sizeof(tmp)-1] = 0;
00150         i++;
00151         if (i >= argc) 
00152             break;
00153         val= atoi(argv[i]);
00154         if (val < 0)
00155             break;
00156         sync();
00157         sync();
00158         fd = open(tmp, O_RDWR);
00159         if (fd < 0)
00160             exit(1);
00161         snprintf(tmp, sizeof(tmp), "%d", val);
00162         write(fd, tmp, strlen(tmp));
00163         close(fd);
00164             setuid(getuid());   // drop all priority asap
00165         exit(0);
00166     } else
00167     if (strcmp(argv[i], "--performance") == 0 || strcmp(argv[i], "-performance") == 0) {
00168         int val;
00169         char tmp[256];
00170 
00171         i++;
00172         if (i >= argc) 
00173             break;
00174         if (strlen(argv[i]) > 50 || strchr(argv[i], '.'))
00175             break;
00176         snprintf(tmp, sizeof(tmp), "/proc/acpi/processor/%s/performance", argv[i]);
00177         tmp[sizeof(tmp)-1] = 0;
00178         i++;
00179         if (i >= argc) 
00180             break;
00181         val= atoi(argv[i]);
00182         if (val < 0)
00183             break;
00184         sync();
00185         sync();
00186         fd = open(tmp, O_RDWR);
00187         if (fd < 0)
00188             exit(1);
00189         snprintf(tmp, sizeof(tmp), "%d", val);
00190         write(fd, tmp, strlen(tmp));
00191         close(fd);
00192             setuid(getuid());   // drop all priority asap
00193         exit(0);
00194     } else
00195     if (strcmp(argv[i], "--toshibalcd") == 0 || strcmp(argv[i], "-toshibalcd") == 0) {
00196         
00197         i++;
00198         if (i >= argc) 
00199             break;
00200         toshibalcd_val= atoi(argv[i]);
00201         if (toshibalcd_val < 0)
00202             toshibalcd_val = 0;
00203         if (toshibalcd_val > 7)
00204             toshibalcd_val = 7;
00205         fd = open("/proc/acpi/TOSHIBA1/lcd", O_RDWR);
00206         if (fd >= 0 ) {
00207             char c;
00208             
00209             c = '0'+toshibalcd_val;
00210             write(fd, &c, 1);
00211             close(fd);
00212         } else {
00213             fd = open("/proc/acpi/toshiba/lcd", O_RDWR);
00214             if (fd >= 0) {
00215             char str[MAX_TOSHIBA_STRING];
00216             
00217             snprintf(str,sizeof(str),"brightness : %d",toshibalcd_val);
00218             str[sizeof(str)-1]=0;
00219             write(fd,str,strlen(str));  
00220             close(fd);
00221             }
00222         }
00223             setuid(getuid());   // drop all priority asap
00224         exit(0);
00225     } else
00226     // CPUFreq support
00227     if (strncmp(argv[i], "--cpufreq", 9) == 0 || strncmp(argv[i], "-cpufreq", 8) == 0) {
00228         if ((i+1) >= argc)
00229             break;
00230         if (strlen(argv[i+1]) > 50 || strchr(argv[i+1], '.'))
00231             break;
00232         int val;
00233         char tmp[256];
00234         // CPUFreq support for the interface of the 2.4 kernell (/proc/sys/cpu/N/)
00235         if (strcmp(argv[i], "--cpufreq-24") == 0 || strcmp(argv[i], "-cpufreq-24") == 0) {
00236             ++i;
00237             snprintf(tmp, sizeof(tmp), "/proc/sys/cpu/%s/speed", argv[i]);
00238             tmp[sizeof(tmp)-1] = 0;
00239             ++i;
00240             if (i >= argc)
00241                 break;
00242             val = atoi(argv[i]);
00243             if (val < 0)
00244                 break;
00245             fd = open(tmp, O_WRONLY);
00246             if (fd < 0)
00247                 exit(1);
00248             snprintf(tmp, sizeof(tmp), "%d", val);
00249             write(fd, tmp, strlen(tmp));
00250         } else
00251         // CPUFreq support for the interface of the 2.5 kernel (/proc/cpufreq)
00252         if (strcmp(argv[i], "--cpufreq-25") == 0 || strcmp(argv[i], "-cpufreq-25") == 0) {
00253             ++i;
00254             snprintf(tmp, sizeof(tmp), "%s", argv[i]);
00255             tmp[sizeof(tmp)-1] = 0;
00256             fd = open("/proc/cpufreq", O_WRONLY);
00257             if (fd < 0)
00258                 exit(1);
00259             write(fd, tmp, strlen(tmp));
00260         } else
00261         // CPUFreq support fot the sysfs interface of the 2.5 kernel (/sys/devices/sys/cpuN/cpufreq/)
00262         if (strcmp(argv[i], "--cpufreq-sysfs") == 0 || strcmp(argv[i], "-cpufreq-sysfs") == 0) {
00263             ++i;
00264             snprintf(tmp, sizeof(tmp), "/sys/devices/system/cpu/%s/cpufreq/scaling_governor", argv[i]);
00265             tmp[sizeof(tmp)-1] = 0;
00266             ++i;
00267             if (i >= argc)
00268                 break;
00269             fd = open(tmp, O_WRONLY);
00270             if (fd < 0)
00271                 exit(1);
00272             if (strlen(argv[i]) > 50)
00273                 break;
00274             snprintf(tmp, sizeof(tmp), "%s", argv[i]);
00275             tmp[sizeof(tmp)-1] = 0;
00276             write(fd, tmp, strlen(tmp));
00277         } else {
00278             break;
00279         }
00280         close(fd);
00281         setuid(getuid()); // drop all priority asap
00282         exit(0);
00283     } else {
00284 usage:
00285             setuid(getuid());   // drop all priority asap
00286         fprintf(stderr, "Usage: %s [--suspend] [--standby] [--hibernate][--software-suspend][--toshibalcd N][--performance CPU N][--throttling CPU N][--cpufreq-[24|25|sysfs]]\n", argv[0]);
00287         exit(1);
00288     }
00289     goto usage;
00290 }

klaptopdaemon

Skip menu "klaptopdaemon"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdelirc
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • kjots
  • klaptopdaemon
  • kmilo
  • ksim
  • ktimer
  • kwallet
  • superkaramba
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal