00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
00052
00053
00054 void write_to_proc_sleep(int value)
00055 {
00056 char tmp[256];
00057 int fd;
00058
00059
00060 if ((value<1) || (value>4))
00061 exit(1);
00062
00063
00064 snprintf(tmp,sizeof(tmp),"%d",value);
00065 tmp[sizeof(tmp)-1]=0;
00066
00067
00068
00069
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());
00078 exit(0);
00079 }
00080
00081
00082
00083
00084
00085 void run_program(const char *path)
00086 {
00087 struct stat sb;
00088 int err;
00089
00090 if (!path) exit(1);
00091 if (path[0] != '/') exit(1);
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());
00103 ::execl(path, NULL);
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);
00115 for (i = 1; i < argc; i++)
00116 if (strcmp(argv[i], "--suspend") == 0 || strcmp(argv[i], "-suspend") == 0) {
00117
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());
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());
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());
00224 exit(0);
00225 } else
00226
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
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
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
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());
00282 exit(0);
00283 } else {
00284 usage:
00285 setuid(getuid());
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 }