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
|
/*
* Copyright (C) 2005-2006 Mattia Dongili<malattia@linux.it>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* EXEC Plugin
* -----------
*
* This plugin execute command on cpufreqd events. Command are serialized.
*
* !! WARNING !!
* starting tasks when cpufreqd switches between 2 profiles rapidly in
* succession may not be what you want, be really careful when defining
* exec_* directives.
*/
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "cpufreqd_plugin.h"
struct exec_cmd {
const char *cmd;
struct exec_cmd *next;
};
static struct exec_cmd *exe_q;
static pthread_mutex_t exe_q_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t exe_q_cond = PTHREAD_COND_INITIALIZER;
static pthread_t exe_thread;
static struct exec_cmd exe_exit_cmd = { .cmd = "", .next = NULL };
static int exec_parse (const char *line, void **obj) {
*obj = strdup(line);
if (*obj == NULL) {
clog(LOG_ERR, "Couldn't make enough room for the command '%s'.\n",
line);
return -1;
}
return 0;
}
/* Grab a command from the queue and execute it
*/
static void *queue_launcher (void __UNUSED__ *arg) {
struct exec_cmd *etemp = NULL;
pid_t child_pid = 0;
int child_ret = 0;
struct sigaction signal_action;
while (1) {
pthread_mutex_lock(&exe_q_mtx);
while (exe_q == NULL) {
pthread_cond_wait(&exe_q_cond, &exe_q_mtx);
}
/* shift queue */
etemp = exe_q;
exe_q = exe_q->next;
pthread_mutex_unlock(&exe_q_mtx);
if (etemp->cmd[0]) {
/* fork + excl */
clog(LOG_DEBUG, "EXE: %s\n", etemp->cmd);
switch (child_pid = fork()) {
case -1:
clog(LOG_ERR, "Unable to fork new process: %s\n",
strerror(errno));
break;
case 0:
clog(LOG_DEBUG, "child process, exec 'sh -c %s'\n",
etemp->cmd);
/* child */
/* reset signal handlers to default */
sigemptyset(&signal_action.sa_mask);
sigaddset(&signal_action.sa_mask, SIGTERM);
sigaddset(&signal_action.sa_mask, SIGINT);
sigaddset(&signal_action.sa_mask, SIGHUP);
sigaddset(&signal_action.sa_mask, SIGALRM);
signal_action.sa_flags = 0;
signal_action.sa_handler = SIG_DFL;
sigaction(SIGTERM, &signal_action, 0);
sigaction(SIGINT, &signal_action, 0);
sigaction(SIGHUP, &signal_action, 0);
sigaction(SIGALRM, &signal_action, 0);
/* TODO: test if file exists, is executable, etc.*/
/* perhaps we don't need that, beacause exit status will be logged*/
child_ret = execl("/bin/sh", "/bin/sh", "-c", etemp->cmd, NULL);
clog(LOG_ERR, "Unable to execl new process: %s\n",
strerror(errno));
exit(1);
default:
waitpid(child_pid, &child_ret, 0);
if(WIFEXITED(child_ret)) {
clog(LOG_NOTICE, "\"%s\" exited with status %d\n",
etemp->cmd, WEXITSTATUS(child_ret));
clog(LOG_DEBUG, "EXE: %s done\n", etemp->cmd);
} else if(WIFSIGNALED(child_ret)) {
clog(LOG_NOTICE, "\"%s\" exited on signal %d\n",
etemp->cmd, WTERMSIG(child_ret));
} else {
clog(LOG_WARNING, "\"%s\" exited with status %d\n",
etemp->cmd, child_ret);
}
}
free(etemp);
} else
break;
}
return NULL;
}
/* Add a command to the queue and wake the exec_thread
*/
static void exec_enqueue (const char *cmd) {
struct exec_cmd *etemp = NULL;
struct exec_cmd *loop = NULL;
pthread_mutex_lock(&exe_q_mtx);
etemp = calloc(1, sizeof(struct exec_cmd));
if (etemp == NULL) {
clog(LOG_ERR, "Couldn't enqueue command \"%s\".\n", cmd);
} else {
etemp->cmd = cmd;
if (exe_q == NULL) {
exe_q = etemp;
} else {
loop = exe_q;
while (loop->next)
loop = loop->next;
loop->next = etemp;
}
clog(LOG_DEBUG, "enqueued: %s\n", etemp->cmd);
pthread_cond_signal(&exe_q_cond);
}
pthread_mutex_unlock(&exe_q_mtx);
}
/* avoid being called for each cpu profile change,
* only act on the last one
*/
static int profile_pre_change_calls;
static int profile_post_change_calls;
static void exec_profile_pre_change(void __UNUSED__ *obj,
const struct cpufreq_policy __UNUSED__ *old,
const struct cpufreq_policy __UNUSED__ *new,
const unsigned int __UNUSED__ cpu) {
struct cpufreqd_info *cinfo = get_cpufreqd_info();
clog(LOG_DEBUG, "launch counter = %d\n", profile_pre_change_calls);
if (profile_pre_change_calls == 0 || cinfo->cpufreqd_mode == MODE_MANUAL)
exec_enqueue((char *)obj);
profile_pre_change_calls++;
}
static void exec_profile_post_change(void *obj,
const struct cpufreq_policy __UNUSED__ *old,
const struct cpufreq_policy __UNUSED__ *new,
const unsigned int __UNUSED__ cpu) {
struct cpufreqd_info *cinfo = get_cpufreqd_info();
clog(LOG_DEBUG, "launch counter = %d\n", profile_post_change_calls);
if (profile_post_change_calls == 0 || cinfo->cpufreqd_mode == MODE_MANUAL)
exec_enqueue((char *)obj);
profile_post_change_calls++;
}
static void exec_rule_change(void *obj,
const struct rule __UNUSED__ *old,
const struct rule __UNUSED__ *new) {
exec_enqueue((char *)obj);
}
static int exec_update(void) {
profile_pre_change_calls = 0;
profile_post_change_calls = 0;
return 0;
}
/* Launch the thread that will wait on the queue
* to have some command available.
*/
static int exec_init (void) {
int ret = 0;
/* launch exec thread */
if ((ret = pthread_create(&exe_thread, NULL, &queue_launcher, NULL)) != 0) {
clog(LOG_ERR, "Unable to launch thread: %s\n", strerror(ret));
return ret;
}
return ret;
}
static int exec_exit (void) {
int ret = 0;
struct exec_cmd *etemp = NULL;
pthread_mutex_lock(&exe_q_mtx);
/* push exit into queue */
exe_exit_cmd.next = exe_q;
exe_q = &exe_exit_cmd;
/* wake the thread */
pthread_cond_signal(&exe_q_cond);
pthread_mutex_unlock(&exe_q_mtx);
/* cleanup */
ret = pthread_join(exe_thread, NULL);
if (ret != 0) {
clog(LOG_ERR, "Couldn't join exec thread.\n");
}
/* free command list if any no need to acquire the mutex */
while (exe_q != NULL) {
etemp = exe_q;
exe_q = exe_q->next;
free(etemp);
}
return 0;
}
static struct cpufreqd_keyword kw[] = {
{
/* only define post change events */
.word = "exec_post",
.parse = exec_parse,
.evaluate = NULL,
.profile_pre_change = NULL,
.profile_post_change = &exec_profile_post_change,
.rule_pre_change = NULL,
.rule_post_change = &exec_rule_change,
.free = NULL,
},
{
/* only define pre change events */
.word = "exec_pre",
.parse = exec_parse,
.evaluate = NULL,
.profile_pre_change = &exec_profile_pre_change,
.profile_post_change = NULL,
.rule_pre_change = &exec_rule_change,
.rule_post_change = NULL,
.free = NULL,
},
{ .word = NULL, }
};
static struct cpufreqd_plugin plugin = {
.plugin_name = "exec",
.keywords = kw,
.plugin_init = exec_init,
.plugin_exit = exec_exit,
.plugin_update = &exec_update,
.plugin_conf = NULL,
.plugin_post_conf = NULL,
};
/*
* A cpufreqd plugin MUST define the following function to provide the
* core cpufreqd with the correct struct cpufreqd_plugin structure
*/
struct cpufreqd_plugin *create_plugin(void) {
return &plugin;
}
|