4 * Copyright (c) 2022 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
22 #include <systemd/sd-bus.h>
23 #include <systemd/sd-daemon.h>
24 #include <systemd/sd-login.h>
25 #include <systemd/sd-id128.h>
33 #ifndef SD_ID128_UUID_FORMAT_STR
34 #define SD_ID128_UUID_FORMAT_STR \
35 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
39 systemd_activated(void)
41 return (sd_listen_fds(0) >= 1);
45 systemd_create_socket(int flags
, char **cause
)
49 struct sockaddr_un sa
;
50 socklen_t addrlen
= sizeof sa
;
52 fds
= sd_listen_fds(0);
53 if (fds
> 1) { /* too many file descriptors */
58 if (fds
== 1) { /* socket-activated */
59 fd
= SD_LISTEN_FDS_START
;
60 if (!sd_is_socket_unix(fd
, SOCK_STREAM
, 1, NULL
, 0)) {
64 if (getsockname(fd
, (struct sockaddr
*)&sa
, &addrlen
) == -1)
66 socket_path
= xstrdup(sa
.sun_path
);
70 return (server_create_socket(flags
, cause
));
74 xasprintf(cause
, "systemd socket error (%s)", strerror(errno
));
79 systemd_move_pid_to_new_cgroup(pid_t pid
, char **cause
)
81 sd_bus_error error
= SD_BUS_ERROR_NULL
;
82 sd_bus_message
*m
= NULL
, *reply
= NULL
;
84 char *name
, *desc
, *slice
;
89 /* Connect to the session bus. */
90 r
= sd_bus_default_user(&bus
);
92 xasprintf(cause
, "failed to connect to session bus: %s",
97 /* Start building the method call. */
98 r
= sd_bus_message_new_method_call(bus
, &m
,
99 "org.freedesktop.systemd1",
100 "/org/freedesktop/systemd1",
101 "org.freedesktop.systemd1.Manager",
102 "StartTransientUnit");
104 xasprintf(cause
, "failed to create bus message: %s",
109 /* Generate a unique name for the new scope, to avoid collisions. */
110 r
= sd_id128_randomize(&uuid
);
112 xasprintf(cause
, "failed to generate uuid: %s", strerror(-r
));
115 xasprintf(&name
, "tmux-spawn-" SD_ID128_UUID_FORMAT_STR
".scope",
116 SD_ID128_FORMAT_VAL(uuid
));
117 r
= sd_bus_message_append(m
, "s", name
);
120 xasprintf(cause
, "failed to append to bus message: %s",
125 /* Mode: fail if there's a queued unit with the same name. */
126 r
= sd_bus_message_append(m
, "s", "fail");
128 xasprintf(cause
, "failed to append to bus message: %s",
133 /* Start properties array. */
134 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
136 xasprintf(cause
, "failed to start properties array: %s",
141 parent_pid
= getpid();
142 xasprintf(&desc
, "tmux child pane %ld launched by process %ld",
143 (long)pid
, (long)parent_pid
);
144 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", desc
);
147 xasprintf(cause
, "failed to append to properties: %s",
153 * Make sure that the session shells are terminated with SIGHUP since
154 * bash and friends tend to ignore SIGTERM.
156 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", 1);
158 xasprintf(cause
, "failed to append to properties: %s",
164 * Inherit the slice from the parent process, or default to
165 * "app-tmux.slice" if that fails.
167 r
= sd_pid_get_user_slice(parent_pid
, &slice
);
169 slice
= xstrdup("app-tmux.slice");
171 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
174 xasprintf(cause
, "failed to append to properties: %s",
179 /* PIDs to add to the scope: length - 1 array of uint32_t. */
180 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
182 xasprintf(cause
, "failed to append to properties: %s",
187 /* Clean up the scope even if it fails. */
188 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s",
189 "inactive-or-failed");
191 xasprintf(cause
, "failed to append to properties: %s",
196 /* End properties array. */
197 r
= sd_bus_message_close_container(m
);
199 xasprintf(cause
, "failed to end properties array: %s",
204 /* aux is currently unused and should be passed an empty array. */
205 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
207 xasprintf(cause
, "failed to append to bus message: %s",
212 /* Call the method with a timeout of 1 second = 1e6 us. */
213 r
= sd_bus_call(bus
, m
, 1000000, &error
, &reply
);
215 if (error
.message
!= NULL
) {
216 /* We have a specific error message from sd-bus. */
217 xasprintf(cause
, "StartTransientUnit call failed: %s",
220 xasprintf(cause
, "StartTransientUnit call failed: %s",
227 sd_bus_error_free(&error
);
228 sd_bus_message_unref(m
);
229 sd_bus_message_unref(reply
);