1 <?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 <refentry id="sd_pid_get_session" conditional='HAVE_PAM'>
27 <title>sd_pid_get_session</title>
28 <productname>systemd</productname>
32 <contrib>Developer</contrib>
33 <firstname>Lennart</firstname>
34 <surname>Poettering</surname>
35 <email>lennart@poettering.net</email>
41 <refentrytitle>sd_pid_get_session</refentrytitle>
42 <manvolnum>3</manvolnum>
46 <refname>sd_pid_get_session</refname>
47 <refname>sd_pid_get_unit</refname>
48 <refname>sd_pid_get_user_unit</refname>
49 <refname>sd_pid_get_owner_uid</refname>
50 <refname>sd_pid_get_machine_name</refname>
51 <refname>sd_pid_get_slice</refname>
52 <refname>sd_pid_get_user_slice</refname>
53 <refname>sd_pid_get_cgroup</refname>
54 <refname>sd_peer_get_session</refname>
55 <refname>sd_peer_get_unit</refname>
56 <refname>sd_peer_get_user_unit</refname>
57 <refname>sd_peer_get_owner_uid</refname>
58 <refname>sd_peer_get_machine_name</refname>
59 <refname>sd_peer_get_slice</refname>
60 <refname>sd_peer_get_user_slice</refname>
61 <refname>sd_peer_get_cgroup</refname>
62 <refpurpose>Determine session, unit, owner of a session,
63 container/VM or slice of a specific PID or socket
69 <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
72 <funcdef>int <function>sd_pid_get_session</function></funcdef>
73 <paramdef>pid_t <parameter>pid</parameter></paramdef>
74 <paramdef>char **<parameter>session</parameter></paramdef>
78 <funcdef>int <function>sd_pid_get_unit</function></funcdef>
79 <paramdef>pid_t <parameter>pid</parameter></paramdef>
80 <paramdef>char **<parameter>unit</parameter></paramdef>
84 <funcdef>int <function>sd_pid_get_user_unit</function></funcdef>
85 <paramdef>pid_t <parameter>pid</parameter></paramdef>
86 <paramdef>char **<parameter>unit</parameter></paramdef>
90 <funcdef>int <function>sd_pid_get_owner_uid</function></funcdef>
91 <paramdef>pid_t <parameter>pid</parameter></paramdef>
92 <paramdef>uid_t *<parameter>uid</parameter></paramdef>
96 <funcdef>int <function>sd_pid_get_machine_name</function></funcdef>
97 <paramdef>pid_t <parameter>pid</parameter></paramdef>
98 <paramdef>char **<parameter>name</parameter></paramdef>
102 <funcdef>int <function>sd_pid_get_slice</function></funcdef>
103 <paramdef>pid_t <parameter>pid</parameter></paramdef>
104 <paramdef>char **<parameter>slice</parameter></paramdef>
108 <funcdef>int <function>sd_pid_get_user_slice</function></funcdef>
109 <paramdef>pid_t <parameter>pid</parameter></paramdef>
110 <paramdef>char **<parameter>slice</parameter></paramdef>
114 <funcdef>int <function>sd_pid_get_cgroup</function></funcdef>
115 <paramdef>pid_t <parameter>pid</parameter></paramdef>
116 <paramdef>char **<parameter>cgroup</parameter></paramdef>
120 <funcdef>int <function>sd_peer_get_session</function></funcdef>
121 <paramdef>int <parameter>fd</parameter></paramdef>
122 <paramdef>char **<parameter>session</parameter></paramdef>
126 <funcdef>int <function>sd_peer_get_unit</function></funcdef>
127 <paramdef>int <parameter>fd</parameter></paramdef>
128 <paramdef>char **<parameter>unit</parameter></paramdef>
132 <funcdef>int <function>sd_peer_get_user_unit</function></funcdef>
133 <paramdef>int <parameter>fd</parameter></paramdef>
134 <paramdef>char **<parameter>unit</parameter></paramdef>
138 <funcdef>int <function>sd_peer_get_owner_uid</function></funcdef>
139 <paramdef>int <parameter>fd</parameter></paramdef>
140 <paramdef>uid_t *<parameter>uid</parameter></paramdef>
144 <funcdef>int <function>sd_peer_get_machine_name</function></funcdef>
145 <paramdef>int <parameter>fd</parameter></paramdef>
146 <paramdef>char **<parameter>name</parameter></paramdef>
150 <funcdef>int <function>sd_peer_get_slice</function></funcdef>
151 <paramdef>int <parameter>fd</parameter></paramdef>
152 <paramdef>char **<parameter>slice</parameter></paramdef>
156 <funcdef>int <function>sd_peer_get_user_slice</function></funcdef>
157 <paramdef>int <parameter>fd</parameter></paramdef>
158 <paramdef>char **<parameter>slice</parameter></paramdef>
162 <funcdef>int <function>sd_peer_get_cgroup</function></funcdef>
163 <paramdef>int <parameter>fd</parameter></paramdef>
164 <paramdef>char **<parameter>cgroup</parameter></paramdef>
170 <title>Description</title>
172 <para><function>sd_pid_get_session()</function> may be used to
173 determine the login session identifier of a process identified by
174 the specified process identifier. The session identifier is a
175 short string, suitable for usage in file system paths. Note that
176 not all processes are part of a login session (e.g. system service
177 processes, user processes that are shared between multiple
178 sessions of the same user, or kernel threads). For processes not
179 being part of a login session, this function will fail with
180 <constant>-ENODATA</constant>. The returned string needs to be freed with the libc
182 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
183 call after use.</para>
185 <para><function>sd_pid_get_unit()</function> may be used to
186 determine the systemd system unit (i.e. system service or scope
187 unit) identifier of a process identified by the specified PID. The
188 unit name is a short string, suitable for usage in file system
189 paths. Note that not all processes are part of a system
190 unit/service (e.g. user processes, or kernel threads). For
191 processes not being part of a systemd system unit, this function
192 will fail with <constant>-ENODATA</constant>. (More specifically, this call will not
193 work for kernel threads.) The returned string needs to be freed
194 with the libc <citerefentry
195 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
196 call after use.</para>
198 <para><function>sd_pid_get_user_unit()</function> may be used to
199 determine the systemd user unit (i.e. user service or scope unit)
200 identifier of a process identified by the specified PID. This is
201 similar to <function>sd_pid_get_unit()</function>, but applies to
202 user units instead of system units.</para>
204 <para><function>sd_pid_get_owner_uid()</function> may be used to
205 determine the Unix UID (user identifier) of the owner of the
206 session of a process identified the specified PID. Note that this
207 function will succeed for user processes which are shared between
208 multiple login sessions of the same user, whereas
209 <function>sd_pid_get_session()</function> will fail. For processes
210 not being part of a login session and not being a shared process
211 of a user, this function will fail with <constant>-ENODATA</constant>.</para>
213 <para><function>sd_pid_get_machine_name()</function> may be used
214 to determine the name of the VM or container is a member of. The
215 machine name is a short string, suitable for usage in file system
216 paths. The returned string needs to be freed with the libc
218 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
219 call after use. For processes not part of a VM or containers, this
220 function fails with <constant>-ENODATA</constant>.</para>
222 <para><function>sd_pid_get_slice()</function> may be used to
223 determine the slice unit the process is a member of. See
224 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>
225 for details about slices. The returned string needs to be freed
227 <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
228 call after use.</para>
230 <para>Similarly, <function>sd_pid_get_user_slice()</function>
231 returns the user slice (as managed by the user's systemd instance)
234 <para><function>sd_pid_get_cgroup()</function> returns the control
235 group path of the specified process, relative to the root of the
236 hierarchy. Returns the path without trailing slash, except for
237 processes located in the root control group, where "/" is
238 returned. To find the actual control group path in the file system,
239 the returned path needs to be prefixed with
240 <filename>/sys/fs/cgroup/</filename> (if the unified control group
242 <filename>/sys/fs/cgroup/<replaceable>HIERARCHY</replaceable>/</filename>
243 (if the legacy multi-hierarchy control group setup is used).</para>
245 <para>If the <varname>pid</varname> parameter of any of these
246 functions is passed as 0, the operation is executed for the
247 calling process.</para>
249 <para>The <function>sd_peer_get_session()</function>,
250 <function>sd_peer_get_unit()</function>,
251 <function>sd_peer_get_user_unit()</function>,
252 <function>sd_peer_get_owner_uid()</function>,
253 <function>sd_peer_get_machine_name()</function>,
254 <function>sd_peer_get_slice()</function>,
255 <function>sd_peer_get_user_slice()</function> and
256 <function>sd_peer_get_cgroup()</function> calls operate similar to
257 their PID counterparts, but operate on a connected AF_UNIX socket
258 and retrieve information about the connected peer process. Note
259 that these fields are retrieved via <filename>/proc</filename>,
260 and hence are not suitable for authorization purposes, as they are
261 subject to races.</para>
265 <title>Return Value</title>
267 <para>On success, these calls return 0 or a positive integer. On
268 failure, these calls return a negative errno-style error
273 <title>Errors</title>
275 <para>Returned errors may indicate the following problems:</para>
280 <term><constant>-ESRCH</constant></term>
282 <listitem><para>The specified PID does not refer to a running
288 <term><constant>-EBADF</constant></term>
290 <listitem><para>The specified socket file descriptor was
291 invalid.</para></listitem>
295 <term><constant>-ENODATA</constant></term>
297 <listitem><para>The given field is not specified for the described
298 process or peer.</para>
303 <term><constant>-EINVAL</constant></term>
305 <listitem><para>An input parameter was invalid (out of range,
306 or NULL, where that is not accepted).</para></listitem>
310 <term><constant>-ENOMEM</constant></term>
312 <listitem><para>Memory allocation failed.</para></listitem>
320 <para>The <function>sd_pid_get_session()</function>,
321 <function>sd_pid_get_unit()</function>,
322 <function>sd_pid_get_user_unit()</function>,
323 <function>sd_pid_get_owner_uid()</function>,
324 <function>sd_pid_get_machine_name()</function>,
325 <function>sd_pid_get_slice()</function>,
326 <function>sd_pid_get_user_slice()</function>,
327 <function>sd_peer_get_session()</function>,
328 <function>sd_peer_get_unit()</function>,
329 <function>sd_peer_get_user_unit()</function>,
330 <function>sd_peer_get_owner_uid()</function>,
331 <function>sd_peer_get_machine_name()</function>,
332 <function>sd_peer_get_slice()</function> and
333 <function>sd_peer_get_user_slice()</function> interfaces are
334 available as a shared library, which can be compiled and linked to
335 with the <constant>libelogind</constant> <citerefentry
336 project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
339 <para>Note that the login session identifier as
340 returned by <function>sd_pid_get_session()</function>
341 is completely unrelated to the process session
342 identifier as returned by
343 <citerefentry><refentrytitle>getsid</refentrytitle><manvolnum>2</manvolnum></citerefentry>.</para>
347 <title>See Also</title>
350 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
351 <citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
352 <citerefentry><refentrytitle>sd_session_is_active</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
353 <citerefentry><refentrytitle>getsid</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
354 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
355 <citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>