1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
* acpi_helper.cpp - acpi helper
*
* Copyright (c) 2002 Paul Campbell <paul@taniwha.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//
// README!!
//
// This file contains code that is intended to be run setuid root
// (only if the end user enables it themselves, it's not set that
// way as part of a standard KDE build).
//
// Because of this this code should be simple and easily visually
// inspected for security holes and/or bugs - if you feel the need
// to change this file please get someone else to review your work
// (I'll happily do it for you - mail me at paul@taniwha.com, please
// review mine!)
//
// I recommend the following practices here - both for safety and
// transparency:
//
// - check all array references (snprintf/strncpy etc)
//
// - avoid malloc/new calls and pointers too if possible
//
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#define MAX_TOSHIBA_STRING 64
/* 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];
int fd;
/* 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 <enter> sync <enter>
on the command line before shutting down the machine;
part of the lore of UNIX machines. */
sync();
sync();
fd = open("/proc/acpi/sleep", O_RDWR);
if (fd < 0)
exit(1);
write(fd, tmp, 1);
close(fd);
setuid(getuid()); // drop all priority asap
exit(0);
}
/* Write string to new acpi power interface */
void write_to_power(const char * str)
{
int fd;
/* Broken imitation of typing sync <enter> sync <enter>
on the command line before shutting down the machine;
part of the lore of UNIX machines. */
sync();
sync();
fd = open("/sys/power/state", O_RDWR);
if (fd < 0)
exit(1);
write(fd, str, strlen(str));
close(fd);
setuid(getuid()); // drop all priority asap
exit(0);
}
/* 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
main(int argc, char **argv)
{
int fd;
int i;
int toshibalcd_val = 0;
bool useSysPower=false;
fd = open("/sys/power/state", O_RDWR);
if (fd >= 0)
useSysPower=true;
close(fd);
::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) {
/* Returns only if suspend does not exist. */
run_program("/usr/sbin/suspend");
if (useSysPower)
write_to_power("mem");
else
write_to_proc_sleep(3);
exit(0);
} else
if (strcmp(argv[i], "--standby") == 0 || strcmp(argv[i], "-standby") == 0) {
if (useSysPower)
write_to_power("standby");
else
write_to_proc_sleep(1);
exit(0);
} else
if (strcmp(argv[i], "--standby2") == 0 || strcmp(argv[i], "-standby2") == 0) {
write_to_proc_sleep(2);
exit(0);
} else
if (strcmp(argv[i], "--hibernate") == 0 || strcmp(argv[i], "-hibernate") == 0) {
run_program("/usr/sbin/hibernate");
if (useSysPower)
write_to_power("disk");
else
write_to_proc_sleep(4);
exit(0);
} else
if (strcmp(argv[i], "--software-suspend") == 0 || strcmp(argv[i], "-software-suspend") == 0) {
run_program("/usr/sbin/hibernate");
exit(0);
} else
if (strcmp(argv[i], "--throttling") == 0 || strcmp(argv[i], "-throttling") == 0) {
int val;
char tmp[256];
i++;
if (i >= argc)
break;
if (strlen(argv[i]) > 50 || strchr(argv[i], '.'))
break;
snprintf(tmp, sizeof(tmp), "/proc/acpi/processor/%s/throttling", argv[i]);
tmp[sizeof(tmp)-1] = 0;
i++;
if (i >= argc)
break;
val= atoi(argv[i]);
if (val < 0)
break;
sync();
sync();
fd = open(tmp, O_RDWR);
if (fd < 0)
exit(1);
snprintf(tmp, sizeof(tmp), "%d", val);
write(fd, tmp, strlen(tmp));
close(fd);
setuid(getuid()); // drop all priority asap
exit(0);
} else
if (strcmp(argv[i], "--performance") == 0 || strcmp(argv[i], "-performance") == 0) {
int val;
char tmp[256];
i++;
if (i >= argc)
break;
if (strlen(argv[i]) > 50 || strchr(argv[i], '.'))
break;
snprintf(tmp, sizeof(tmp), "/proc/acpi/processor/%s/performance", argv[i]);
tmp[sizeof(tmp)-1] = 0;
i++;
if (i >= argc)
break;
val= atoi(argv[i]);
if (val < 0)
break;
sync();
sync();
fd = open(tmp, O_RDWR);
if (fd < 0)
exit(1);
snprintf(tmp, sizeof(tmp), "%d", val);
write(fd, tmp, strlen(tmp));
close(fd);
setuid(getuid()); // drop all priority asap
exit(0);
} else
if (strcmp(argv[i], "--toshibalcd") == 0 || strcmp(argv[i], "-toshibalcd") == 0) {
i++;
if (i >= argc)
break;
toshibalcd_val= atoi(argv[i]);
if (toshibalcd_val < 0)
toshibalcd_val = 0;
if (toshibalcd_val > 7)
toshibalcd_val = 7;
fd = open("/proc/acpi/TOSHIBA1/lcd", O_RDWR);
if (fd >= 0 ) {
char c;
c = '0'+toshibalcd_val;
write(fd, &c, 1);
close(fd);
} else {
fd = open("/proc/acpi/toshiba/lcd", O_RDWR);
if (fd >= 0) {
char str[MAX_TOSHIBA_STRING];
snprintf(str,sizeof(str),"brightness : %d",toshibalcd_val);
str[sizeof(str)-1]=0;
write(fd,str,strlen(str));
close(fd);
}
}
setuid(getuid()); // drop all priority asap
exit(0);
} else
// CPUFreq support
if (strncmp(argv[i], "--cpufreq", 9) == 0 || strncmp(argv[i], "-cpufreq", 8) == 0) {
if ((i+1) >= argc)
break;
if (strlen(argv[i+1]) > 50 || strchr(argv[i+1], '.'))
break;
int val;
char tmp[256];
// CPUFreq support for the interface of the 2.4 kernell (/proc/sys/cpu/N/)
if (strcmp(argv[i], "--cpufreq-24") == 0 || strcmp(argv[i], "-cpufreq-24") == 0) {
++i;
snprintf(tmp, sizeof(tmp), "/proc/sys/cpu/%s/speed", argv[i]);
tmp[sizeof(tmp)-1] = 0;
++i;
if (i >= argc)
break;
val = atoi(argv[i]);
if (val < 0)
break;
fd = open(tmp, O_WRONLY);
if (fd < 0)
exit(1);
snprintf(tmp, sizeof(tmp), "%d", val);
write(fd, tmp, strlen(tmp));
} else
// CPUFreq support for the interface of the 2.5 kernel (/proc/cpufreq)
if (strcmp(argv[i], "--cpufreq-25") == 0 || strcmp(argv[i], "-cpufreq-25") == 0) {
++i;
snprintf(tmp, sizeof(tmp), "%s", argv[i]);
tmp[sizeof(tmp)-1] = 0;
fd = open("/proc/cpufreq", O_WRONLY);
if (fd < 0)
exit(1);
write(fd, tmp, strlen(tmp));
} else
// CPUFreq support fot the sysfs interface of the 2.5 kernel (/sys/devices/sys/cpuN/cpufreq/)
if (strcmp(argv[i], "--cpufreq-sysfs") == 0 || strcmp(argv[i], "-cpufreq-sysfs") == 0) {
++i;
snprintf(tmp, sizeof(tmp), "/sys/devices/system/cpu/%s/cpufreq/scaling_governor", argv[i]);
tmp[sizeof(tmp)-1] = 0;
++i;
if (i >= argc)
break;
fd = open(tmp, O_WRONLY);
if (fd < 0)
exit(1);
if (strlen(argv[i]) > 50)
break;
snprintf(tmp, sizeof(tmp), "%s", argv[i]);
tmp[sizeof(tmp)-1] = 0;
write(fd, tmp, strlen(tmp));
} else {
break;
}
close(fd);
setuid(getuid()); // drop all priority asap
exit(0);
} else {
usage:
setuid(getuid()); // drop all priority asap
fprintf(stderr, "Usage: %s [--suspend] [--standby] [--hibernate][--software-suspend][--toshibalcd N][--performance CPU N][--throttling CPU N][--cpufreq-[24|25|sysfs]]\n", argv[0]);
exit(1);
}
goto usage;
}
|