[gnome-initial-setup/mcatanzaro/resurrect-software-page: 4/9] Add gis_pkexec() helper function




commit 6f0f2632a1640acd471e84a73ccfb8ea34252c0c
Author: Will Thompson <wjt endlessm com>
Date:   Thu Oct 13 16:58:49 2016 +0100

    Add gis_pkexec() helper function
    
    Several bits of Endless-specific code will need to run various scripts
    as various different users.
    
    Part-of: <https://gitlab.gnome.org/GNOME/gnome-initial-setup/-/merge_requests/121>

 gnome-initial-setup/gis-pkexec.c          | 65 +++++++++++++++++++++++++++++++
 gnome-initial-setup/gis-pkexec.h          | 41 +++++++++++++++++++
 gnome-initial-setup/gnome-initial-setup.h |  1 +
 gnome-initial-setup/meson.build           |  2 +
 4 files changed, 109 insertions(+)
---
diff --git a/gnome-initial-setup/gis-pkexec.c b/gnome-initial-setup/gis-pkexec.c
new file mode 100644
index 00000000..7b88f5c7
--- /dev/null
+++ b/gnome-initial-setup/gis-pkexec.c
@@ -0,0 +1,65 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2015-2016 Red Hat
+ * Copyright (C) 2015-2017 Endless Mobile, Inc.
+ *
+ * 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.
+ *
+ * Written by:
+ *     Dan Nicholson <nicholson endlessm com>
+ *     Will Thompson <wjt endlessm com>
+ */
+
+#include "gis-pkexec.h"
+
+gboolean
+gis_pkexec (const gchar  *command,
+            const gchar  *arg1,
+            const gchar  *user,
+            GError      **error)
+{
+  GSubprocessLauncher *launcher = NULL;
+  GSubprocess *process = NULL;
+  const gchar * const root_argv[] = { "pkexec", command, arg1, NULL };
+  const gchar * const user_argv[] = { "pkexec", "--user", user, command, arg1, NULL };
+  const gchar * const *argv = user == NULL ? root_argv : user_argv;
+  gboolean ret = FALSE;
+
+  launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
+
+  /* pkexec won't let us run the program if $SHELL isn't in /etc/shells,
+   * so remove it from the environment.
+   */
+  g_subprocess_launcher_unsetenv (launcher, "SHELL");
+  process = g_subprocess_launcher_spawnv (launcher, argv, error);
+
+  if (!process) {
+    g_prefix_error (error, "Failed to create %s process: ", command);
+    goto out;
+  }
+
+  if (!g_subprocess_wait_check (process, NULL, error)) {
+    g_prefix_error (error, "%s failed: ", command);
+    goto out;
+  }
+
+  ret = TRUE;
+
+out:
+  g_clear_object (&process);
+  g_clear_object (&launcher);
+  return ret;
+}
diff --git a/gnome-initial-setup/gis-pkexec.h b/gnome-initial-setup/gis-pkexec.h
new file mode 100644
index 00000000..0984f3e0
--- /dev/null
+++ b/gnome-initial-setup/gis-pkexec.h
@@ -0,0 +1,41 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2015-2016 Red Hat
+ * Copyright (C) 2015-2017 Endless Mobile, Inc.
+ *
+ * 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.
+ *
+ * Written by:
+ *     Dan Nicholson <nicholson endlessm com>
+ *     Will Thompson <wjt endlessm com>
+ */
+
+#ifndef __GIS_PKEXEC_H__
+#define __GIS_PKEXEC_H__
+
+#include "gnome-initial-setup.h"
+
+G_BEGIN_DECLS
+
+gboolean
+gis_pkexec (const gchar  *command,
+            const gchar  *arg1,
+            const gchar  *user,
+            GError      **error);
+
+G_END_DECLS
+
+#endif /* __GIS_PKEXEC_H__ */
diff --git a/gnome-initial-setup/gnome-initial-setup.h b/gnome-initial-setup/gnome-initial-setup.h
index 55fcc7d2..88806345 100644
--- a/gnome-initial-setup/gnome-initial-setup.h
+++ b/gnome-initial-setup/gnome-initial-setup.h
@@ -33,6 +33,7 @@ typedef struct _GisPage      GisPage;
 #include "gis-driver.h"
 #include "gis-assistant.h"
 #include "gis-page.h"
+#include "gis-pkexec.h"
 #include "gis-keyring.h"
 
 void gis_ensure_stamp_files (GisDriver *driver);
diff --git a/gnome-initial-setup/meson.build b/gnome-initial-setup/meson.build
index a23b160c..b08c3892 100644
--- a/gnome-initial-setup/meson.build
+++ b/gnome-initial-setup/meson.build
@@ -15,12 +15,14 @@ sources += [
     'gis-assistant.c',
     'gis-page.c',
     'gis-page-header.c',
+    'gis-pkexec.c',
     'gis-driver.c',
     'gis-keyring.c',
     'gnome-initial-setup.h',
     'gis-assistant.h',
     'gis-page.h',
     'gis-page-header.h',
+    'gis-pkexec.h',
     'gis-driver.h',
     'gis-keyring.h'
 ]


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