~ubuntu-branches/ubuntu/quantal/consolekit/quantal

« back to all changes in this revision

Viewing changes to .pc/01-retry-console-open-on-EIO.patch/src/ck-sysdeps-unix.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2010-11-20 18:17:28 UTC
  • mfrom: (0.1.16 sid)
  • Revision ID: james.westby@ubuntu.com-20101120181728-8e5bwe4ttgmk4j41
Tags: 0.4.3-2
Add 01-retry-console-open-on-EIO.patch: As reported in LP: #544139,
ConsoleKit sometimes fails to track the active VT. This particular case
was tracked down to a race condition that happens if you try to open
/dev/console while the current TTY is currently being closed. This yields
an -EIO error, in which case CK should just try again. Thanks Colin Watson
for the patch!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2006 William Jon McCann <mccann@jhu.edu>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program 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
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#include <fcntl.h>
 
26
#include <unistd.h>
 
27
#include <string.h>
 
28
#include <errno.h>
 
29
#include <sys/types.h>
 
30
#include <sys/stat.h>
 
31
#include <sys/socket.h>
 
32
#include <sys/ioctl.h>
 
33
 
 
34
#ifdef __linux__
 
35
#include <linux/kd.h>
 
36
#endif
 
37
 
 
38
#ifdef HAVE_SYS_VT_H
 
39
#include <sys/vt.h>
 
40
#endif
 
41
 
 
42
#if HAVE_SYS_CONSIO_H
 
43
#include <sys/consio.h>
 
44
#endif
 
45
 
 
46
#ifdef HAVE_GETPEERUCRED
 
47
#include <ucred.h>
 
48
#endif
 
49
 
 
50
#include "ck-sysdeps.h"
 
51
 
 
52
#ifndef ERROR
 
53
#define ERROR -1
 
54
#endif
 
55
 
 
56
/* Adapted from dbus-sysdeps-unix.c:_dbus_read_credentials_socket() */
 
57
gboolean
 
58
ck_get_socket_peer_credentials   (int      socket_fd,
 
59
                                  pid_t   *pid,
 
60
                                  uid_t   *uid,
 
61
                                  GError **error)
 
62
{
 
63
        gboolean ret;
 
64
        uid_t    uid_read;
 
65
        pid_t    pid_read;
 
66
 
 
67
        pid_read = -1;
 
68
        uid_read = -1;
 
69
        ret = FALSE;
 
70
 
 
71
#ifdef SO_PEERCRED
 
72
        struct ucred cr;
 
73
        socklen_t    cr_len;
 
74
 
 
75
        cr_len = sizeof (cr);
 
76
 
 
77
        if (getsockopt (socket_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 && cr_len == sizeof (cr)) {
 
78
                pid_read = cr.pid;
 
79
                uid_read = cr.uid;
 
80
                ret = TRUE;
 
81
        } else {
 
82
                g_warning ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
 
83
                           cr_len,
 
84
                           (int) sizeof (cr),
 
85
                           g_strerror (errno));
 
86
        }
 
87
#elif defined(HAVE_GETPEERUCRED)
 
88
        ucred_t *ucred;
 
89
 
 
90
        ucred = NULL;
 
91
        if (getpeerucred (socket_fd, &ucred) == 0) {
 
92
                pid_read = ucred_getpid (ucred);
 
93
                uid_read = ucred_geteuid (ucred);
 
94
                ret = TRUE;
 
95
        } else {
 
96
                g_warning ("Failed to getpeerucred() credentials: %s\n",
 
97
                           g_strerror (errno));
 
98
        }
 
99
        if (ucred != NULL) {
 
100
                ucred_free (ucred);
 
101
        }
 
102
#else /* !SO_PEERCRED && !HAVE_GETPEERUCRED */
 
103
        g_warning ("Socket credentials not supported on this OS\n");
 
104
#endif
 
105
 
 
106
        if (pid != NULL) {
 
107
                *pid = pid_read;
 
108
        }
 
109
 
 
110
        if (uid != NULL) {
 
111
                *uid = uid_read;
 
112
        }
 
113
 
 
114
        return ret;
 
115
}
 
116
 
 
117
 
 
118
/*
 
119
 * getfd.c
 
120
 *
 
121
 * Get an fd for use with kbd/console ioctls.
 
122
 * We try several things because opening /dev/console will fail
 
123
 * if someone else used X (which does a chown on /dev/console).
 
124
 */
 
125
 
 
126
gboolean
 
127
ck_fd_is_a_console (int fd)
 
128
{
 
129
#ifdef __linux__
 
130
        struct vt_stat vts;
 
131
#elif defined(__FreeBSD__)
 
132
        int vers;
 
133
#endif
 
134
        int  kb_ok;
 
135
 
 
136
        errno = 0;
 
137
#ifdef __linux__
 
138
        kb_ok = (ioctl (fd, VT_GETSTATE, &vts) == 0);
 
139
#elif defined(__FreeBSD__)
 
140
        kb_ok = (ioctl (fd, CONS_GETVERS, &vers) == 0);
 
141
#else
 
142
        kb_ok = 1;
 
143
#endif
 
144
 
 
145
        return (isatty (fd) && kb_ok);
 
146
}
 
147
 
 
148
static int
 
149
open_a_console (char *fnam)
 
150
{
 
151
        int fd;
 
152
 
 
153
        fd = open (fnam, O_RDONLY | O_NOCTTY);
 
154
        if (fd < 0 && errno == EACCES)
 
155
                fd = open (fnam, O_WRONLY | O_NOCTTY);
 
156
 
 
157
        if (fd < 0)
 
158
                return -1;
 
159
 
 
160
        if (! ck_fd_is_a_console (fd)) {
 
161
                close (fd);
 
162
                fd = -1;
 
163
        }
 
164
 
 
165
        return fd;
 
166
}
 
167
 
 
168
int
 
169
ck_get_a_console_fd (void)
 
170
{
 
171
        int fd;
 
172
 
 
173
        fd = -1;
 
174
 
 
175
#ifdef __sun
 
176
        /* On Solaris, first try Sun VT device. */
 
177
        fd = open_a_console ("/dev/vt/active");
 
178
        if (fd >= 0) {
 
179
                goto done;
 
180
        }
 
181
        fd = open_a_console ("/dev/vt/0");
 
182
        if (fd >= 0) {
 
183
                goto done;
 
184
        }
 
185
#endif
 
186
 
 
187
#ifdef _PATH_TTY
 
188
        fd = open_a_console (_PATH_TTY);
 
189
        if (fd >= 0) {
 
190
                goto done;
 
191
        }
 
192
#endif
 
193
 
 
194
        fd = open_a_console ("/dev/tty");
 
195
        if (fd >= 0) {
 
196
                goto done;
 
197
        }
 
198
 
 
199
        fd = open_a_console ("/dev/tty0");
 
200
        if (fd >= 0) {
 
201
                goto done;
 
202
        }
 
203
 
 
204
#ifdef _PATH_CONSOLE
 
205
        fd = open_a_console (_PATH_CONSOLE);
 
206
        if (fd >= 0) {
 
207
                goto done;
 
208
        }
 
209
#endif
 
210
 
 
211
        fd = open_a_console ("/dev/console");
 
212
        if (fd >= 0) {
 
213
                goto done;
 
214
        }
 
215
 
 
216
        for (fd = 0; fd < 3; fd++) {
 
217
                if (ck_fd_is_a_console (fd)) {
 
218
                        goto done;
 
219
                }
 
220
        }
 
221
 done:
 
222
        return fd;
 
223
}
 
224
 
 
225
gboolean
 
226
ck_is_root_user (void)
 
227
{
 
228
#ifndef G_OS_WIN32
 
229
        uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
 
230
        gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
 
231
 
 
232
#ifdef HAVE_GETRESUID
 
233
        if (getresuid (&ruid, &euid, &suid) != 0 ||
 
234
            getresgid (&rgid, &egid, &sgid) != 0)
 
235
#endif /* HAVE_GETRESUID */
 
236
                {
 
237
                        suid = ruid = getuid ();
 
238
                        sgid = rgid = getgid ();
 
239
                        euid = geteuid ();
 
240
                        egid = getegid ();
 
241
                }
 
242
 
 
243
        if (ruid == 0) {
 
244
                return TRUE;
 
245
        }
 
246
 
 
247
#endif
 
248
        return FALSE;
 
249
}
 
250
 
 
251
gboolean
 
252
ck_wait_for_active_console_num (int   console_fd,
 
253
                                guint num)
 
254
{
 
255
        gboolean ret;
 
256
        int      res;
 
257
 
 
258
        g_assert (console_fd != -1);
 
259
 
 
260
 again:
 
261
        ret = FALSE;
 
262
 
 
263
        errno = 0;
 
264
#ifdef VT_WAITACTIVE
 
265
        g_debug ("VT_WAITACTIVE for vt %d", num);
 
266
        res = ioctl (console_fd, VT_WAITACTIVE, num);
 
267
        g_debug ("VT_WAITACTIVE for vt %d returned %d", num, res);
 
268
#else
 
269
        res = ERROR;
 
270
        errno = ENOTSUP;
 
271
#endif
 
272
 
 
273
        if (res == ERROR) {
 
274
                const char *errmsg;
 
275
 
 
276
                errmsg = g_strerror (errno);
 
277
 
 
278
                if (errno == EINTR) {
 
279
                        g_debug ("Interrupted waiting for native console %d activation: %s",
 
280
                                  num,
 
281
                                  errmsg);
 
282
                       goto again;
 
283
                } else if (errno == ENOTSUP) {
 
284
                        g_debug ("Console activation not supported on this system");
 
285
                } else {
 
286
                        g_warning ("Error waiting for native console %d activation: %s",
 
287
                                   num,
 
288
                                   errmsg);
 
289
                }
 
290
                goto out;
 
291
        }
 
292
 
 
293
        ret = TRUE;
 
294
 
 
295
 out:
 
296
        return ret;
 
297
}
 
298
 
 
299
gboolean
 
300
ck_activate_console_num (int   console_fd,
 
301
                         guint num)
 
302
{
 
303
        gboolean ret;
 
304
        int      res;
 
305
 
 
306
        g_assert (console_fd != -1);
 
307
 
 
308
        ret = FALSE;
 
309
 
 
310
        errno = 0;
 
311
#ifdef VT_ACTIVATE
 
312
        res = ioctl (console_fd, VT_ACTIVATE, num);
 
313
#else
 
314
        res = ERROR;
 
315
        errno = ENOTSUP;
 
316
#endif
 
317
 
 
318
        if (res == 0) {
 
319
                ret = TRUE;
 
320
        } else {
 
321
                if (errno == ENOTSUP) {
 
322
                        g_debug ("Console activation not supported on this system");
 
323
                } else {
 
324
                        g_warning ("Unable to activate console: %s", g_strerror (errno));
 
325
                }
 
326
        }
 
327
 
 
328
        return ret;
 
329
}