chiark / gitweb /
logind: introduce session "positions"
[elogind.git] / src / login / multi-seat-x.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "util.h"
26 #include "mkdir.h"
27
28 int main(int argc, char *argv[]) {
29
30         int i;
31         const char *seat = NULL;
32         char **new_argv;
33         _cleanup_free_ char *path = NULL;
34         int r;
35         _cleanup_fclose_ FILE *f = NULL;
36
37         /* This binary will go away as soon as X natively takes the
38          * arguments in question as command line parameters, instead
39          * of requiring them in the configuration file. */
40
41         /* If this file is removed, don't forget to remove the code
42          * that invokes this in gdm and other display managers. */
43
44         for (i = 1; i < argc; i++)
45                 if (streq(argv[i], "-seat"))
46                         seat = argv[i+1];
47
48         if (isempty(seat) || streq(seat, "seat0")) {
49                 argv[0] = (char*) X_SERVER;
50                 execv(X_SERVER, argv);
51                 log_error("Failed to execute real X server: %m");
52                 goto fail;
53         }
54
55         r = mkdir_safe_label("/run/systemd/multi-session-x", 0755, 0, 0);
56         if (r < 0) {
57                 log_error("Failed to create directory: %s", strerror(-r));
58                 goto fail;
59         }
60
61         path = strappend("/run/systemd/multi-session-x/", seat);
62         if (!path) {
63                 log_oom();
64                 goto fail;
65         }
66
67         f = fopen(path, "we");
68         if (!f) {
69                 log_error("Failed to write configuration file: %m");
70                 goto fail;
71         }
72
73         fprintf(f,
74                 "Section \"ServerFlags\"\n"
75                 "        Option \"AutoAddDevices\" \"True\"\n"
76                 "        Option \"AllowEmptyInput\" \"True\"\n"
77                 "        Option \"DontVTSwitch\" \"True\"\n"
78                 "EndSection\n"
79                 "Section \"InputClass\"\n"
80                 "        Identifier \"Force Input Devices to Seat\"\n"
81                 "        Option \"GrabDevice\" \"True\"\n"
82                 "EndSection\n");
83
84         fflush(f);
85
86         if (ferror(f)) {
87                 log_error("Failed to write configuration file: %m");
88                 goto fail;
89         }
90
91         fclose(f);
92         f = NULL;
93
94         new_argv = newa(char*, argc + 3 + 1);
95         memcpy(new_argv, argv, sizeof(char*) * (argc + 2 + 1));
96
97         new_argv[0] = (char*) X_SERVER;
98         new_argv[argc+0] = (char*) "-config";
99         new_argv[argc+1] = path;
100         new_argv[argc+2] = (char*) "-sharevts";
101         new_argv[argc+3] = NULL;
102
103         execv(X_SERVER, new_argv);
104         log_error("Failed to execute real X server: %m");
105
106 fail:
107         return EXIT_FAILURE;
108 }