| File: | cpufreq/src/cpufreq-utils.c |
| Warning: | line 73, column 19 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * CAFE CPUFreq Applet |
| 3 | * Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library 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 GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public |
| 16 | * License along with this library; if not, write to the Free |
| 17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | * Authors : Carlos García Campos <carlosgc@gnome.org> |
| 20 | */ |
| 21 | |
| 22 | #include <config.h> |
| 23 | |
| 24 | #include <glib.h> |
| 25 | #include <ctk/ctk.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <unistd.h> |
| 30 | #include <string.h> |
| 31 | #include <errno(*__errno_location ()).h> |
| 32 | |
| 33 | #include "cpufreq-utils.h" |
| 34 | |
| 35 | #ifdef HAVE_POLKIT1 |
| 36 | #include <dbus/dbus-glib.h> |
| 37 | #endif /* HAVE_POLKIT */ |
| 38 | |
| 39 | guint |
| 40 | cpufreq_utils_get_n_cpus (void) |
| 41 | { |
| 42 | gint mcpu = -1; |
| 43 | gchar *file = NULL((void*)0); |
| 44 | static guint n_cpus = 0; |
| 45 | |
| 46 | if (n_cpus > 0) |
| 47 | return n_cpus; |
| 48 | |
| 49 | do { |
| 50 | if (file) |
| 51 | g_free (file); |
| 52 | mcpu ++; |
| 53 | file = g_strdup_printf ("/sys/devices/system/cpu/cpu%d", mcpu); |
| 54 | } while (g_file_test (file, G_FILE_TEST_EXISTS)); |
| 55 | g_free (file); |
| 56 | |
| 57 | if (mcpu >= 0) { |
| 58 | n_cpus = (guint)mcpu; |
| 59 | return mcpu; |
| 60 | } |
| 61 | |
| 62 | mcpu = -1; |
| 63 | file = NULL((void*)0); |
| 64 | do { |
| 65 | if (file) |
| 66 | g_free (file); |
| 67 | mcpu ++; |
| 68 | file = g_strdup_printf ("/proc/sys/cpu/%d", mcpu); |
| 69 | } while (g_file_test (file, G_FILE_TEST_EXISTS)); |
| 70 | g_free (file); |
| 71 | |
| 72 | if (mcpu >= 0) { |
| 73 | n_cpus = (guint)mcpu; |
This statement is never executed | |
| 74 | return mcpu; |
| 75 | } |
| 76 | |
| 77 | n_cpus = 1; |
| 78 | |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | cpufreq_utils_display_error (const gchar *message, |
| 84 | const gchar *secondary) |
| 85 | { |
| 86 | CtkWidget *dialog; |
| 87 | |
| 88 | g_return_if_fail (message != NULL)do { if ((message != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "message != NULL" ); return; } } while (0); |
| 89 | |
| 90 | dialog = ctk_message_dialog_new (NULL((void*)0), |
| 91 | CTK_DIALOG_DESTROY_WITH_PARENT, |
| 92 | CTK_MESSAGE_ERROR, |
| 93 | CTK_BUTTONS_OK, |
| 94 | "%s", message); |
| 95 | if (secondary) { |
| 96 | ctk_message_dialog_format_secondary_text (CTK_MESSAGE_DIALOG (dialog)((((CtkMessageDialog*) (void *) g_type_check_instance_cast (( GTypeInstance*) ((dialog)), ((ctk_message_dialog_get_type ()) ))))), |
| 97 | "%s", secondary); |
| 98 | } |
| 99 | |
| 100 | ctk_window_set_title (CTK_WINDOW (dialog)((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((dialog)), ((ctk_window_get_type ())))))), ""); /* as per HIG */ |
| 101 | ctk_window_set_skip_taskbar_hint (CTK_WINDOW (dialog)((((CtkWindow*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((dialog)), ((ctk_window_get_type ())))))), TRUE(!(0))); |
| 102 | g_signal_connect (G_OBJECT (dialog),g_signal_connect_data ((((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance*) ((dialog)), (((GType) ((20) << (2))) )))))), ("response"), (((GCallback) (ctk_widget_destroy))), ( ((void*)0)), ((void*)0), (GConnectFlags) 0) |
| 103 | "response",g_signal_connect_data ((((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance*) ((dialog)), (((GType) ((20) << (2))) )))))), ("response"), (((GCallback) (ctk_widget_destroy))), ( ((void*)0)), ((void*)0), (GConnectFlags) 0) |
| 104 | G_CALLBACK (ctk_widget_destroy), NULL)g_signal_connect_data ((((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance*) ((dialog)), (((GType) ((20) << (2))) )))))), ("response"), (((GCallback) (ctk_widget_destroy))), ( ((void*)0)), ((void*)0), (GConnectFlags) 0); |
| 105 | ctk_widget_show (dialog); |
| 106 | } |
| 107 | |
| 108 | #ifdef HAVE_POLKIT1 |
| 109 | #define CACHE_VALIDITY_SEC2 2 |
| 110 | |
| 111 | static gboolean |
| 112 | selector_is_available (void) |
| 113 | { |
| 114 | DBusGProxy *proxy; |
| 115 | static DBusGConnection *system_bus = NULL((void*)0); |
| 116 | GError *error = NULL((void*)0); |
| 117 | gboolean result; |
| 118 | |
| 119 | if (!system_bus) { |
| 120 | system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); |
| 121 | if (!system_bus) { |
| 122 | g_warning ("%s", error->message); |
| 123 | g_error_free (error); |
| 124 | |
| 125 | return FALSE(0); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | proxy = dbus_g_proxy_new_for_name (system_bus, |
| 130 | "org.cafe.CPUFreqSelector", |
| 131 | "/org/cafe/cpufreq_selector/selector", |
| 132 | "org.cafe.CPUFreqSelector"); |
| 133 | |
| 134 | if (!dbus_g_proxy_call (proxy, "CanSet", &error, |
| 135 | G_TYPE_INVALID((GType) ((0) << (2))), |
| 136 | G_TYPE_BOOLEAN((GType) ((5) << (2))), &result, |
| 137 | G_TYPE_INVALID((GType) ((0) << (2))))) { |
| 138 | g_warning ("Error calling org.cafe.CPUFreqSelector.CanSet: %s", error->message); |
| 139 | g_error_free (error); |
| 140 | result = FALSE(0); |
| 141 | } |
| 142 | |
| 143 | g_object_unref (proxy); |
| 144 | |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | gboolean |
| 149 | cpufreq_utils_selector_is_available (void) |
| 150 | { |
| 151 | static gboolean cache = FALSE(0); |
| 152 | static time_t last_refreshed = 0; |
| 153 | time_t now; |
| 154 | |
| 155 | time (&now); |
| 156 | if (ABS (now - last_refreshed)(((now - last_refreshed) < 0) ? -(now - last_refreshed) : ( now - last_refreshed)) > CACHE_VALIDITY_SEC2) { |
| 157 | cache = selector_is_available (); |
| 158 | last_refreshed = now; |
| 159 | } |
| 160 | |
| 161 | return cache; |
| 162 | } |
| 163 | #else /* !HAVE_POLKIT */ |
| 164 | gboolean |
| 165 | cpufreq_utils_selector_is_available (void) |
| 166 | { |
| 167 | struct stat *info; |
| 168 | gchar *path = NULL((void*)0); |
| 169 | |
| 170 | path = g_find_program_in_path ("cpufreq-selector"); |
| 171 | if (!path) |
| 172 | return FALSE(0); |
| 173 | |
| 174 | if (geteuid () == 0) { |
| 175 | g_free (path); |
| 176 | return TRUE(!(0)); |
| 177 | } |
| 178 | |
| 179 | info = (struct stat *) g_malloc (sizeof (struct stat)); |
| 180 | |
| 181 | if ((lstat (path, info)) != -1) { |
| 182 | if ((info->st_mode & S_ISUID04000) && (info->st_uid == 0)) { |
| 183 | g_free (info); |
| 184 | g_free (path); |
| 185 | |
| 186 | return TRUE(!(0)); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | g_free (info); |
| 191 | g_free (path); |
| 192 | |
| 193 | return FALSE(0); |
| 194 | } |
| 195 | #endif /* HAVE_POLKIT_CAFE */ |
| 196 | |
| 197 | gchar * |
| 198 | cpufreq_utils_get_frequency_label (guint freq) |
| 199 | { |
| 200 | gint divisor; |
| 201 | |
| 202 | if (freq > 999999) /* freq (kHz) */ |
| 203 | divisor = (1000 * 1000); |
| 204 | else |
| 205 | divisor = 1000; |
| 206 | |
| 207 | if (((freq % divisor) == 0) || divisor == 1000) /* integer */ |
| 208 | return g_strdup_printf ("%d", freq / divisor); |
| 209 | else /* float */ |
| 210 | return g_strdup_printf ("%3.2f", ((gfloat)freq / divisor)); |
| 211 | } |
| 212 | |
| 213 | gchar * |
| 214 | cpufreq_utils_get_frequency_unit (guint freq) |
| 215 | { |
| 216 | if (freq > 999999) /* freq (kHz) */ |
| 217 | return g_strdup ("GHz")g_strdup_inline ("GHz"); |
| 218 | else |
| 219 | return g_strdup ("MHz")g_strdup_inline ("MHz"); |
| 220 | } |
| 221 | |
| 222 | gboolean |
| 223 | cpufreq_utils_governor_is_automatic (const gchar *governor) |
| 224 | { |
| 225 | g_return_val_if_fail (governor != NULL, FALSE)do { if ((governor != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "governor != NULL" ); return ((0)); } } while (0); |
| 226 | |
| 227 | if (g_ascii_strcasecmp (governor, "userspace") == 0) |
| 228 | return FALSE(0); |
| 229 | |
| 230 | return TRUE(!(0)); |
| 231 | } |
| 232 | |
| 233 | gboolean |
| 234 | cpufreq_file_get_contents (const gchar *filename, |
| 235 | gchar **contents, |
| 236 | gsize *length, |
| 237 | GError **error) |
| 238 | { |
| 239 | gint fd; |
| 240 | GString *buffer = NULL((void*)0); |
| 241 | gchar *display_filename; |
| 242 | |
| 243 | g_return_val_if_fail (filename != NULL, FALSE)do { if ((filename != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "filename != NULL" ); return ((0)); } } while (0); |
| 244 | g_return_val_if_fail (contents != NULL, FALSE)do { if ((contents != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "contents != NULL" ); return ((0)); } } while (0); |
| 245 | |
| 246 | display_filename = g_filename_display_name (filename); |
| 247 | |
| 248 | *contents = NULL((void*)0); |
| 249 | if (length) |
| 250 | *length = 0; |
| 251 | |
| 252 | fd = open (filename, O_RDONLY00); |
| 253 | if (fd < 0) { |
| 254 | gint save_errno = errno(*__errno_location ()); |
| 255 | |
| 256 | g_set_error (error, |
| 257 | G_FILE_ERRORg_file_error_quark (), |
| 258 | g_file_error_from_errno (save_errno), |
| 259 | "Failed to open file '%s': %s", |
| 260 | display_filename, |
| 261 | g_strerror (save_errno)); |
| 262 | g_free (display_filename); |
| 263 | |
| 264 | return FALSE(0); |
| 265 | } |
| 266 | |
| 267 | while (TRUE(!(0))) { |
| 268 | ssize_t bytes_read; |
| 269 | gchar buf[1024]; |
| 270 | |
| 271 | bytes_read = read (fd, buf, sizeof (buf)); |
| 272 | if (bytes_read < 0) { /* Error */ |
| 273 | if (errno(*__errno_location ()) != EINTR4) { |
| 274 | gint save_errno = errno(*__errno_location ()); |
| 275 | |
| 276 | g_set_error (error, |
| 277 | G_FILE_ERRORg_file_error_quark (), |
| 278 | g_file_error_from_errno (save_errno), |
| 279 | "Failed to read from file '%s': %s", |
| 280 | display_filename, |
| 281 | g_strerror (save_errno)); |
| 282 | |
| 283 | if (buffer) |
| 284 | g_string_free (buffer, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) ( (buffer), ((!(0)))) : g_string_free_and_steal (buffer)) : (g_string_free ) ((buffer), ((!(0))))); |
| 285 | |
| 286 | g_free (display_filename); |
| 287 | close (fd); |
| 288 | |
| 289 | return FALSE(0); |
| 290 | } |
| 291 | } else if (bytes_read == 0) { /* EOF */ |
| 292 | break; |
| 293 | } else { |
| 294 | if (!buffer) |
| 295 | buffer = g_string_sized_new (bytes_read); |
| 296 | buffer = g_string_append_len (buffer, buf, bytes_read)g_string_append_len_inline (buffer, buf, bytes_read); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | g_free (display_filename); |
| 301 | |
| 302 | if (buffer) { |
| 303 | *contents = g_string_free (buffer, FALSE)(__builtin_constant_p ((0)) ? (((0)) ? (g_string_free) ((buffer ), ((0))) : g_string_free_and_steal (buffer)) : (g_string_free ) ((buffer), ((0)))); |
| 304 | |
| 305 | if (length) |
| 306 | *length = strlen (*contents); |
| 307 | } |
| 308 | |
| 309 | close (fd); |
| 310 | |
| 311 | return TRUE(!(0)); |
| 312 | } |