NoPaste

xfsm-shutdown-helper

von uname
SNIPPET_DESC:
xfsm-shutdown-helper
SNIPPET_CREATION_TIME:
04.02.2013 14:12:24
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. /* $Id$ */
  2. /*-
  3.  * Copyright (c) 2003-2004 Benedikt Meurer <benny@xfce.org>
  4.  * All rights reserved.
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  * MA 02110-1301 USA.
  20.  
  21.  *
  22.  * XXX - since this program is executed with root permissions, it may not
  23.  *       be a good idea to trust glib!!
  24.  */
  25.  
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29.  
  30. #ifdef HAVE_SYS_WAIT_H
  31. #include <sys/wait.h>
  32. #endif
  33.  
  34. #ifdef HAVE_MEMORY_H
  35. #include <memory.h>
  36. #endif
  37. #ifdef HAVE_SIGNAL_H
  38. #include <signal.h>
  39. #endif
  40. #include <stdio.h>
  41. #ifdef HAVE_STDLIB_H
  42. #include <stdlib.h>
  43. #endif
  44. #ifdef HAVE_STRING_H
  45. #include <string.h>
  46. #endif
  47. #ifdef HAVE_UNISTD_H
  48. #include <unistd.h>
  49. #endif
  50.  
  51. #include <glib.h>
  52.  
  53. /* XXX */
  54. #ifdef POWEROFF_CMD
  55. #undef POWEROFF_CMD
  56. #endif
  57. #ifdef REBOOT_CMD
  58. #undef REBOOT_CMD
  59. #endif
  60.  
  61. #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  62. #define POWEROFF_CMD  "/sbin/shutdown -p now"
  63. #define REBOOT_CMD    "/sbin/shutdown -r now"
  64. #elif defined(sun) || defined(__sun)
  65. #define POWEROFF_CMD  "/usr/sbin/shutdown -i 5 -g 0 -y"
  66. #define REBOOT_CMD    "/usr/sbin/shutdown -i 6 -g 0 -y"
  67. #else
  68. #define POWEROFF_CMD  "/sbin/shutdown -h now"
  69. #define REBOOT_CMD    "/sbin/shutdown -r now"
  70. #endif
  71.  
  72.  
  73. static gboolean
  74. run (const gchar *command)
  75. {
  76. #if defined(HAVE_SIGPROCMASK)
  77.   sigset_t sigset;
  78. #endif
  79.   gboolean result;
  80.   gchar **argv;
  81.   gchar **envp;
  82.   GError *err;
  83.   gint status;
  84.   gint argc;
  85.  
  86. #if defined(HAVE_SETSID)
  87.   setsid ();
  88. #endif
  89.  
  90. #if defined (HAVE_SIGPROCMASK)
  91.   sigemptyset (&sigset);
  92.   sigaddset (&sigset, SIGHUP);
  93.   sigaddset (&sigset, SIGINT);
  94.   sigprocmask (SIG_BLOCK, &sigset, NULL);
  95. #endif
  96.  
  97.   result = g_shell_parse_argv (command, &argc, &argv, &err);
  98.  
  99.   if (result)
  100.     {
  101.       envp = g_new0 (gchar *, 1);
  102.  
  103.       result = g_spawn_sync (NULL, argv, envp,
  104.                              G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL |
  105.                              G_SPAWN_STDERR_TO_DEV_NULL,
  106.                              NULL, NULL, NULL, NULL, &status, &err);
  107.  
  108.       g_strfreev (envp);
  109.       g_strfreev (argv);
  110.     }
  111.  
  112.   if (!result)
  113.     {
  114.       g_error_free (err);
  115.       return FALSE;
  116.     }
  117.  
  118.   return (WIFEXITED (status) && WEXITSTATUS (status) == 0);
  119. }
  120.  
  121.  
  122. int
  123. main (int argc, char **argv)
  124. {
  125.   gboolean succeed = FALSE;
  126.   char action[1024];
  127.  
  128.   /* display banner */
  129.   fprintf (stdout, "XFSM_SUDO_DONE ");
  130.   fflush (stdout);
  131.  
  132.   if (fgets (action, 1024, stdin) == NULL)
  133.     {
  134.       fprintf (stdout, "FAILED\n");
  135.       return EXIT_FAILURE;
  136.     }
  137.  
  138.   if (strncasecmp (action, "POWEROFF", 8) == 0)
  139.     {
  140.       succeed = run (POWEROFF_CMD);
  141.     }
  142.   else if (strncasecmp (action, "REBOOT", 6) == 0)
  143.     {
  144.       succeed = run (REBOOT_CMD);
  145.     }
  146.  
  147.   if (succeed)
  148.     {
  149.       fprintf (stdout, "SUCCEED\n");
  150.       return EXIT_SUCCESS;
  151.     }
  152.  
  153.   fprintf (stdout, "FAILED\n");
  154.   return EXIT_FAILURE;
  155. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN