|
|||
Prex Home / Browse Source - Prex Version: 0.9.0 |
|||
root/usr/sbin/pmctrl/pmctrl.c/* [<][>][^][v][top][bottom][index][help] */DEFINITIONSThis source file includes following definitions.
1 /* 2 * Copyright (c) 2009, Kohsuke Ohtani 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * pmctrl.c - power management utility 32 * 33 * Required capabilities: 34 * CAP_POWERMGMT 35 */ 36 37 #include <sys/prex.h> 38 #include <sys/ioctl.h> 39 #include <ipc/pow.h> 40 #include <ipc/ipc.h> 41 42 #include <unistd.h> 43 #include <err.h> 44 #include <errno.h> 45 #include <stdlib.h> 46 #include <stdio.h> 47 #include <string.h> 48 #include <signal.h> 49 50 static void pmctrl_help(int, char **); 51 static void pmctrl_off(int, char **); 52 static void pmctrl_reboot(int, char **); 53 static void pmctrl_suspend(int, char **); 54 static void pmctrl_info(int, char **); 55 static void pmctrl_policy(int, char **); 56 static void pmctrl_sustime(int, char **); 57 static void pmctrl_dimtime(int, char **); 58 static void pmctrl_battery(int, char **); 59 static void pmctrl_null(int, char **); 60 61 struct cmdtab { 62 const char *cmd; 63 void (*func)(int, char **); 64 const char *usage; 65 }; 66 67 static const struct cmdtab cmdtab[] = { 68 { "off" ,pmctrl_off ,"Power off." }, 69 { "reboot" ,pmctrl_reboot ,"Reboot system." }, 70 { "suspend" ,pmctrl_suspend ,"Suspend system." }, 71 { "info" ,pmctrl_info ,"Disaplay power management information." }, 72 { "policy" ,pmctrl_policy ,"Set power policy." }, 73 { "sustime" ,pmctrl_sustime ,"Set timeout for suspend timer." }, 74 { "dimtime" ,pmctrl_dimtime ,"Set timeout for dim timer." }, 75 { "battery" ,pmctrl_battery ,"Show current battery level." }, 76 { "-?" ,pmctrl_help ,"This help." }, 77 { NULL ,pmctrl_null ,NULL }, 78 }; 79 80 static object_t powobj; 81 82 static void 83 pmctrl_null(int argc, char **argv) 84 { 85 } 86 87 static void 88 pmctrl_help(int argc, char **argv) 89 { 90 int i = 0; 91 92 fprintf(stderr, "usage: pmctrl command\n"); 93 fprintf(stderr, "commands:\n"); 94 while (cmdtab[i].cmd != NULL) { 95 if (cmdtab[i].usage) 96 fprintf(stderr, " %-8s -- %s\n", cmdtab[i].cmd, 97 cmdtab[i].usage); 98 i++; 99 } 100 } 101 102 /* 103 * User confirmation is required for some actions 104 * due to security reason. 105 */ 106 static void 107 pmctrl_confirm(char *action) 108 { 109 int ch, checkch; 110 111 printf("Do you want to %s the system now? (y/n) ", action); 112 checkch = ch = getchar(); 113 while (ch != '\n' && ch != EOF) 114 ch = getchar(); 115 if (checkch != 'y') 116 exit(1); 117 } 118 119 static void 120 pmctrl_off(int argc, char **argv) 121 { 122 struct msg m; 123 124 pmctrl_confirm("shutdown"); 125 126 printf("Shutdown system...\n"); 127 m.hdr.code = POW_SET_POWER; 128 m.data[0] = PWR_OFF; 129 msg_send(powobj, &m, sizeof(m)); 130 131 fprintf(stderr, "Shutdown failed!\n"); 132 } 133 134 static void 135 pmctrl_reboot(int argc, char **argv) 136 { 137 struct msg m; 138 139 pmctrl_confirm("reboot"); 140 141 printf("Reboot system...\n"); 142 m.hdr.code = POW_SET_POWER; 143 m.data[0] = PWR_REBOOT; 144 msg_send(powobj, &m, sizeof(m)); 145 146 fprintf(stderr, "Reboot failed!\n"); 147 } 148 149 static void 150 pmctrl_suspend(int argc, char **argv) 151 { 152 struct msg m; 153 154 pmctrl_confirm("suspend"); 155 156 printf("Suspend system...\n"); 157 m.hdr.code = POW_SET_POWER; 158 m.data[0] = PWR_SUSPEND; 159 msg_send(powobj, &m, sizeof(m)); 160 161 fprintf(stderr, "Suspend failed!\n"); 162 } 163 164 static void 165 pmctrl_info(int argc, char **argv) 166 { 167 struct msg m; 168 int policy; 169 int timeout; 170 171 m.hdr.code = POW_GET_POLICY; 172 msg_send(powobj, &m, sizeof(m)); 173 policy = m.data[0]; 174 printf("Power policy : %s mode\n", 175 policy == PM_PERFORMANCE ? "high performance" : "power save"); 176 177 m.hdr.code = POW_GET_SUSTMR; 178 msg_send(powobj, &m, sizeof(m)); 179 timeout = m.data[0]; 180 printf("Suspend timeout: %d sec\n", timeout); 181 182 m.hdr.code = POW_GET_DIMTMR; 183 msg_send(powobj, &m, sizeof(m)); 184 timeout = m.data[0]; 185 printf("Dim timeout : %d sec\n", timeout); 186 } 187 188 static void 189 pmctrl_policy(int argc, char **argv) 190 { 191 struct msg m; 192 193 if (argc != 3) { 194 fprintf(stderr, "Usage: pmctrl policy high|save\n"); 195 exit(1); 196 } 197 198 if (!strcmp(argv[2], "high")) 199 m.data[0] = PM_PERFORMANCE; 200 else if (!strcmp(argv[2], "save")) 201 m.data[0] = PM_POWERSAVE; 202 else { 203 fprintf(stderr, "Invalid policy\n"); 204 exit(1); 205 } 206 m.hdr.code = POW_SET_POLICY; 207 msg_send(powobj, &m, sizeof(m)); 208 } 209 210 static void 211 pmctrl_sustime(int argc, char **argv) 212 { 213 struct msg m; 214 int timeout; 215 216 if (argc < 3) 217 fprintf(stderr, "Usage: pmctrl sustime sec\n"); 218 else { 219 timeout = atoi(argv[2]); 220 221 m.hdr.code = POW_SET_SUSTMR; 222 m.data[0] = timeout; 223 msg_send(powobj, &m, sizeof(m)); 224 } 225 } 226 227 static void 228 pmctrl_dimtime(int argc, char **argv) 229 { 230 struct msg m; 231 int timeout; 232 233 if (argc < 3) 234 fprintf(stderr, "Usage: pmctrl dimtime sec\n"); 235 else { 236 timeout = atoi(argv[2]); 237 238 m.hdr.code = POW_SET_DIMTMR; 239 m.data[0] = timeout; 240 msg_send(powobj, &m, sizeof(m)); 241 } 242 } 243 244 static void 245 pmctrl_battery(int argc, char **argv) 246 { 247 248 fprintf(stderr, "Not supported...\n"); 249 } 250 251 int 252 main(int argc, char *argv[]) 253 { 254 int i = 0; 255 int found = 0; 256 257 if (argc < 2) { 258 pmctrl_help(1, NULL); 259 exit(1); 260 } 261 262 if (object_lookup("!pow", &powobj) != 0) { 263 fprintf(stderr, "No power server found\n"); 264 exit(1); 265 } 266 267 while (cmdtab[i].cmd != NULL) { 268 if (!strncmp(argv[1], cmdtab[i].cmd, LINE_MAX)) { 269 (cmdtab[i].func)(argc, argv); 270 found = 1; 271 break; 272 } 273 i++; 274 } 275 if (!found) 276 pmctrl_help(1, NULL); 277 exit(1); 278 } /* [<][>][^][v][top][bottom][index][help] */ | |||
Copyright© 2005-2009 Kohsuke Ohtani |