~ubuntu-branches/ubuntu/karmic/xfce4-session/karmic

« back to all changes in this revision

Viewing changes to xfsm-shutdown-helper/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Yves-Alexis Perez
  • Date: 2005-11-06 22:01:12 UTC
  • mto: (4.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20051106220112-5rusox237ymjghsp
Tags: upstream-4.2.3
ImportĀ upstreamĀ versionĀ 4.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: main.c 4565 2004-05-20 20:45:28Z benny $ */
 
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., 59 Temple Place - Suite 330, Boston, MA
 
19
 * 02111-1307, USA.
 
20
 *
 
21
 * XXX - since this program is executed with root permissions, it may not
 
22
 *       be a good idea to trust glib!!
 
23
 */
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#include <config.h>
 
27
#endif
 
28
 
 
29
#ifdef HAVE_SYS_WAIT_H
 
30
#include <sys/wait.h>
 
31
#endif
 
32
 
 
33
#ifdef HAVE_MEMORY_H
 
34
#include <memory.h>
 
35
#endif
 
36
#ifdef HAVE_SIGNAL_H
 
37
#include <signal.h>
 
38
#endif
 
39
#include <stdio.h>
 
40
#ifdef HAVE_STDLIB_H
 
41
#include <stdlib.h>
 
42
#endif
 
43
#ifdef HAVE_STRING_H
 
44
#include <string.h>
 
45
#endif
 
46
#ifdef HAVE_UNISTD_H
 
47
#include <unistd.h>
 
48
#endif
 
49
 
 
50
#include <glib.h>
 
51
 
 
52
/* XXX */
 
53
#ifdef POWEROFF_CMD
 
54
#undef POWEROFF_CMD
 
55
#endif
 
56
#ifdef REBOOT_CMD
 
57
#undef REBOOT_CMD
 
58
#endif
 
59
 
 
60
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
 
61
#define POWEROFF_CMD  "/sbin/shutdown -p now"
 
62
#define REBOOT_CMD    "/sbin/shutdown -r now"
 
63
#else
 
64
#define POWEROFF_CMD  "/sbin/shutdown -h now"
 
65
#define REBOOT_CMD    "/sbin/shutdown -r now"
 
66
#endif
 
67
 
 
68
 
 
69
static gboolean
 
70
run (const gchar *command)
 
71
{
 
72
#if defined(HAVE_SIGPROCMASK)
 
73
  sigset_t sigset;
 
74
#endif
 
75
  gboolean result;
 
76
  gchar **argv;
 
77
  gchar **envp;
 
78
  GError *err;
 
79
  gint status;
 
80
  gint argc;
 
81
 
 
82
#if defined(HAVE_SETSID)
 
83
  setsid ();
 
84
#endif
 
85
  
 
86
#if defined (HAVE_SIGPROCMASK)
 
87
  sigemptyset (&sigset);
 
88
  sigaddset (&sigset, SIGHUP);
 
89
  sigaddset (&sigset, SIGINT);
 
90
  sigprocmask (SIG_BLOCK, &sigset, NULL);
 
91
#endif
 
92
 
 
93
  result = g_shell_parse_argv (command, &argc, &argv, &err);
 
94
 
 
95
  if (result)
 
96
    {
 
97
      envp = g_new0 (gchar *, 1);
 
98
 
 
99
      result = g_spawn_sync (NULL, argv, envp,
 
100
                             G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL |
 
101
                             G_SPAWN_STDERR_TO_DEV_NULL,
 
102
                             NULL, NULL, NULL, NULL, &status, &err);
 
103
 
 
104
      g_strfreev (envp);
 
105
      g_strfreev (argv);
 
106
    }
 
107
 
 
108
  if (!result)
 
109
    {
 
110
      g_error_free (err);
 
111
      return FALSE;
 
112
    }
 
113
 
 
114
  return (WIFEXITED (status) && WEXITSTATUS (status) == 0);
 
115
}
 
116
 
 
117
 
 
118
int
 
119
main (int argc, char **argv)
 
120
{
 
121
  gboolean succeed = FALSE;
 
122
  char action[1024];
 
123
 
 
124
  /* display banner */
 
125
  fprintf (stdout, "XFSM_SUDO_DONE ");
 
126
  fflush (stdout);
 
127
 
 
128
  if (fgets (action, 1024, stdin) == NULL)
 
129
    {
 
130
      fprintf (stdout, "FAILED\n");
 
131
      return EXIT_FAILURE;
 
132
    }
 
133
 
 
134
  if (strncasecmp (action, "POWEROFF", 8) == 0)
 
135
    {
 
136
      succeed = run (POWEROFF_CMD);
 
137
    }
 
138
  else if (strncasecmp (action, "REBOOT", 6) == 0)
 
139
    {
 
140
      succeed = run (REBOOT_CMD);
 
141
    }
 
142
 
 
143
  if (succeed)
 
144
    {
 
145
      fprintf (stdout, "SUCCEED\n");
 
146
      return EXIT_SUCCESS;
 
147
    }
 
148
 
 
149
  fprintf (stdout, "FAILED\n");
 
150
  return EXIT_FAILURE;
 
151
}
 
152
 
 
153