~ubuntu-branches/ubuntu/utopic/thermald/utopic

« back to all changes in this revision

Viewing changes to .pc/0028-get_preference_cstr-return-C-string-allocated-on-hea.patch/src/main.cpp

  • Committer: Package Import Robot
  • Author(s): Colin King
  • Date: 2014-05-19 12:41:22 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140519124122-zan11arvjxtbqcq5
Tags: 1.2-1
* Adjust for coretemp path change
* Deny non root users to send system bus dbus messages
* Remove compile warning
* Remove rpmlint warning for manual page
* Remove old patches that are now included into version 1.2
* Sync up with version 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * main.cpp: Thermal Daemon entry point
3
 
 *
4
 
 * Copyright (C) 2012 Intel Corporation. All rights reserved.
5
 
 *
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.
9
 
 *
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.
14
 
 *
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
18
 
 * 02110-1301, USA.
19
 
 *
20
 
 *
21
 
 * Author Name <Srinivas.Pandruvada@linux.intel.com>
22
 
 *
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
25
 
 * functions.
26
 
 */
27
 
 
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
35
 
 * sensors.
36
 
 * Dbus interface allows user to switch between active/passive thermal controls
37
 
 * if the thermal-conf.xml defines parameters.
38
 
 */
39
 
 
40
 
#include <syslog.h>
41
 
#include <signal.h>
42
 
#include "thermald.h"
43
 
#include "thd_preference.h"
44
 
#include "thd_engine.h"
45
 
#include "thd_engine_default.h"
46
 
#include "thd_parse.h"
47
 
#include <syslog.h>
48
 
 
49
 
#if !defined(TD_DIST_VERSION)
50
 
#define TD_DIST_VERSION PACKAGE_VERSION
51
 
#endif
52
 
 
53
 
typedef struct {
54
 
        GObject parent;
55
 
} PrefObject;
56
 
 
57
 
typedef struct {
58
 
        GObjectClass parent;
59
 
} PrefObjectClass;
60
 
 
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)
65
 
 
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,
69
 
                GError **error);
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"
76
 
 
77
 
// Default log level
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;
80
 
 
81
 
// Daemonize or not
82
 
static gboolean thd_daemonize;
83
 
 
84
 
// Disable dbus
85
 
static gboolean dbus_enable;
86
 
 
87
 
// poll mode
88
 
int thd_poll_interval = 4; //in seconds
89
 
 
90
 
// check cpuid
91
 
static gboolean ignore_cpuid_check = false;
92
 
 
93
 
// Thermal engine
94
 
cthd_engine *thd_engine;
95
 
 
96
 
gboolean exclusive_control = FALSE;
97
 
 
98
 
// DBUS Related functions
99
 
 
100
 
// Dbus object initialization
101
 
static void pref_object_init(PrefObject *obj) {
102
 
        g_assert(obj != NULL);
103
 
}
104
 
 
105
 
// Dbus object class initialization
106
 
static void pref_object_class_init(PrefObjectClass *_class) {
107
 
        g_assert(_class != NULL);
108
 
 
109
 
        dbus_g_object_type_install_info(PREF_TYPE_OBJECT,
110
 
                        &dbus_glib_thd_dbus_interface_object_info);
111
 
}
112
 
 
113
 
// Callback function called to inform a sent value via dbus
114
 
gboolean thd_dbus_interface_set_current_preference(PrefObject *obj, gchar *pref,
115
 
                GError **error) {
116
 
        int ret;
117
 
 
118
 
        thd_log_debug("thd_dbus_interface_set_current_preference %s\n",
119
 
                        (char*) pref);
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);
124
 
 
125
 
        return ret;
126
 
}
127
 
 
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);
133
 
        gchar *value_out;
134
 
        static char *pref_str;
135
 
 
136
 
        pref_str = g_new(char, MAX_DBUS_REPLY_STR_LEN);
137
 
 
138
 
        if (!pref_str)
139
 
                return FALSE;
140
 
 
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",
145
 
                        pref_str);
146
 
        *pref_out = pref_str;
147
 
 
148
 
        return TRUE;
149
 
}
150
 
 
151
 
gboolean thd_dbus_interface_calibrate(PrefObject *obj, GError **error) {
152
 
//      thd_engine->thd_engine_calibrate();
153
 
        return TRUE;
154
 
}
155
 
 
156
 
gboolean thd_dbus_interface_terminate(PrefObject *obj, GError **error) {
157
 
        thd_engine->thd_engine_terminate();
158
 
        return TRUE;
159
 
}
160
 
 
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,
164
 
                        temperature);
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);
170
 
 
171
 
        return TRUE;
172
 
}
173
 
 
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))
178
 
                return;
179
 
        if (thd_daemonize) {
180
 
                int syslog_priority;
181
 
                switch (log_level) {
182
 
                case G_LOG_LEVEL_ERROR:
183
 
                        syslog_priority = LOG_CRIT;
184
 
                        break;
185
 
                case G_LOG_LEVEL_CRITICAL:
186
 
                        syslog_priority = LOG_ERR;
187
 
                        break;
188
 
                case G_LOG_LEVEL_WARNING:
189
 
                        syslog_priority = LOG_WARNING;
190
 
                        break;
191
 
                case G_LOG_LEVEL_MESSAGE:
192
 
                        syslog_priority = LOG_NOTICE;
193
 
                        break;
194
 
                case G_LOG_LEVEL_DEBUG:
195
 
                        syslog_priority = LOG_DEBUG;
196
 
                        break;
197
 
                case G_LOG_LEVEL_INFO:
198
 
                default:
199
 
                        syslog_priority = LOG_INFO;
200
 
                        break;
201
 
                }
202
 
                syslog(syslog_priority, "%s", message);
203
 
        } else
204
 
                g_print("%s", message);
205
 
}
206
 
 
207
 
static GMainLoop *g_main_loop;
208
 
 
209
 
// Setup dbus server
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;
215
 
        guint result;
216
 
        PrefObject *value_obj;
217
 
 
218
 
        thd_engine = NULL;
219
 
        // Initialize the GType/GObject system
220
 
        g_type_init();
221
 
 
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;
227
 
        }
228
 
        if (dbus_enable) {
229
 
                bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
230
 
                if (error != NULL) {
231
 
                        thd_log_error("Couldn't connect to session bus: %s:\n",
232
 
                                        error->message);
233
 
                        return THD_FATAL_ERROR;
234
 
                }
235
 
 
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;
242
 
                }
243
 
 
244
 
                thd_log_debug("Registering the well-known name (%s)\n",
245
 
                                THD_SERVICE_NAME);
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;
252
 
                }
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;
257
 
                }
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;
262
 
                }
263
 
 
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));
267
 
        }
268
 
        if (!no_daemon) {
269
 
                printf("Ready to serve requests: Daemonizing.. %d\n", thd_daemonize);
270
 
                thd_log_info(
271
 
                                "thermald ver %s: Ready to serve requests: Daemonizing..\n",
272
 
                                TD_DIST_VERSION);
273
 
 
274
 
                if (daemon(0, 0) != 0) {
275
 
                        thd_log_error("Failed to daemonize.\n");
276
 
                        return THD_FATAL_ERROR;
277
 
                }
278
 
        }
279
 
 
280
 
        thd_engine = new cthd_engine_default();
281
 
        if (exclusive_control)
282
 
                thd_engine->set_control_mode(EXCLUSIVE);
283
 
 
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");
288
 
                closelog();
289
 
                exit(EXIT_FAILURE);
290
 
        }
291
 
 
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");
296
 
        return THD_SUCCESS;
297
 
}
298
 
 
299
 
void sig_int_handler(int signum) {
300
 
        // control+c handler
301
 
        thd_engine->thd_engine_terminate();
302
 
        sleep(1);
303
 
        delete thd_engine;
304
 
        if (g_main_loop)
305
 
                g_main_loop_quit(g_main_loop);
306
 
}
307
 
 
308
 
// main function
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;
316
 
        gboolean success;
317
 
        GOptionContext *opt_ctx;
318
 
 
319
 
        thd_daemonize = TRUE;
320
 
        dbus_enable = FALSE;
321
 
 
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 },
341
 
 
342
 
        { NULL } };
343
 
 
344
 
        if (!g_module_supported()) {
345
 
                fprintf(stderr, _("GModules are not supported on your platform!\n"));
346
 
                exit(EXIT_FAILURE);
347
 
        }
348
 
 
349
 
        /* Set locale to be able to use environment variables */
350
 
        setlocale(LC_ALL, "");
351
 
 
352
 
        bindtextdomain(GETTEXT_PACKAGE, TDLOCALEDIR);
353
 
        bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
354
 
        textdomain(GETTEXT_PACKAGE);
355
 
        /* Parse options */
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);
361
 
 
362
 
        g_option_context_set_summary(opt_ctx,
363
 
                        _(
364
 
                                        "Thermal daemon monitors temperature sensors and decides the best action "
365
 
                                                        "based on the temperature readings and user preferences."));
366
 
 
367
 
        success = g_option_context_parse(opt_ctx, &argc, &argv, NULL);
368
 
        g_option_context_free(opt_ctx);
369
 
 
370
 
        if (!success) {
371
 
                fprintf(stderr,
372
 
                                _(
373
 
                                                "Invalid option.  Please use --help to see a list of valid options.\n"));
374
 
                exit(EXIT_FAILURE);
375
 
        }
376
 
 
377
 
        if (show_version) {
378
 
                fprintf(stdout, TD_DIST_VERSION "\n");
379
 
                exit(EXIT_SUCCESS);
380
 
        }
381
 
 
382
 
        if (getuid() != 0 && !test_mode) {
383
 
                fprintf(stderr, _("You must be root to run thermald!\n"));
384
 
                exit(EXIT_FAILURE);
385
 
        }
386
 
        if (g_mkdir_with_parents(TDRUNDIR, 0755) != 0) {
387
 
                fprintf(stderr, "Cannot create '%s': %s", TDRUNDIR, strerror(errno));
388
 
                exit(EXIT_FAILURE);
389
 
        }
390
 
        g_mkdir_with_parents(TDCONFDIR, 0755); // Don't care return value as directory
391
 
        // may already exist
392
 
        if (log_info) {
393
 
                thd_log_level |= G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO;
394
 
        }
395
 
        if (log_debug) {
396
 
                thd_log_level |= G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO
397
 
                                | G_LOG_LEVEL_DEBUG;
398
 
        }
399
 
        if (poll_interval >= 0) {
400
 
                fprintf(stdout, "Polling enabled: %d\n", poll_interval);
401
 
                thd_poll_interval = poll_interval;
402
 
        }
403
 
 
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);
409
 
 
410
 
        if (no_daemon)
411
 
                signal(SIGINT, sig_int_handler);
412
 
 
413
 
        // dbus glib processing begin
414
 
        thd_dbus_server_proc(no_daemon);
415
 
 
416
 
        fprintf(stdout, "Exiting ..\n");
417
 
        closelog();
418
 
}