]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/session.c
Launchpad automatic translations update.
[sojka/lightdm.git] / src / session.c
1 /*
2  * Copyright (C) 2010-2011 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11
12 #include <config.h>
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <sys/wait.h>
19 #include <fcntl.h>
20 #include <glib/gstdio.h>
21 #include <grp.h>
22 #include <pwd.h>
23
24 #include "session.h"
25 #include "configuration.h"
26 #include "console-kit.h"
27 #include "login1.h"
28 #include "guest-account.h"
29
30 enum {
31     GOT_MESSAGES,
32     AUTHENTICATION_COMPLETE,
33     STOPPED,
34     LAST_SIGNAL
35 };
36 static guint signals[LAST_SIGNAL] = { 0 };
37
38 struct SessionPrivate
39 {
40     /* Session type */
41     gchar *session_type;
42
43     /* Display server running on */
44     DisplayServer *display_server;
45
46     /* PID of child process */
47     GPid pid;
48
49     /* Pipes to talk to child */
50     int to_child_input;
51     int from_child_output;
52     GIOChannel *from_child_channel;
53     guint from_child_watch;
54     guint child_watch;
55
56     /* User to authenticate as */
57     gchar *username;
58
59     /* TRUE if is a guest account */
60     gboolean is_guest;
61
62     /* User object that matches the current username */
63     User *user;
64
65     /* PAM service to use */
66     gchar *pam_service;
67
68     /* TRUE if should run PAM authentication phase */
69     gboolean do_authenticate;
70
71     /* TRUE if can handle PAM prompts */
72     gboolean is_interactive;
73
74     /* Messages being requested by PAM */
75     int messages_length;
76     struct pam_message *messages;
77
78     /* Authentication result from PAM */
79     gboolean authentication_started;
80     gboolean authentication_complete;
81     int authentication_result;
82     gchar *authentication_result_string;
83
84     /* File to log to */
85     gchar *log_filename;
86
87     /* tty this session is running on */
88     gchar *tty;
89
90     /* X display connected to */
91     gchar *xdisplay;
92     XAuthority *x_authority;
93     gboolean x_authority_use_system_location;
94
95     /* Remote host this session is being controlled from */
96     gchar *remote_host_name;
97
98     /* Console kit cookie */
99     gchar *console_kit_cookie;
100
101     /* login1 session */
102     gchar *login1_session;
103
104     /* Environment to set in child */
105     GList *env;
106
107     /* Command to run in child */
108     gchar **argv;
109
110     /* True if have run command */
111     gboolean command_run;
112
113     /* TRUE if stopping this session */
114     gboolean stopping;
115 };
116
117 /* Maximum length of a string to pass between daemon and session */
118 #define MAX_STRING_LENGTH 65535
119
120 static void session_logger_iface_init (LoggerInterface *iface);
121
122 G_DEFINE_TYPE_WITH_CODE (Session, session, G_TYPE_OBJECT,
123                          G_IMPLEMENT_INTERFACE (
124                              LOGGER_TYPE, session_logger_iface_init));
125
126 Session *
127 session_new (void)
128 {
129     return g_object_new (SESSION_TYPE, NULL);
130 }
131
132 void
133 session_set_session_type (Session *session, const gchar *session_type)
134 {
135     g_return_if_fail (session != NULL);
136     g_free (session->priv->session_type);
137     session->priv->session_type = g_strdup (session_type);
138 }
139
140 const gchar *
141 session_get_session_type (Session *session)
142 {
143     g_return_val_if_fail (session != NULL, NULL);
144     return session->priv->session_type;
145 }
146
147 void
148 session_set_pam_service (Session *session, const gchar *pam_service)
149 {
150     g_return_if_fail (session != NULL);
151     g_free (session->priv->pam_service);
152     session->priv->pam_service = g_strdup (pam_service);
153 }
154
155 void
156 session_set_username (Session *session, const gchar *username)
157 {
158     g_return_if_fail (session != NULL);
159     g_free (session->priv->username);
160     session->priv->username = g_strdup (username);
161 }
162
163 void
164 session_set_do_authenticate (Session *session, gboolean do_authenticate)
165 {
166     g_return_if_fail (session != NULL);
167     session->priv->do_authenticate = do_authenticate;
168 }
169
170 void
171 session_set_is_interactive (Session *session, gboolean is_interactive)
172 {
173     g_return_if_fail (session != NULL);
174     session->priv->is_interactive = is_interactive;
175 }
176
177 void
178 session_set_is_guest (Session *session, gboolean is_guest)
179 {
180     g_return_if_fail (session != NULL);
181     session->priv->is_guest = is_guest;
182 }
183
184 gboolean
185 session_get_is_guest (Session *session)
186 {
187     g_return_val_if_fail (session != NULL, FALSE);
188     return session->priv->is_guest;
189 }
190
191 void
192 session_set_log_file (Session *session, const gchar *filename)
193 {
194     g_return_if_fail (session != NULL);
195     g_free (session->priv->log_filename);
196     session->priv->log_filename = g_strdup (filename);
197 }
198
199 void
200 session_set_display_server (Session *session, DisplayServer *display_server)
201 {
202     g_return_if_fail (session != NULL);
203     g_return_if_fail (display_server != NULL);
204     if (session->priv->display_server)
205     {
206         display_server_disconnect_session (session->priv->display_server, session);
207         g_object_unref (session->priv->display_server);
208     }
209     session->priv->display_server = g_object_ref (display_server);
210 }
211
212 DisplayServer *
213 session_get_display_server (Session *session)
214 {
215     g_return_val_if_fail (session != NULL, NULL);
216     return session->priv->display_server;
217 }
218
219 void
220 session_set_tty (Session *session, const gchar *tty)
221 {
222     g_return_if_fail (session != NULL);
223     g_free (session->priv->tty);
224     session->priv->tty = g_strdup (tty);
225 }
226
227 void
228 session_set_xdisplay (Session *session, const gchar *xdisplay)
229 {
230     g_return_if_fail (session != NULL);
231     g_free (session->priv->xdisplay);
232     session->priv->xdisplay = g_strdup (xdisplay);
233 }
234
235 void
236 session_set_x_authority (Session *session, XAuthority *authority, gboolean use_system_location)
237 {
238     g_return_if_fail (session != NULL);
239     if (session->priv->x_authority)
240     {
241         g_object_unref (session->priv->x_authority);
242         session->priv->x_authority = NULL;
243     }
244     if (authority)
245         session->priv->x_authority = g_object_ref (authority);
246     session->priv->x_authority_use_system_location = use_system_location;
247 }
248
249 void
250 session_set_remote_host_name (Session *session, const gchar *remote_host_name)
251 {
252     g_return_if_fail (session != NULL);
253     g_free (session->priv->remote_host_name);
254     session->priv->remote_host_name = g_strdup (remote_host_name);
255 }
256
257 static GList *
258 find_env_entry (Session *session, const gchar *name)
259 {
260     GList *link;
261
262     for (link = session->priv->env; link; link = link->next)
263     {
264         const gchar *entry = link->data;
265
266         if (g_str_has_prefix (entry, name) && entry[strlen (name)] == '=')
267             return link;
268     }
269
270     return NULL;
271 }
272
273 void
274 session_set_env (Session *session, const gchar *name, const gchar *value)
275 {
276     GList *link;
277     gchar *entry;
278
279     g_return_if_fail (session != NULL);
280     g_return_if_fail (value != NULL);
281
282     entry = g_strdup_printf ("%s=%s", name, value);
283
284     link = find_env_entry (session, name);
285     if (link)
286     {
287         g_free (link->data);
288         link->data = entry;
289     }
290     else
291         session->priv->env = g_list_append (session->priv->env, entry);
292 }
293
294 void
295 session_unset_env (Session *session, const gchar *name)
296 {
297     GList *link;
298
299     g_return_if_fail (session != NULL);
300   
301     link = find_env_entry (session, name);
302     if (!link)
303         return;
304
305     g_free (link->data);
306     session->priv->env = g_list_remove_link (session->priv->env, link);
307 }
308
309 void
310 session_set_argv (Session *session, gchar **argv)
311 {
312     g_return_if_fail (session != NULL);
313     session->priv->argv = g_strdupv (argv);
314 }
315
316 User *
317 session_get_user (Session *session)
318 {
319     g_return_val_if_fail (session != NULL, NULL);
320
321     if (session->priv->username == NULL)
322         return NULL;
323
324     if (!session->priv->user)
325         session->priv->user = accounts_get_user_by_name (session->priv->username);
326
327     return session->priv->user;
328 }
329
330 static void
331 write_data (Session *session, const void *buf, size_t count)
332 {
333     if (write (session->priv->to_child_input, buf, count) != count)
334         l_warning (session, "Error writing to session: %s", strerror (errno));
335 }
336
337 static void
338 write_string (Session *session, const char *value)
339 {
340     int length;
341
342     length = value ? strlen (value) : -1;
343     write_data (session, &length, sizeof (length));
344     if (value)
345         write_data (session, value, sizeof (char) * length);
346 }
347
348 static void
349 write_xauth (Session *session, XAuthority *x_authority)
350 {
351     guint16 family;
352     gsize length;
353
354     if (!x_authority)
355     {
356         write_string (session, NULL);
357         return;
358     }
359
360     write_string (session, x_authority_get_authorization_name (session->priv->x_authority));
361     family = x_authority_get_family (session->priv->x_authority);
362     write_data (session, &family, sizeof (family));
363     length = x_authority_get_address_length (session->priv->x_authority);
364     write_data (session, &length, sizeof (length));
365     write_data (session, x_authority_get_address (session->priv->x_authority), length);
366     write_string (session, x_authority_get_number (session->priv->x_authority));
367     length = x_authority_get_authorization_data_length (session->priv->x_authority);
368     write_data (session, &length, sizeof (length));
369     write_data (session, x_authority_get_authorization_data (session->priv->x_authority), length);
370 }
371
372 static ssize_t
373 read_from_child (Session *session, void *buf, size_t count)
374 {
375     ssize_t n_read;
376     n_read = read (session->priv->from_child_output, buf, count);
377     if (n_read < 0)
378         l_warning (session, "Error reading from session: %s", strerror (errno));
379     return n_read;
380 }
381
382 static gchar *
383 read_string_from_child (Session *session)
384 {
385     int length;
386     char *value;
387
388     if (read_from_child (session, &length, sizeof (length)) <= 0)
389         return NULL;
390     if (length < 0)
391         return NULL;
392     if (length > MAX_STRING_LENGTH)
393     {
394         l_warning (session, "Invalid string length %d from child", length);
395         return NULL;
396     }
397
398     value = g_malloc (sizeof (char) * (length + 1));
399     read_from_child (session, value, length);
400     value[length] = '\0';
401
402     return value;
403 }
404
405 static void
406 session_watch_cb (GPid pid, gint status, gpointer data)
407 {
408     Session *session = data;
409
410     if (WIFEXITED (status))
411         l_debug (session, "Exited with return value %d", WEXITSTATUS (status));
412     else if (WIFSIGNALED (status))
413         l_debug (session, "Terminated with signal %d", WTERMSIG (status));
414
415     /* do this as late as possible for log messages prefix */
416     session->priv->pid = 0;
417
418     /* If failed during authentication then report this as an authentication failure */
419     if (session->priv->authentication_started && !session->priv->authentication_complete)
420     {
421         l_debug (session, "Failed during authentication");
422         session->priv->authentication_complete = TRUE;
423         session->priv->authentication_result = PAM_CONV_ERR;
424         g_free (session->priv->authentication_result_string);
425         session->priv->authentication_result_string = g_strdup ("Authentication stopped before completion");
426         g_signal_emit (G_OBJECT (session), signals[AUTHENTICATION_COMPLETE], 0);
427     }
428
429     g_signal_emit (G_OBJECT (session), signals[STOPPED], 0);
430
431     /* Delete account if it is a guest one */
432     if (session->priv->is_guest)
433         guest_account_cleanup (session->priv->username);
434
435     /* Drop our reference on the child process, it has terminated */
436     g_object_unref (session);
437 }
438
439 static gboolean
440 from_child_cb (GIOChannel *source, GIOCondition condition, gpointer data)
441 {
442     Session *session = data;
443     gchar *username;
444     ssize_t n_read;
445     gboolean auth_complete;
446
447     /* Remote end gone */
448     if (condition == G_IO_HUP)
449     {
450         session->priv->from_child_watch = 0;
451         return FALSE;
452     }
453
454     /* Get the username currently being authenticated (may change during authentication) */
455     username = read_string_from_child (session);
456     if (g_strcmp0 (username, session->priv->username) != 0)
457     {
458         g_free (session->priv->username);
459         session->priv->username = username;
460         if (session->priv->user)
461             g_object_unref (session->priv->user);
462         session->priv->user = NULL;
463     }
464     else
465         g_free (username);
466
467     /* Check if authentication completed */
468     n_read = read_from_child (session, &auth_complete, sizeof (auth_complete));
469     if (n_read < 0)
470         l_debug (session, "Error reading from child: %s", strerror (errno));
471     if (n_read <= 0)
472     {
473         session->priv->from_child_watch = 0;
474         return FALSE;
475     }
476
477     if (auth_complete)
478     {
479         session->priv->authentication_complete = TRUE;
480         read_from_child (session, &session->priv->authentication_result, sizeof (session->priv->authentication_result));
481         g_free (session->priv->authentication_result_string);
482         session->priv->authentication_result_string = read_string_from_child (session);
483
484         l_debug (session, "Authentication complete with return value %d: %s", session->priv->authentication_result, session->priv->authentication_result_string);
485
486         /* No longer expect any more messages */
487         session->priv->from_child_watch = 0;
488
489         g_signal_emit (G_OBJECT (session), signals[AUTHENTICATION_COMPLETE], 0);
490
491         return FALSE;
492     }
493     else
494     {
495         int i;
496
497         session->priv->messages_length = 0;
498         read_from_child (session, &session->priv->messages_length, sizeof (session->priv->messages_length));
499         session->priv->messages = calloc (session->priv->messages_length, sizeof (struct pam_message));
500         for (i = 0; i < session->priv->messages_length; i++)
501         {
502             struct pam_message *m = &session->priv->messages[i];
503             read_from_child (session, &m->msg_style, sizeof (m->msg_style));
504             m->msg = read_string_from_child (session);
505         }
506
507         l_debug (session, "Got %d message(s) from PAM", session->priv->messages_length);
508
509         g_signal_emit (G_OBJECT (session), signals[GOT_MESSAGES], 0);
510     }
511
512     return TRUE;
513 }
514
515 gboolean
516 session_start (Session *session)
517 {
518     g_return_val_if_fail (session != NULL, FALSE);
519     return SESSION_GET_CLASS (session)->start (session);
520 }
521
522 gboolean
523 session_get_is_started (Session *session)
524 {
525     return session->priv->pid != 0;
526 }
527
528 static gboolean
529 session_real_start (Session *session)
530 {
531     int version;
532     int to_child_pipe[2], from_child_pipe[2];
533     int to_child_output, from_child_input;
534
535     g_return_val_if_fail (session->priv->pid == 0, FALSE);
536
537     if (session->priv->display_server)
538         display_server_connect_session (session->priv->display_server, session);
539
540     /* Create pipes to talk to the child */
541     if (pipe (to_child_pipe) < 0 || pipe (from_child_pipe) < 0)
542     {
543         g_warning ("Failed to create pipe to communicate with session process: %s", strerror (errno));
544         return FALSE;
545     }
546     to_child_output = to_child_pipe[0];
547     session->priv->to_child_input = to_child_pipe[1];
548     session->priv->from_child_output = from_child_pipe[0];
549     from_child_input = from_child_pipe[1];
550     session->priv->from_child_channel = g_io_channel_unix_new (session->priv->from_child_output);
551     session->priv->from_child_watch = g_io_add_watch (session->priv->from_child_channel, G_IO_IN | G_IO_HUP, from_child_cb, session);
552
553     /* Don't allow the daemon end of the pipes to be accessed in child processes */
554     fcntl (session->priv->to_child_input, F_SETFD, FD_CLOEXEC);
555     fcntl (session->priv->from_child_output, F_SETFD, FD_CLOEXEC);
556
557     /* Create the guest account if it is one */
558     if (session->priv->is_guest && session->priv->username == NULL)
559     {
560         session->priv->username = guest_account_setup ();
561         if (!session->priv->username)
562             return FALSE;
563     }
564
565     /* Run the child */
566     session->priv->pid = fork ();
567     if (session->priv->pid < 0)
568     {
569         g_debug ("Failed to fork session child process: %s", strerror (errno));
570         return FALSE;
571     }
572
573     if (session->priv->pid == 0)
574     {
575         /* Run us again in session child mode */
576         execlp ("lightdm",
577                 "lightdm",
578                 "--session-child",
579                 g_strdup_printf ("%d", to_child_output),
580                 g_strdup_printf ("%d", from_child_input),
581                 NULL);
582         _exit (EXIT_FAILURE);
583     }
584
585     /* Hold a reference on this object until the child process terminates so we
586      * can handle the watch callback even if it is no longer used. Otherwise a
587      * zombie process will remain */
588     g_object_ref (session);
589
590     /* Listen for session termination */
591     session->priv->authentication_started = TRUE;
592     session->priv->child_watch = g_child_watch_add (session->priv->pid, session_watch_cb, session);
593
594     /* Close the ends of the pipes we don't need */
595     close (to_child_output);
596     close (from_child_input);
597
598     /* Indicate what version of the protocol we are using */
599     version = 1;
600     write_data (session, &version, sizeof (version));
601
602     /* Send configuration */
603     write_string (session, session->priv->pam_service);
604     write_string (session, session->priv->username);
605     write_data (session, &session->priv->do_authenticate, sizeof (session->priv->do_authenticate));
606     write_data (session, &session->priv->is_interactive, sizeof (session->priv->is_interactive));
607     write_string (session, NULL); /* Used to be class, now we just use the environment variable */
608     write_string (session, session->priv->tty);
609     write_string (session, session->priv->remote_host_name);
610     write_string (session, session->priv->xdisplay);
611     write_xauth (session, session->priv->x_authority);
612
613     l_debug (session, "Started with service '%s', username '%s'", session->priv->pam_service, session->priv->username);
614
615     return TRUE;
616 }
617
618 const gchar *
619 session_get_username (Session *session)
620 {
621     g_return_val_if_fail (session != NULL, NULL);
622     return session->priv->username;
623 }
624
625 const gchar *
626 session_get_console_kit_cookie (Session *session)
627 {
628     g_return_val_if_fail (session != NULL, NULL);
629     return session->priv->console_kit_cookie;
630 }
631
632 void
633 session_respond (Session *session, struct pam_response *response)
634 {
635     int error = PAM_SUCCESS;
636     int i;
637
638     g_return_if_fail (session != NULL);
639
640     write_data (session, &error, sizeof (error));
641     for (i = 0; i < session->priv->messages_length; i++)
642     {
643         write_string (session, response[i].resp);
644         write_data (session, &response[i].resp_retcode, sizeof (response[i].resp_retcode));
645     }
646
647     /* Delete the old messages */
648     for (i = 0; i < session->priv->messages_length; i++)
649         g_free ((char *) session->priv->messages[i].msg);
650     g_free (session->priv->messages);
651     session->priv->messages = NULL;
652     session->priv->messages_length = 0;
653 }
654
655 void
656 session_respond_error (Session *session, int error)
657 {
658     g_return_if_fail (session != NULL);
659     g_return_if_fail (error != PAM_SUCCESS);
660
661     write_data (session, &error, sizeof (error));
662 }
663
664 int
665 session_get_messages_length (Session *session)
666 {
667     g_return_val_if_fail (session != NULL, 0);
668     return session->priv->messages_length;
669 }
670
671 const struct pam_message *
672 session_get_messages (Session *session)
673 {
674     g_return_val_if_fail (session != NULL, NULL);
675     return session->priv->messages;
676 }
677
678 gboolean
679 session_get_is_authenticated (Session *session)
680 {
681     g_return_val_if_fail (session != NULL, FALSE);
682     return session->priv->authentication_complete && session->priv->authentication_result == PAM_SUCCESS;
683 }
684
685 int
686 session_get_authentication_result (Session *session)
687 {
688     g_return_val_if_fail (session != NULL, 0);
689     return session->priv->authentication_result;
690 }
691
692 const gchar *
693 session_get_authentication_result_string (Session *session)
694 {
695     g_return_val_if_fail (session != NULL, NULL);
696     return session->priv->authentication_result_string;
697 }
698
699 void
700 session_run (Session *session)
701 {
702     g_return_if_fail (session->priv->display_server != NULL);
703     return SESSION_GET_CLASS (session)->run (session);
704 }
705
706 static void
707 session_real_run (Session *session)
708 {
709     gsize i, argc;
710     gchar *command, *x_authority_filename;
711     GList *link;
712
713     g_return_if_fail (session != NULL);
714     g_return_if_fail (!session->priv->command_run);
715     g_return_if_fail (session_get_is_authenticated (session));
716     g_return_if_fail (session->priv->argv != NULL);
717     g_return_if_fail (session->priv->pid != 0);
718
719     display_server_connect_session (session->priv->display_server, session);
720
721     session->priv->command_run = TRUE;
722
723     command = g_strjoinv (" ", session->priv->argv);
724     l_debug (session, "Running command %s", command);
725     g_free (command);
726
727     /* Create authority location */
728     if (session->priv->x_authority_use_system_location)
729     {
730         gchar *run_dir, *dir;
731
732         run_dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
733         dir = g_build_filename (run_dir, session->priv->username, NULL);
734         g_free (run_dir);
735
736         if (g_mkdir_with_parents (dir, S_IRWXU) < 0)
737             l_warning (session, "Failed to set create system authority dir %s: %s", dir, strerror (errno));
738         if (getuid () == 0)
739         {
740             if (chown (dir, user_get_uid (session_get_user (session)), user_get_gid (session_get_user (session))) < 0)
741                 l_warning (session, "Failed to set ownership of user authority dir: %s", strerror (errno));
742         }
743
744         x_authority_filename = g_build_filename (dir, "xauthority", NULL);
745         g_free (dir);
746     }
747     else
748         x_authority_filename = g_build_filename (user_get_home_directory (session_get_user (session)), ".Xauthority", NULL);
749
750     if (session->priv->log_filename)
751         l_debug (session, "Logging to %s", session->priv->log_filename);
752     write_string (session, session->priv->log_filename);
753     write_string (session, session->priv->tty);
754     write_string (session, x_authority_filename);
755     g_free (x_authority_filename);
756     write_string (session, session->priv->xdisplay);
757     write_xauth (session, session->priv->x_authority);
758     argc = g_list_length (session->priv->env);
759     write_data (session, &argc, sizeof (argc));
760     for (link = session->priv->env; link; link = link->next)
761         write_string (session, (gchar *) link->data);
762     argc = g_strv_length (session->priv->argv);
763     write_data (session, &argc, sizeof (argc));
764     for (i = 0; i < argc; i++)
765         write_string (session, session->priv->argv[i]);
766
767     if (login1_is_running ())
768         session->priv->login1_session = read_string_from_child (session);
769     if (!session->priv->login1_session)
770         session->priv->console_kit_cookie = read_string_from_child (session);
771 }
772
773 void
774 session_lock (Session *session)
775 {
776     g_return_if_fail (session != NULL);
777     if (getuid () == 0)
778     {
779         if (session->priv->login1_session)
780             login1_lock_session (session->priv->login1_session);
781         else if (session->priv->console_kit_cookie)
782             ck_lock_session (session->priv->console_kit_cookie);
783     }
784 }
785
786 void
787 session_unlock (Session *session)
788 {
789     g_return_if_fail (session != NULL);
790     if (getuid () == 0)
791     {
792         if (session->priv->login1_session)
793             login1_unlock_session (session->priv->login1_session);
794         else if (session->priv->console_kit_cookie)
795             ck_unlock_session (session->priv->console_kit_cookie);
796     }
797 }
798
799 void
800 session_stop (Session *session)
801 {
802     g_return_if_fail (session != NULL);
803
804     if (session->priv->stopping)
805         return;
806     session->priv->stopping = TRUE;
807
808     return SESSION_GET_CLASS (session)->stop (session);
809 }
810
811 static void
812 session_real_stop (Session *session)
813 {
814     g_return_if_fail (session != NULL);
815
816     if (session->priv->pid > 0)
817     {
818         l_debug (session, "Sending SIGTERM");
819         kill (session->priv->pid, SIGTERM);
820         // FIXME: Handle timeout
821     }
822     else
823         g_signal_emit (G_OBJECT (session), signals[STOPPED], 0);
824 }
825
826 gboolean
827 session_get_is_stopping (Session *session)
828 {
829     g_return_val_if_fail (session != NULL, FALSE);
830     return session->priv->stopping;
831 }
832
833 static void
834 session_init (Session *session)
835 {
836     session->priv = G_TYPE_INSTANCE_GET_PRIVATE (session, SESSION_TYPE, SessionPrivate);
837     session->priv->log_filename = g_strdup (".xsession-errors");
838 }
839
840 static void
841 session_finalize (GObject *object)
842 {
843     Session *self = SESSION (object);
844     int i;
845
846     g_free (self->priv->session_type);
847     if (self->priv->display_server)
848         g_object_unref (self->priv->display_server);
849     if (self->priv->pid)
850         kill (self->priv->pid, SIGKILL);
851     if (self->priv->from_child_channel)
852         g_io_channel_unref (self->priv->from_child_channel);
853     if (self->priv->from_child_watch)
854         g_source_remove (self->priv->from_child_watch);
855     if (self->priv->child_watch)
856         g_source_remove (self->priv->child_watch);
857     g_free (self->priv->username);
858     if (self->priv->user)
859         g_object_unref (self->priv->user);
860     g_free (self->priv->pam_service);
861     for (i = 0; i < self->priv->messages_length; i++)
862         g_free ((char *) self->priv->messages[i].msg);
863     g_free (self->priv->messages);
864     g_free (self->priv->authentication_result_string);
865     g_free (self->priv->log_filename);
866     g_free (self->priv->tty);
867     g_free (self->priv->xdisplay);
868     if (self->priv->x_authority)
869         g_object_unref (self->priv->x_authority);
870     g_free (self->priv->remote_host_name);
871     g_free (self->priv->login1_session);
872     g_free (self->priv->console_kit_cookie);
873     g_list_free_full (self->priv->env, g_free);
874     g_strfreev (self->priv->argv);
875
876     G_OBJECT_CLASS (session_parent_class)->finalize (object);
877 }
878
879 static void
880 session_class_init (SessionClass *klass)
881 {
882     GObjectClass *object_class = G_OBJECT_CLASS (klass);
883
884     klass->start = session_real_start;
885     klass->run = session_real_run;
886     klass->stop = session_real_stop;
887     object_class->finalize = session_finalize;
888
889     g_type_class_add_private (klass, sizeof (SessionPrivate));
890
891     signals[GOT_MESSAGES] =
892         g_signal_new ("got-messages",
893                       G_TYPE_FROM_CLASS (klass),
894                       G_SIGNAL_RUN_LAST,
895                       G_STRUCT_OFFSET (SessionClass, got_messages),
896                       NULL, NULL,
897                       NULL,
898                       G_TYPE_NONE, 0);
899
900     signals[AUTHENTICATION_COMPLETE] =
901         g_signal_new ("authentication-complete",
902                       G_TYPE_FROM_CLASS (klass),
903                       G_SIGNAL_RUN_LAST,
904                       G_STRUCT_OFFSET (SessionClass, authentication_complete),
905                       NULL, NULL,
906                       NULL,
907                       G_TYPE_NONE, 0);
908
909     signals[STOPPED] =
910         g_signal_new ("stopped",
911                       G_TYPE_FROM_CLASS (klass),
912                       G_SIGNAL_RUN_LAST,
913                       G_STRUCT_OFFSET (SessionClass, stopped),
914                       NULL, NULL,
915                       NULL,
916                       G_TYPE_NONE, 0);
917 }
918
919 static gint
920 session_real_logprefix (Logger *self, gchar *buf, gulong buflen)
921 {
922     Session *session = SESSION (self);
923     if (session->priv->pid != 0)
924         return g_snprintf (buf, buflen, "Session pid=%d: ", session->priv->pid);
925     else
926         return g_snprintf (buf, buflen, "Session: ");
927 }
928
929 static void
930 session_logger_iface_init (LoggerInterface *iface)
931 {
932     iface->logprefix = &session_real_logprefix;
933 }