]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/session.c
Merge from trunk
[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 const gchar *
295 session_get_env (Session *session, const gchar *name)
296 {
297     GList *link;
298     gchar *entry;
299
300     link = find_env_entry (session, name);
301     if (!link)
302         return NULL;
303   
304     entry = link->data;
305
306     return entry + strlen (name) + 1;
307 }
308
309 void
310 session_unset_env (Session *session, const gchar *name)
311 {
312     GList *link;
313
314     g_return_if_fail (session != NULL);
315   
316     link = find_env_entry (session, name);
317     if (!link)
318         return;
319
320     g_free (link->data);
321     session->priv->env = g_list_remove_link (session->priv->env, link);
322 }
323
324 void
325 session_set_argv (Session *session, gchar **argv)
326 {
327     g_return_if_fail (session != NULL);
328     session->priv->argv = g_strdupv (argv);
329 }
330
331 User *
332 session_get_user (Session *session)
333 {
334     g_return_val_if_fail (session != NULL, NULL);
335
336     if (session->priv->username == NULL)
337         return NULL;
338
339     if (!session->priv->user)
340         session->priv->user = accounts_get_user_by_name (session->priv->username);
341
342     return session->priv->user;
343 }
344
345 static void
346 write_data (Session *session, const void *buf, size_t count)
347 {
348     if (write (session->priv->to_child_input, buf, count) != count)
349         l_warning (session, "Error writing to session: %s", strerror (errno));
350 }
351
352 static void
353 write_string (Session *session, const char *value)
354 {
355     int length;
356
357     length = value ? strlen (value) : -1;
358     write_data (session, &length, sizeof (length));
359     if (value)
360         write_data (session, value, sizeof (char) * length);
361 }
362
363 static void
364 write_xauth (Session *session, XAuthority *x_authority)
365 {
366     guint16 family;
367     gsize length;
368
369     if (!x_authority)
370     {
371         write_string (session, NULL);
372         return;
373     }
374
375     write_string (session, x_authority_get_authorization_name (session->priv->x_authority));
376     family = x_authority_get_family (session->priv->x_authority);
377     write_data (session, &family, sizeof (family));
378     length = x_authority_get_address_length (session->priv->x_authority);
379     write_data (session, &length, sizeof (length));
380     write_data (session, x_authority_get_address (session->priv->x_authority), length);
381     write_string (session, x_authority_get_number (session->priv->x_authority));
382     length = x_authority_get_authorization_data_length (session->priv->x_authority);
383     write_data (session, &length, sizeof (length));
384     write_data (session, x_authority_get_authorization_data (session->priv->x_authority), length);
385 }
386
387 static ssize_t
388 read_from_child (Session *session, void *buf, size_t count)
389 {
390     ssize_t n_read;
391     n_read = read (session->priv->from_child_output, buf, count);
392     if (n_read < 0)
393         l_warning (session, "Error reading from session: %s", strerror (errno));
394     return n_read;
395 }
396
397 static gchar *
398 read_string_from_child (Session *session)
399 {
400     int length;
401     char *value;
402
403     if (read_from_child (session, &length, sizeof (length)) <= 0)
404         return NULL;
405     if (length < 0)
406         return NULL;
407     if (length > MAX_STRING_LENGTH)
408     {
409         l_warning (session, "Invalid string length %d from child", length);
410         return NULL;
411     }
412
413     value = g_malloc (sizeof (char) * (length + 1));
414     read_from_child (session, value, length);
415     value[length] = '\0';
416
417     return value;
418 }
419
420 static void
421 session_watch_cb (GPid pid, gint status, gpointer data)
422 {
423     Session *session = data;
424
425     if (WIFEXITED (status))
426         l_debug (session, "Exited with return value %d", WEXITSTATUS (status));
427     else if (WIFSIGNALED (status))
428         l_debug (session, "Terminated with signal %d", WTERMSIG (status));
429
430     /* do this as late as possible for log messages prefix */
431     session->priv->pid = 0;
432
433     /* If failed during authentication then report this as an authentication failure */
434     if (session->priv->authentication_started && !session->priv->authentication_complete)
435     {
436         l_debug (session, "Failed during authentication");
437         session->priv->authentication_complete = TRUE;
438         session->priv->authentication_result = PAM_CONV_ERR;
439         g_free (session->priv->authentication_result_string);
440         session->priv->authentication_result_string = g_strdup ("Authentication stopped before completion");
441         g_signal_emit (G_OBJECT (session), signals[AUTHENTICATION_COMPLETE], 0);
442     }
443
444     g_signal_emit (G_OBJECT (session), signals[STOPPED], 0);
445
446     /* Delete account if it is a guest one */
447     if (session->priv->is_guest)
448         guest_account_cleanup (session->priv->username);
449
450     /* Drop our reference on the child process, it has terminated */
451     g_object_unref (session);
452 }
453
454 static gboolean
455 from_child_cb (GIOChannel *source, GIOCondition condition, gpointer data)
456 {
457     Session *session = data;
458     gchar *username;
459     ssize_t n_read;
460     gboolean auth_complete;
461
462     /* Remote end gone */
463     if (condition == G_IO_HUP)
464     {
465         session->priv->from_child_watch = 0;
466         return FALSE;
467     }
468
469     /* Get the username currently being authenticated (may change during authentication) */
470     username = read_string_from_child (session);
471     if (g_strcmp0 (username, session->priv->username) != 0)
472     {
473         g_free (session->priv->username);
474         session->priv->username = username;
475         if (session->priv->user)
476             g_object_unref (session->priv->user);
477         session->priv->user = NULL;
478     }
479     else
480         g_free (username);
481
482     /* Check if authentication completed */
483     n_read = read_from_child (session, &auth_complete, sizeof (auth_complete));
484     if (n_read < 0)
485         l_debug (session, "Error reading from child: %s", strerror (errno));
486     if (n_read <= 0)
487     {
488         session->priv->from_child_watch = 0;
489         return FALSE;
490     }
491
492     if (auth_complete)
493     {
494         session->priv->authentication_complete = TRUE;
495         read_from_child (session, &session->priv->authentication_result, sizeof (session->priv->authentication_result));
496         g_free (session->priv->authentication_result_string);
497         session->priv->authentication_result_string = read_string_from_child (session);
498
499         l_debug (session, "Authentication complete with return value %d: %s", session->priv->authentication_result, session->priv->authentication_result_string);
500
501         /* No longer expect any more messages */
502         session->priv->from_child_watch = 0;
503
504         g_signal_emit (G_OBJECT (session), signals[AUTHENTICATION_COMPLETE], 0);
505
506         return FALSE;
507     }
508     else
509     {
510         int i;
511
512         session->priv->messages_length = 0;
513         read_from_child (session, &session->priv->messages_length, sizeof (session->priv->messages_length));
514         session->priv->messages = calloc (session->priv->messages_length, sizeof (struct pam_message));
515         for (i = 0; i < session->priv->messages_length; i++)
516         {
517             struct pam_message *m = &session->priv->messages[i];
518             read_from_child (session, &m->msg_style, sizeof (m->msg_style));
519             m->msg = read_string_from_child (session);
520         }
521
522         l_debug (session, "Got %d message(s) from PAM", session->priv->messages_length);
523
524         g_signal_emit (G_OBJECT (session), signals[GOT_MESSAGES], 0);
525     }
526
527     return TRUE;
528 }
529
530 gboolean
531 session_start (Session *session)
532 {
533     g_return_val_if_fail (session != NULL, FALSE);
534     return SESSION_GET_CLASS (session)->start (session);
535 }
536
537 gboolean
538 session_get_is_started (Session *session)
539 {
540     return session->priv->pid != 0;
541 }
542
543 static gboolean
544 session_real_start (Session *session)
545 {
546     int version;
547     int to_child_pipe[2], from_child_pipe[2];
548     int to_child_output, from_child_input;
549
550     g_return_val_if_fail (session->priv->pid == 0, FALSE);
551
552     if (session->priv->display_server)
553         display_server_connect_session (session->priv->display_server, session);
554
555     /* Create pipes to talk to the child */
556     if (pipe (to_child_pipe) < 0 || pipe (from_child_pipe) < 0)
557     {
558         g_warning ("Failed to create pipe to communicate with session process: %s", strerror (errno));
559         return FALSE;
560     }
561     to_child_output = to_child_pipe[0];
562     session->priv->to_child_input = to_child_pipe[1];
563     session->priv->from_child_output = from_child_pipe[0];
564     from_child_input = from_child_pipe[1];
565     session->priv->from_child_channel = g_io_channel_unix_new (session->priv->from_child_output);
566     session->priv->from_child_watch = g_io_add_watch (session->priv->from_child_channel, G_IO_IN | G_IO_HUP, from_child_cb, session);
567
568     /* Don't allow the daemon end of the pipes to be accessed in child processes */
569     fcntl (session->priv->to_child_input, F_SETFD, FD_CLOEXEC);
570     fcntl (session->priv->from_child_output, F_SETFD, FD_CLOEXEC);
571
572     /* Create the guest account if it is one */
573     if (session->priv->is_guest && session->priv->username == NULL)
574     {
575         session->priv->username = guest_account_setup ();
576         if (!session->priv->username)
577             return FALSE;
578     }
579
580     /* Run the child */
581     session->priv->pid = fork ();
582     if (session->priv->pid < 0)
583     {
584         g_debug ("Failed to fork session child process: %s", strerror (errno));
585         return FALSE;
586     }
587
588     if (session->priv->pid == 0)
589     {
590         /* Run us again in session child mode */
591         execlp ("lightdm",
592                 "lightdm",
593                 "--session-child",
594                 g_strdup_printf ("%d", to_child_output),
595                 g_strdup_printf ("%d", from_child_input),
596                 NULL);
597         _exit (EXIT_FAILURE);
598     }
599
600     /* Hold a reference on this object until the child process terminates so we
601      * can handle the watch callback even if it is no longer used. Otherwise a
602      * zombie process will remain */
603     g_object_ref (session);
604
605     /* Listen for session termination */
606     session->priv->authentication_started = TRUE;
607     session->priv->child_watch = g_child_watch_add (session->priv->pid, session_watch_cb, session);
608
609     /* Close the ends of the pipes we don't need */
610     close (to_child_output);
611     close (from_child_input);
612
613     /* Indicate what version of the protocol we are using */
614     version = 1;
615     write_data (session, &version, sizeof (version));
616
617     /* Send configuration */
618     write_string (session, session->priv->pam_service);
619     write_string (session, session->priv->username);
620     write_data (session, &session->priv->do_authenticate, sizeof (session->priv->do_authenticate));
621     write_data (session, &session->priv->is_interactive, sizeof (session->priv->is_interactive));
622     write_string (session, NULL); /* Used to be class, now we just use the environment variable */
623     write_string (session, session->priv->tty);
624     write_string (session, session->priv->remote_host_name);
625     write_string (session, session->priv->xdisplay);
626     write_xauth (session, session->priv->x_authority);
627
628     l_debug (session, "Started with service '%s', username '%s'", session->priv->pam_service, session->priv->username);
629
630     return TRUE;
631 }
632
633 const gchar *
634 session_get_username (Session *session)
635 {
636     g_return_val_if_fail (session != NULL, NULL);
637     return session->priv->username;
638 }
639
640 const gchar *
641 session_get_console_kit_cookie (Session *session)
642 {
643     g_return_val_if_fail (session != NULL, NULL);
644     return session->priv->console_kit_cookie;
645 }
646
647 void
648 session_respond (Session *session, struct pam_response *response)
649 {
650     int error = PAM_SUCCESS;
651     int i;
652
653     g_return_if_fail (session != NULL);
654
655     write_data (session, &error, sizeof (error));
656     for (i = 0; i < session->priv->messages_length; i++)
657     {
658         write_string (session, response[i].resp);
659         write_data (session, &response[i].resp_retcode, sizeof (response[i].resp_retcode));
660     }
661
662     /* Delete the old messages */
663     for (i = 0; i < session->priv->messages_length; i++)
664         g_free ((char *) session->priv->messages[i].msg);
665     g_free (session->priv->messages);
666     session->priv->messages = NULL;
667     session->priv->messages_length = 0;
668 }
669
670 void
671 session_respond_error (Session *session, int error)
672 {
673     g_return_if_fail (session != NULL);
674     g_return_if_fail (error != PAM_SUCCESS);
675
676     write_data (session, &error, sizeof (error));
677 }
678
679 int
680 session_get_messages_length (Session *session)
681 {
682     g_return_val_if_fail (session != NULL, 0);
683     return session->priv->messages_length;
684 }
685
686 const struct pam_message *
687 session_get_messages (Session *session)
688 {
689     g_return_val_if_fail (session != NULL, NULL);
690     return session->priv->messages;
691 }
692
693 gboolean
694 session_get_is_authenticated (Session *session)
695 {
696     g_return_val_if_fail (session != NULL, FALSE);
697     return session->priv->authentication_complete && session->priv->authentication_result == PAM_SUCCESS;
698 }
699
700 int
701 session_get_authentication_result (Session *session)
702 {
703     g_return_val_if_fail (session != NULL, 0);
704     return session->priv->authentication_result;
705 }
706
707 const gchar *
708 session_get_authentication_result_string (Session *session)
709 {
710     g_return_val_if_fail (session != NULL, NULL);
711     return session->priv->authentication_result_string;
712 }
713
714 void
715 session_run (Session *session)
716 {
717     g_return_if_fail (session->priv->display_server != NULL);
718     return SESSION_GET_CLASS (session)->run (session);
719 }
720
721 static void
722 session_real_run (Session *session)
723 {
724     gsize i, argc;
725     gchar *command, *x_authority_filename;
726     GList *link;
727
728     g_return_if_fail (session != NULL);
729     g_return_if_fail (!session->priv->command_run);
730     g_return_if_fail (session_get_is_authenticated (session));
731     g_return_if_fail (session->priv->argv != NULL);
732     g_return_if_fail (session->priv->pid != 0);
733
734     display_server_connect_session (session->priv->display_server, session);
735
736     session->priv->command_run = TRUE;
737
738     command = g_strjoinv (" ", session->priv->argv);
739     l_debug (session, "Running command %s", command);
740     g_free (command);
741
742     /* Create authority location */
743     if (session->priv->x_authority_use_system_location)
744     {
745         gchar *run_dir, *dir;
746
747         run_dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
748         dir = g_build_filename (run_dir, session->priv->username, NULL);
749         g_free (run_dir);
750
751         if (g_mkdir_with_parents (dir, S_IRWXU) < 0)
752             l_warning (session, "Failed to set create system authority dir %s: %s", dir, strerror (errno));
753         if (getuid () == 0)
754         {
755             if (chown (dir, user_get_uid (session_get_user (session)), user_get_gid (session_get_user (session))) < 0)
756                 l_warning (session, "Failed to set ownership of user authority dir: %s", strerror (errno));
757         }
758
759         x_authority_filename = g_build_filename (dir, "xauthority", NULL);
760         g_free (dir);
761     }
762     else
763         x_authority_filename = g_build_filename (user_get_home_directory (session_get_user (session)), ".Xauthority", NULL);
764
765     if (session->priv->log_filename)
766         l_debug (session, "Logging to %s", session->priv->log_filename);
767     write_string (session, session->priv->log_filename);
768     write_string (session, session->priv->tty);
769     write_string (session, x_authority_filename);
770     g_free (x_authority_filename);
771     write_string (session, session->priv->xdisplay);
772     write_xauth (session, session->priv->x_authority);
773     argc = g_list_length (session->priv->env);
774     write_data (session, &argc, sizeof (argc));
775     for (link = session->priv->env; link; link = link->next)
776         write_string (session, (gchar *) link->data);
777     argc = g_strv_length (session->priv->argv);
778     write_data (session, &argc, sizeof (argc));
779     for (i = 0; i < argc; i++)
780         write_string (session, session->priv->argv[i]);
781
782     if (login1_is_running ())
783         session->priv->login1_session = read_string_from_child (session);
784     if (!session->priv->login1_session)
785         session->priv->console_kit_cookie = read_string_from_child (session);
786 }
787
788 void
789 session_lock (Session *session)
790 {
791     g_return_if_fail (session != NULL);
792     if (getuid () == 0)
793     {
794         if (session->priv->login1_session)
795             login1_lock_session (session->priv->login1_session);
796         else if (session->priv->console_kit_cookie)
797             ck_lock_session (session->priv->console_kit_cookie);
798     }
799 }
800
801 void
802 session_unlock (Session *session)
803 {
804     g_return_if_fail (session != NULL);
805     if (getuid () == 0)
806     {
807         if (session->priv->login1_session)
808             login1_unlock_session (session->priv->login1_session);
809         else if (session->priv->console_kit_cookie)
810             ck_unlock_session (session->priv->console_kit_cookie);
811     }
812 }
813
814 void
815 session_stop (Session *session)
816 {
817     g_return_if_fail (session != NULL);
818
819     if (session->priv->stopping)
820         return;
821     session->priv->stopping = TRUE;
822
823     return SESSION_GET_CLASS (session)->stop (session);
824 }
825
826 static void
827 session_real_stop (Session *session)
828 {
829     g_return_if_fail (session != NULL);
830
831     if (session->priv->pid > 0)
832     {
833         l_debug (session, "Sending SIGTERM");
834         kill (session->priv->pid, SIGTERM);
835         // FIXME: Handle timeout
836     }
837     else
838         g_signal_emit (G_OBJECT (session), signals[STOPPED], 0);
839 }
840
841 gboolean
842 session_get_is_stopping (Session *session)
843 {
844     g_return_val_if_fail (session != NULL, FALSE);
845     return session->priv->stopping;
846 }
847
848 static void
849 session_init (Session *session)
850 {
851     session->priv = G_TYPE_INSTANCE_GET_PRIVATE (session, SESSION_TYPE, SessionPrivate);
852     session->priv->log_filename = g_strdup (".xsession-errors");
853 }
854
855 static void
856 session_finalize (GObject *object)
857 {
858     Session *self = SESSION (object);
859     int i;
860
861     g_free (self->priv->session_type);
862     if (self->priv->display_server)
863         g_object_unref (self->priv->display_server);
864     if (self->priv->pid)
865         kill (self->priv->pid, SIGKILL);
866     if (self->priv->from_child_channel)
867         g_io_channel_unref (self->priv->from_child_channel);
868     if (self->priv->from_child_watch)
869         g_source_remove (self->priv->from_child_watch);
870     if (self->priv->child_watch)
871         g_source_remove (self->priv->child_watch);
872     g_free (self->priv->username);
873     if (self->priv->user)
874         g_object_unref (self->priv->user);
875     g_free (self->priv->pam_service);
876     for (i = 0; i < self->priv->messages_length; i++)
877         g_free ((char *) self->priv->messages[i].msg);
878     g_free (self->priv->messages);
879     g_free (self->priv->authentication_result_string);
880     g_free (self->priv->log_filename);
881     g_free (self->priv->tty);
882     g_free (self->priv->xdisplay);
883     if (self->priv->x_authority)
884         g_object_unref (self->priv->x_authority);
885     g_free (self->priv->remote_host_name);
886     g_free (self->priv->login1_session);
887     g_free (self->priv->console_kit_cookie);
888     g_list_free_full (self->priv->env, g_free);
889     g_strfreev (self->priv->argv);
890
891     G_OBJECT_CLASS (session_parent_class)->finalize (object);
892 }
893
894 static void
895 session_class_init (SessionClass *klass)
896 {
897     GObjectClass *object_class = G_OBJECT_CLASS (klass);
898
899     klass->start = session_real_start;
900     klass->run = session_real_run;
901     klass->stop = session_real_stop;
902     object_class->finalize = session_finalize;
903
904     g_type_class_add_private (klass, sizeof (SessionPrivate));
905
906     signals[GOT_MESSAGES] =
907         g_signal_new ("got-messages",
908                       G_TYPE_FROM_CLASS (klass),
909                       G_SIGNAL_RUN_LAST,
910                       G_STRUCT_OFFSET (SessionClass, got_messages),
911                       NULL, NULL,
912                       NULL,
913                       G_TYPE_NONE, 0);
914
915     signals[AUTHENTICATION_COMPLETE] =
916         g_signal_new ("authentication-complete",
917                       G_TYPE_FROM_CLASS (klass),
918                       G_SIGNAL_RUN_LAST,
919                       G_STRUCT_OFFSET (SessionClass, authentication_complete),
920                       NULL, NULL,
921                       NULL,
922                       G_TYPE_NONE, 0);
923
924     signals[STOPPED] =
925         g_signal_new ("stopped",
926                       G_TYPE_FROM_CLASS (klass),
927                       G_SIGNAL_RUN_LAST,
928                       G_STRUCT_OFFSET (SessionClass, stopped),
929                       NULL, NULL,
930                       NULL,
931                       G_TYPE_NONE, 0);
932 }
933
934 static gint
935 session_real_logprefix (Logger *self, gchar *buf, gulong buflen)
936 {
937     Session *session = SESSION (self);
938     if (session->priv->pid != 0)
939         return g_snprintf (buf, buflen, "Session pid=%d: ", session->priv->pid);
940     else
941         return g_snprintf (buf, buflen, "Session: ");
942 }
943
944 static void
945 session_logger_iface_init (LoggerInterface *iface)
946 {
947     iface->logprefix = &session_real_logprefix;
948 }