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