2
* main.cpp: Thermal Daemon entry point
4
* Copyright (C) 2012 Intel Corporation. All rights reserved.
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License version
8
* 2 or later as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
* Author Name <Srinivas.Pandruvada@linux.intel.com>
23
* This is the main entry point for thermal daemon. This has main function
24
* which parses command line arguments, set up dbus server and log related
28
/* This implements main() function. This will parse command line options and
29
* a new instance of cthd_engine object. By default it will create a engine
30
* which uses dts engine, which DTS sensor and use P states to control
31
* temperature, without any configuration. Alternatively if the
32
* thermal-conf.xml has exact UUID match then it can use the zones and
33
* cooling devices defined it to control thermals. This file will allow fine
34
* tune ACPI thermal config or create new thermal config using custom
36
* Dbus interface allows user to switch between active/passive thermal controls
37
* if the thermal-conf.xml defines parameters.
43
#include "thd_preference.h"
44
#include "thd_engine.h"
45
#include "thd_engine_default.h"
46
#include "thd_parse.h"
49
#if !defined(TD_DIST_VERSION)
50
#define TD_DIST_VERSION PACKAGE_VERSION
61
GType pref_object_get_type(void);
62
#define MAX_DBUS_REPLY_STR_LEN 20
63
#define PREF_TYPE_OBJECT (pref_object_get_type())
64
G_DEFINE_TYPE(PrefObject, pref_object, G_TYPE_OBJECT)
66
gboolean thd_dbus_interface_calibrate(PrefObject *obj, GError **error);
67
gboolean thd_dbus_interface_terminate(PrefObject *obj, GError **error);
68
gboolean thd_dbus_interface_set_current_preference(PrefObject *obj, gchar *pref,
70
gboolean thd_dbus_interface_get_current_preference(PrefObject *obj,
71
gchar **pref_out, GError **error);
72
gboolean thd_dbus_interface_set_user_max_temperature(PrefObject *obj,
73
gchar *zone_name, gchar *temperature, GError **error);
74
// This is a generated file, which expects the above prototypes
75
#include "thd-dbus-interface.h"
78
static int thd_log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL
79
| G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO;
82
static gboolean thd_daemonize;
85
static gboolean dbus_enable;
88
int thd_poll_interval = 4; //in seconds
91
static gboolean ignore_cpuid_check = false;
94
cthd_engine *thd_engine;
96
gboolean exclusive_control = FALSE;
98
// DBUS Related functions
100
// Dbus object initialization
101
static void pref_object_init(PrefObject *obj) {
102
g_assert(obj != NULL);
105
// Dbus object class initialization
106
static void pref_object_class_init(PrefObjectClass *_class) {
107
g_assert(_class != NULL);
109
dbus_g_object_type_install_info(PREF_TYPE_OBJECT,
110
&dbus_glib_thd_dbus_interface_object_info);
113
// Callback function called to inform a sent value via dbus
114
gboolean thd_dbus_interface_set_current_preference(PrefObject *obj, gchar *pref,
118
thd_log_debug("thd_dbus_interface_set_current_preference %s\n",
120
g_assert(obj != NULL);
121
cthd_preference thd_pref;
122
ret = thd_pref.set_preference((char*) pref);
123
thd_engine->send_message(PREF_CHANGED, 0, NULL);
128
// Callback function called to get value via dbus
129
gboolean thd_dbus_interface_get_current_preference(PrefObject *obj,
130
gchar **pref_out, GError **error) {
131
thd_log_debug("thd_dbus_interface_get_current_preference\n");
132
g_assert(obj != NULL);
134
static char *pref_str;
136
pref_str = g_new(char, MAX_DBUS_REPLY_STR_LEN);
141
cthd_preference thd_pref;
142
value_out = (gchar*) thd_pref.get_preference_cstr();
143
strncpy(pref_str, value_out, MAX_DBUS_REPLY_STR_LEN);
144
thd_log_debug("thd_dbus_interface_get_current_preference out :%s\n",
146
*pref_out = pref_str;
151
gboolean thd_dbus_interface_calibrate(PrefObject *obj, GError **error) {
152
// thd_engine->thd_engine_calibrate();
156
gboolean thd_dbus_interface_terminate(PrefObject *obj, GError **error) {
157
thd_engine->thd_engine_terminate();
161
gboolean thd_dbus_interface_set_user_max_temperature(PrefObject *obj,
162
gchar *zone_name, gchar *temperature, GError **error) {
163
thd_log_debug("thd_dbus_interface_set_user_set_point %s:%s\n", zone_name,
165
g_assert(obj != NULL);
166
cthd_preference thd_pref;
167
if (thd_engine->thd_engine_set_user_max_temp(zone_name,
168
(char*) temperature) == THD_SUCCESS)
169
thd_engine->send_message(PREF_CHANGED, 0, NULL);
174
// g_log handler. All logs will be directed here
175
void thd_logger(const gchar *log_domain, GLogLevelFlags log_level,
176
const gchar *message, gpointer user_data) {
177
if (!(thd_log_level & log_level))
182
case G_LOG_LEVEL_ERROR:
183
syslog_priority = LOG_CRIT;
185
case G_LOG_LEVEL_CRITICAL:
186
syslog_priority = LOG_ERR;
188
case G_LOG_LEVEL_WARNING:
189
syslog_priority = LOG_WARNING;
191
case G_LOG_LEVEL_MESSAGE:
192
syslog_priority = LOG_NOTICE;
194
case G_LOG_LEVEL_DEBUG:
195
syslog_priority = LOG_DEBUG;
197
case G_LOG_LEVEL_INFO:
199
syslog_priority = LOG_INFO;
202
syslog(syslog_priority, "%s", message);
204
g_print("%s", message);
207
static GMainLoop *g_main_loop;
210
static int thd_dbus_server_proc(gboolean no_daemon) {
211
DBusGConnection *bus;
212
DBusGProxy *bus_proxy;
213
GMainLoop *main_loop;
214
GError *error = NULL;
216
PrefObject *value_obj;
219
// Initialize the GType/GObject system
222
// Create a main loop that will dispatch callbacks
223
g_main_loop = main_loop = g_main_loop_new(NULL, FALSE);
224
if (main_loop == NULL) {
225
thd_log_error("Couldn't create GMainLoop:\n");
226
return THD_FATAL_ERROR;
229
bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
231
thd_log_error("Couldn't connect to session bus: %s:\n",
233
return THD_FATAL_ERROR;
236
// Get a bus proxy instance
237
bus_proxy = dbus_g_proxy_new_for_name(bus, DBUS_SERVICE_DBUS,
238
DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
239
if (bus_proxy == NULL) {
240
thd_log_error("Failed to get a proxy for D-Bus:\n");
241
return THD_FATAL_ERROR;
244
thd_log_debug("Registering the well-known name (%s)\n",
246
// register the well-known name
247
if (!dbus_g_proxy_call(bus_proxy, "RequestName", &error, G_TYPE_STRING,
248
THD_SERVICE_NAME, G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_UINT,
249
&result, G_TYPE_INVALID)) {
250
thd_log_error("D-Bus.RequestName RPC failed: %s\n", error->message);
251
return THD_FATAL_ERROR;
253
thd_log_debug("RequestName returned %d.\n", result);
254
if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
255
thd_log_error("Failed to get the primary well-known name:\n");
256
return THD_FATAL_ERROR;
258
value_obj = (PrefObject*) g_object_new(PREF_TYPE_OBJECT, NULL);
259
if (value_obj == NULL) {
260
thd_log_error("Failed to create one Value instance:\n");
261
return THD_FATAL_ERROR;
264
thd_log_debug("Registering it on the D-Bus.\n");
265
dbus_g_connection_register_g_object(bus, THD_SERVICE_OBJECT_PATH,
266
G_OBJECT(value_obj));
269
printf("Ready to serve requests: Daemonizing.. %d\n", thd_daemonize);
271
"thermald ver %s: Ready to serve requests: Daemonizing..\n",
274
if (daemon(0, 0) != 0) {
275
thd_log_error("Failed to daemonize.\n");
276
return THD_FATAL_ERROR;
280
thd_engine = new cthd_engine_default();
281
if (exclusive_control)
282
thd_engine->set_control_mode(EXCLUSIVE);
284
// Initialize thermald objects
285
thd_engine->set_poll_interval(thd_poll_interval);
286
if (thd_engine->thd_engine_start(ignore_cpuid_check) != THD_SUCCESS) {
287
thd_log_error("THD engine start failed:\n");
292
// Start service requests on the D-Bus
293
thd_log_debug("Start main loop\n");
294
g_main_loop_run(main_loop);
295
thd_log_warn("Oops g main loop exit..\n");
299
void sig_int_handler(int signum) {
301
thd_engine->thd_engine_terminate();
305
g_main_loop_quit(g_main_loop);
309
int main(int argc, char *argv[]) {
310
gboolean show_version = FALSE;
311
gboolean log_info = FALSE;
312
gboolean log_debug = FALSE;
313
gboolean no_daemon = FALSE;
314
gboolean test_mode = FALSE;
315
gint poll_interval = -1;
317
GOptionContext *opt_ctx;
319
thd_daemonize = TRUE;
322
GOptionEntry options[] = { { "version", 0, 0, G_OPTION_ARG_NONE,
323
&show_version, N_("Print thermald version and exit"), NULL }, {
324
"no-daemon", 0, 0, G_OPTION_ARG_NONE, &no_daemon, N_(
325
"Don't become a daemon: Default is daemon mode"), NULL }, {
326
"loglevel=info", 0, 0, G_OPTION_ARG_NONE, &log_info, N_(
327
"log severity: info level and up"), NULL }, {
328
"loglevel=debug", 0, 0, G_OPTION_ARG_NONE, &log_debug, N_(
329
"log severity: debug level and up: Max logging"), NULL }, {
330
"test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode, N_(
331
"Test Mode only: Allow non root user"), NULL }, {
332
"poll-interval", 0, 0, G_OPTION_ARG_INT, &poll_interval,
333
N_("Poll interval in seconds: Poll for zone temperature changes. "
334
"If want to disable polling set to zero."), NULL }, {
335
"dbus-enable", 0, 0, G_OPTION_ARG_NONE, &dbus_enable, N_(
336
"Enable Dbus."), NULL }, { "exclusive-control", 0, 0,
337
G_OPTION_ARG_NONE, &exclusive_control, N_(
338
"Take over thermal control from kernel thermal driver."),
339
NULL }, { "ingore-cpuid-check", 0, 0, G_OPTION_ARG_NONE,
340
&ignore_cpuid_check, N_("Ignore CPU ID check."), NULL },
344
if (!g_module_supported()) {
345
fprintf(stderr, _("GModules are not supported on your platform!\n"));
349
/* Set locale to be able to use environment variables */
350
setlocale(LC_ALL, "");
352
bindtextdomain(GETTEXT_PACKAGE, TDLOCALEDIR);
353
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
354
textdomain(GETTEXT_PACKAGE);
356
opt_ctx = g_option_context_new(NULL);
357
g_option_context_set_translation_domain(opt_ctx, GETTEXT_PACKAGE);
358
g_option_context_set_ignore_unknown_options(opt_ctx, FALSE);
359
g_option_context_set_help_enabled(opt_ctx, TRUE);
360
g_option_context_add_main_entries(opt_ctx, options, NULL);
362
g_option_context_set_summary(opt_ctx,
364
"Thermal daemon monitors temperature sensors and decides the best action "
365
"based on the temperature readings and user preferences."));
367
success = g_option_context_parse(opt_ctx, &argc, &argv, NULL);
368
g_option_context_free(opt_ctx);
373
"Invalid option. Please use --help to see a list of valid options.\n"));
378
fprintf(stdout, TD_DIST_VERSION "\n");
382
if (getuid() != 0 && !test_mode) {
383
fprintf(stderr, _("You must be root to run thermald!\n"));
386
if (g_mkdir_with_parents(TDRUNDIR, 0755) != 0) {
387
fprintf(stderr, "Cannot create '%s': %s", TDRUNDIR, strerror(errno));
390
g_mkdir_with_parents(TDCONFDIR, 0755); // Don't care return value as directory
393
thd_log_level |= G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO;
396
thd_log_level |= G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO
399
if (poll_interval >= 0) {
400
fprintf(stdout, "Polling enabled: %d\n", poll_interval);
401
thd_poll_interval = poll_interval;
404
openlog("thermald", LOG_PID, LOG_USER | LOG_DAEMON | LOG_SYSLOG);
405
// Don't care return val
406
//setlogmask(LOG_CRIT | LOG_ERR | LOG_WARNING | LOG_NOTICE | LOG_DEBUG | LOG_INFO);
407
thd_daemonize = !no_daemon;
408
g_log_set_handler(NULL, G_LOG_LEVEL_MASK, thd_logger, NULL);
411
signal(SIGINT, sig_int_handler);
413
// dbus glib processing begin
414
thd_dbus_server_proc(no_daemon);
416
fprintf(stdout, "Exiting ..\n");