⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dpmlib.c

📁 Linux动态电源管理库
💻 C
字号:
/* * dpmlib.c libdpm DPM helper functions. * * (c) 2003 MontaVista Software, Inc. This file is licensed under the * terms of the GNU Lesser General Public License version 2. This * program is licensed "as is" without any warranty of any kind, * whether express or implied. */#include <dpmlib.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <limits.h>#include <asm/page.h>#define SYSFS_PREFIX "/sys/dpm/"static intlibdpm_open(char *file, int flags){	char path[PATH_MAX];	int ret;	if ((ret = 	     snprintf(path, sizeof(path), "%s/%s", SYSFS_PREFIX, file)) < 0)		return ret;	ret = open(path, flags);	return ret;}static intlibdpm_read(char *file, char *buf, size_t bufsiz){	int fd;	int ret;	if ((fd = libdpm_open(file, O_RDONLY)) < 0)		return -1;	ret = read(fd, buf, bufsiz);	if ((ret >= 0) && (ret < bufsiz))		buf[ret] = '\0';#ifdef DEBUG	if (ret > 0)		printf("read %s: %s\n", file, buf);#endif	close(fd);	return ret;}static intlibdpm_write(char *file, char *cmd){	int fd;	int ret;#ifdef DEBUG	printf("write %s: %s\n", file, cmd);#endif	if ((fd = libdpm_open(file, O_WRONLY)) < 0)		return -1;	ret = write(fd, cmd, strlen(cmd));	close(fd);	return ret < 0 ? ret : 0;}static intlibdpm_init(){	return libdpm_write("control", "init\n");}static intlibdpm_terminate(){	return libdpm_write("control", "terminate\n");}static intlibdpm_disable(){	return libdpm_write("control", "disable\n");}static intlibdpm_enable(){	return libdpm_write("control", "enable\n");}static intlibdpm_set_policy(char *name){	return libdpm_write("policy/active", name);}static intlibdpm_destroy_policy(char *policy){	char file[256];	if (snprintf(file, sizeof(file), "policy/%s/control", policy) < 0)		return -1;	if (libdpm_write(file, "destroy") < 0)		return -1;	return 0;}static intlibdpm_set_state(char *name){	return libdpm_write("state/active", name);}static intlibdpm_get_task_state(pid_t pid, char *buf, size_t bufsiz){	int ret, fd;	char path[PATH_MAX];	if ((ret = 	     snprintf(path, sizeof(path), "/proc/%d/dpmstate", pid)) < 0)		return ret;		if ((fd = open(path, O_RDONLY)) < 0)		return fd;	ret = read(fd, buf, bufsiz);	close(fd);	if (ret >= 0)		buf[ret] = '\0';#ifdef DEBUG	if (ret > 0)		printf("read %s: %s\n", path, buf);#endif	if ((ret > 0) && (buf[ret-1] == '\n'))		buf[ret-1] = '\0';	return 0;}static intlibdpm_set_task_state(pid_t pid, char *statename){	int ret, fd;	char path[PATH_MAX];	if ((ret = 	     snprintf(path, sizeof(path), "/proc/%d/dpmstate", pid)) < 0)		return ret;		if ((fd = open(path, O_WRONLY)) < 0)		return fd;#ifdef DEBUG	printf("write %s: %s\n", path, statename);#endif	ret = write(fd, statename, strlen(statename));	close(fd);	return ret < 0 ? ret : 0;}/* * -------------------------------------------------------------------------- * DPM API section. */intdpm_init(void){	return libdpm_init();}intdpm_terminate(void){	return libdpm_terminate();}intdpm_set_state(char *statename){	return libdpm_set_state(statename);}intdpm_create_op(char *name, char *params){	char buf[PATH_MAX];	int ret;	/*	 * If the optional old-style list of params is given then	 * append 'em.	 */	if (params) {		if ((ret = snprintf(buf, sizeof(buf), "create %s %s\n", name, 				    params)) < 0)			return ret;	} else {		if ((ret = snprintf(buf, sizeof(buf), "create %s\n", name))		    < 0)			return ret;	}	return libdpm_write("op/control", buf);		    }intdpm_get_op_param(char *op, char *param, char *buf, size_t bufsiz){	char path[PATH_MAX];	int ret;	if ((ret = snprintf(path, sizeof(path),"op/%s/%s", op, param)) < 0)		return ret;	if ((ret = libdpm_read(path, buf, bufsiz)) > 0)		if ((ret > 0) && (buf[ret-1] == '\n'))			buf[--ret] = '\0';		return ret;}intdpm_set_op_param(char *op, char *param, int value){	char path[PATH_MAX];	char buf[20];	int ret;	if ((ret = snprintf(path, sizeof(path),"op/%s/%s", op, param)) < 0)		return ret;	if ((ret = snprintf(buf, sizeof(buf), "%d",  value)) < 0)		return ret;	return libdpm_write(path, buf);		    }intdpm_create_class(char *name, char *params){	char buf[PATH_MAX];	int ret;	if ((ret = snprintf(buf, sizeof(buf), "create %s %s\n", name, params))	    < 0)		return ret;	return libdpm_write("class/control", buf);		    }intdpm_create_policy(char *name, char *params){	char buf[PATH_MAX];	int ret;	/*	 * If the optional old-style list of ops/classes in state order	 * is given then append 'em.	 */	if (params) {		if ((ret = snprintf(buf, sizeof(buf), "create %s %s\n", name, 				    params)) < 0)			return ret;	} else {		if ((ret = snprintf(buf, sizeof(buf), "create %s\n", name))		    < 0)			return ret;	}	return libdpm_write("policy/control", buf);		    }intdpm_set_policy_state_map(char *policy, char *state, char *opclass){	char path[PATH_MAX];	int ret;	if ((ret = snprintf(path, sizeof(path),"policy/%s/%s", policy,			    state)) < 0)		return ret;	return libdpm_write(path, opclass);		    }intdpm_get_policy_state_map(char *policy, char *state, char *buf, size_t bufsiz){	char path[PATH_MAX];	int ret;	if ((ret = snprintf(path, sizeof(path),"policy/%s/%s", policy,			    state)) < 0)		return ret;	if ((ret = libdpm_read(path, buf, bufsiz)) > 0)		if ((ret > 0) && (buf[ret-1] == '\n'))			buf[--ret] = '\0';	return ret;}intdpm_get_active_policy(char *buf, size_t bufsiz){	int ret;	if ((ret = libdpm_read("policy/active", buf, bufsiz)) > 0) {		if ((ret > 0) && (buf[ret-1] == '\n'))			buf[--ret] = '\0';	}	return ret;}intdpm_set_active_policy(char *policy){	return libdpm_set_policy(policy);}/* * -------------------------------------------------------------------------- * sys_dpm() backward compatability section (removed) */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -