[gnome-settings-daemon/wip/benzea/backlight: 2/7] power: Simplify backlight helper to only support writing



commit 0330777890958b627f15398216a030502e730574
Author: Benjamin Berg <bberg redhat com>
Date:   Wed Jan 10 20:51:13 2018 +0100

    power: Simplify backlight helper to only support writing
    
    There is no need for the backlight helper to be capable of reading the
    current brightness value or returning the maximum brightness. These are
    now read directly by gsd-power and these features can be removed.
    
    The only thing left for the helper to do is to ensure the user is not
    trying to set a non-backlight brightness sysfs attribute.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=758413

 plugins/power/gsd-backlight-helper.c | 313 +++++++++++------------------------
 plugins/power/gsd-backlight-linux.c  | 123 --------------
 plugins/power/gsd-backlight-linux.h  |  28 ----
 plugins/power/gsd-backlight.c        |   2 +-
 plugins/power/meson.build            |   3 -
 5 files changed, 94 insertions(+), 375 deletions(-)
---
diff --git a/plugins/power/gsd-backlight-helper.c b/plugins/power/gsd-backlight-helper.c
index 00bc263b..852446ca 100644
--- a/plugins/power/gsd-backlight-helper.c
+++ b/plugins/power/gsd-backlight-helper.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2010-2011 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2018 Benjamin Berg <bberg redhat com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -22,254 +22,127 @@
 #include "config.h"
 
 #include <stdlib.h>
-#include <unistd.h>
-#include <glib-object.h>
-#include <locale.h>
+#include <dirent.h>
+#include <errno.h>
+#include <string.h>
+#include <libgen.h>
+#include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <string.h>
-#include <gudev/gudev.h>
-
-#include "gsd-backlight-linux.h"
+#include <unistd.h>
 
 #define GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS                 0
 #define GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED                  1
 #define GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID       3
 #define GSD_BACKLIGHT_HELPER_EXIT_CODE_INVALID_USER            4
-#define GSD_BACKLIGHT_HELPER_EXIT_CODE_NO_DEVICES              5
-
-static gboolean
-gsd_backlight_helper_write (const gchar *filename, gint value, GError **error)
-{
-       gchar *filename_path = NULL;
-       gchar *text = NULL;
-       gint retval;
-       gint length;
-       gint fd = -1;
-       gboolean ret = TRUE;
-
-       filename_path = g_build_filename (filename, "brightness", NULL);
-
-       fd = open (filename_path, O_WRONLY);
-       if (fd < 0) {
-               ret = FALSE;
-               g_set_error (error, 1, 0, "failed to open filename: %s", filename);
-               goto out;
-       }
-
-       /* convert to text */
-       text = g_strdup_printf ("%i", value);
-       length = strlen (text);
-
-       /* write to device file */
-       retval = write (fd, text, length);
-       if (retval != length) {
-               ret = FALSE;
-               g_set_error (error, 1, 0, "writing '%s' to %s failed", text, filename);
-               goto out;
-       }
-out:
-       if (fd >= 0)
-               close (fd);
-       g_free (text);
-       g_free (filename_path);
-       return ret;
-}
-
-static gint
-gsd_backlight_helper_read_value (const gchar *filename, GError **error)
-{
-       gchar *contents = NULL;
-       gint value;
-
-       if (g_file_get_contents (filename, &contents, NULL, error))
-               value = atoi (contents);
-       else
-               value = -1;
-       g_free (contents);
-
-       if (value < 0 && *error == NULL)
-               g_set_error (error, 1, 0, "got invalid backlight value from %s", filename);
-
-       return value;
-}
-
-static gint
-gsd_backlight_helper_get (const gchar *filename, GError **error)
-{
-       gchar *filename_path = NULL;
-       gint value;
-
-       filename_path = g_build_filename (filename, "brightness", NULL);
-       value = gsd_backlight_helper_read_value (filename_path, error);
-       g_free (filename_path);
-       return value;
-}
-
-static gint
-gsd_backlight_helper_get_max (const gchar *filename, GError **error)
-{
-       gchar *filename_path = NULL;
-       gint value;
 
-       filename_path = g_build_filename (filename, "max_brightness", NULL);
-       value = gsd_backlight_helper_read_value (filename_path, error);
-       g_free (filename_path);
-       return value;
-}
+#ifndef __linux__
+#error "gsd-backlight-helper does not work on non-Linux"
+#endif
 
-static gint
-clamp_minimum (gint max, gint value)
+static void
+usage(int argc, char *argv[])
 {
-       gint minimum;
-       /* If the interface has less than 100 possible values, it's
-        * likely that 0 doesn't turn the backlight off so we let 0 be
-        * set in that case. */
-       if (max > 99)
-               minimum = 1;
-       else
-               minimum = 0;
-
-       return MAX (value, minimum);
+       fprintf (stderr, "Usage: %s device brightness\n", argv[0]);
+       fprintf (stderr, "  device:      The backlight directory starting with \"/sys/class/backlight/\"\n");
+       fprintf (stderr, "  brightness:  The new brightness to write\n");
 }
 
 int
 main (int argc, char *argv[])
 {
-       GOptionContext *context;
-       gint uid;
-       gint euid;
-       guint retval = 0;
-       GError *error = NULL;
-       gint set_brightness = -1;
-       gboolean get_brightness = FALSE;
-       gboolean get_max_brightness = FALSE;
-       gchar *filename = NULL;
-       GsdBacklightType type;
+       char tmp[512];
+       char *device = NULL;
+       int fd, len, res;
+       int uid, euid;
+       int brightness;
+       int result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
+       DIR *dp = NULL;
+       struct dirent *ep;
 
-       const GOptionEntry options[] = {
-               { "set-brightness", '\0', 0, G_OPTION_ARG_INT, &set_brightness,
-                  /* command line argument */
-                 "Set the current brightness", NULL },
-               { "get-brightness", '\0', 0, G_OPTION_ARG_NONE, &get_brightness,
-                  /* command line argument */
-                 "Get the current brightness", NULL },
-               { "get-max-brightness", '\0', 0, G_OPTION_ARG_NONE, &get_max_brightness,
-                  /* command line argument */
-                 "Get the number of brightness levels supported", NULL },
-               { NULL}
-       };
-
-       context = g_option_context_new (NULL);
-       g_option_context_set_summary (context, "GNOME Settings Daemon Backlight Helper");
-       g_option_context_add_main_entries (context, options, NULL);
-       g_option_context_parse (context, &argc, &argv, NULL);
-       g_option_context_free (context);
-
-#ifndef __linux__
-       /* the g-s-d plugin should only call this helper on linux */
-       g_critical ("Attempting to call gsb-backlight-helper on non-Linux");
-       g_assert_not_reached ();
-#endif
-
-       /* no input */
-       if (set_brightness == -1 && !get_brightness && !get_max_brightness) {
-               g_print ("%s\n", "No valid option was specified");
-               retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
-               goto out;
+       /* check calling UID */
+       uid = getuid ();
+       euid = geteuid ();
+       if (uid != 0 || euid != 0) {
+               fprintf (stderr, "This program can only be used by the root user\n");
+               result = GSD_BACKLIGHT_HELPER_EXIT_CODE_INVALID_USER;
+               goto done;
        }
 
-       /* find device */
-       filename = gsd_backlight_helper_get_best_backlight (&type);
-       if (filename == NULL) {
-               retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_NO_DEVICES;
-               g_print ("%s: %s\n",
-                        "Could not get or set the value of the backlight",
-                        "No backlight devices present");
-               goto out;
+       if (argc != 3) {
+               fprintf (stderr, "Error: Need to be called with exactly two arguments\n");
+               usage (argc, argv);
+               result = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
+               goto done;
        }
 
-       /* GetBrightness */
-       if (get_brightness) {
-               gint value;
-               value = gsd_backlight_helper_get (filename, &error);
-               if (value < 0) {
-                       g_print ("%s: %s\n",
-                                "Could not get the value of the backlight",
-                                error->message);
-                       g_error_free (error);
-                       retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
-                       goto out;
-               }
-
-               /* just print the contents to stdout */
-               g_print ("%d", value);
-               retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
-               goto out;
+       errno = 0;
+       brightness = strtol (argv[2], NULL, 0);
+       if (errno) {
+               fprintf (stderr, "Error: Invalid brightness argument (%d: %s)\n", errno, strerror (errno));
+               usage (argc, argv);
+               goto done;
        }
 
-       /* GetSteps */
-       if (get_max_brightness) {
-               gint value;
-               value = gsd_backlight_helper_get_max (filename, &error);
-               if (value < 0) {
-                       g_print ("%s: %s\n",
-                                "Could not get the maximum value of the backlight",
-                                error->message);
-                       g_error_free (error);
-                       retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
-                       goto out;
-               }
+       device = realpath (argv[1], NULL);
 
-               /* just print the contents to stdout */
-               g_print ("%d", value);
-               retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
-               goto out;
+       dp = opendir ("/sys/class/backlight");
+       if (dp == NULL) {
+               fprintf (stderr, "Error: Could not open /sys/class/backlight (%d: %s)\n", errno, strerror 
(errno));
+               result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
+               goto done;
        }
 
-       /* check calling UID */
-       uid = getuid ();
-       euid = geteuid ();
-       if (uid != 0 || euid != 0) {
-               g_print ("%s\n",
-                        "This program can only be used by the root user");
-               retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
-               goto out;
-       }
-
-       /* SetBrightness */
-       if (set_brightness != -1) {
-               gboolean ret = FALSE;
-               gint max = gsd_backlight_helper_get_max (filename, &error);
-
-               if (max < 0) {
-                       g_print ("%s: %s\n",
-                                "Could not get the maximum value of the backlight",
-                                error->message);
-                       g_error_free (error);
-                       retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
-                       goto out;
+       while ((ep = readdir (dp))) {
+               char *path;
+
+               if (ep->d_name[0] == '.')
+                       continue;
+
+               /* Leave room for "/brightness" */
+               snprintf (tmp, sizeof(tmp) - 11, "/sys/class/backlight/%s", ep->d_name);
+               path = realpath (tmp, NULL);
+               if (strcmp (path, device) == 0) {
+                       free (path);
+                       strcat (tmp, "/brightness");
+
+                       fd = open (tmp, O_WRONLY);
+                       if (fd < 0) {
+                               fprintf (stderr, "Error: Could not open brightness sysfs file (%d: %s)\n", 
errno, strerror(errno));
+                               result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
+                               goto done;
+                       }
+
+                       len = snprintf (tmp, sizeof(tmp), "%d", brightness);
+                       if ((res = write (fd, tmp, len)) != len) {
+                               if (res == -1)
+                                       fprintf (stderr, "Error: Writing to file (%d: %s)\n", errno, 
strerror(errno));
+                               else
+                                       fprintf (stderr, "Error: Wrote the wrong length (%d of %d bytes)!\n", 
res, len);
+
+                               close (fd);
+                               result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
+                               goto done;
+                       }
+                       close (fd);
+
+                       result = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
+                       goto done;
+               } else {
+                       free (path);
                }
+       }
 
-               if (type == GSD_BACKLIGHT_TYPE_RAW)
-                       set_brightness = clamp_minimum (max, set_brightness);
+       result = GSD_BACKLIGHT_HELPER_EXIT_CODE_FAILED;
+       fprintf (stderr, "Error: Could not find the specified backlight \"%s\"\n", argv[1]);
 
-               ret = gsd_backlight_helper_write (filename, set_brightness, &error);
-               if (!ret) {
-                       g_print ("%s: %s\n",
-                                "Could not set the value of the backlight",
-                                error->message);
-                       g_error_free (error);
-                       retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_ARGUMENTS_INVALID;
-                       goto out;
-               }
-       }
+done:
+       if (device)
+               free (device);
+       if (dp)
+               closedir (dp);
 
-       /* success */
-       retval = GSD_BACKLIGHT_HELPER_EXIT_CODE_SUCCESS;
-out:
-       g_free (filename);
-       return retval;
+       return result;
 }
 
diff --git a/plugins/power/gsd-backlight.c b/plugins/power/gsd-backlight.c
index 4851a054..39693a75 100644
--- a/plugins/power/gsd-backlight.c
+++ b/plugins/power/gsd-backlight.c
@@ -353,7 +353,7 @@ gsd_backlight_run_set_helper (GsdBacklight *backlight, GTask *task)
                                  &error,
                                  "pkexec", 
                                  LIBEXECDIR "/gsd-backlight-helper",
-                                 "--set-brightness",
+                                 g_udev_device_get_sysfs_path (backlight->udev_device),
                                  data->value_str, NULL);
 
         if (proc == NULL) {
diff --git a/plugins/power/meson.build b/plugins/power/meson.build
index 94fcda08..caeb8e47 100644
--- a/plugins/power/meson.build
+++ b/plugins/power/meson.build
@@ -79,12 +79,9 @@ if enable_gudev
 
   sources = files(
     'gsd-backlight-helper.c',
-    'gsd-backlight-linux.c'
   )
 
   deps = [
-    gudev_dep,
-    m_dep
   ]
 
   executable(


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]