]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/sd_pid_get_session.xml
test-execute: Add tests for new PassEnvironment= directive
[thirdparty/systemd.git] / man / sd_pid_get_session.xml
CommitLineData
707b66c6 1<?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
0b3b020a 2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
12b42c76 3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
0b3b020a
LP
4
5<!--
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
0b3b020a
LP
13 (at your option) any later version.
14
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
5430f7f2 18 Lesser General Public License for more details.
0b3b020a 19
5430f7f2 20 You should have received a copy of the GNU Lesser General Public License
0b3b020a
LP
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22-->
23
56ba3c78 24<refentry id="sd_pid_get_session" conditional='HAVE_PAM'>
0b3b020a 25
798d3a52
ZJS
26 <refentryinfo>
27 <title>sd_pid_get_session</title>
28 <productname>systemd</productname>
29
30 <authorgroup>
31 <author>
32 <contrib>Developer</contrib>
33 <firstname>Lennart</firstname>
34 <surname>Poettering</surname>
35 <email>lennart@poettering.net</email>
36 </author>
37 </authorgroup>
38 </refentryinfo>
39
40 <refmeta>
41 <refentrytitle>sd_pid_get_session</refentrytitle>
42 <manvolnum>3</manvolnum>
43 </refmeta>
44
45 <refnamediv>
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>
dddbc695 52 <refname>sd_pid_get_user_slice</refname>
f5aaf575 53 <refname>sd_pid_get_cgroup</refname>
798d3a52
ZJS
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>
dddbc695 60 <refname>sd_peer_get_user_slice</refname>
f5aaf575 61 <refname>sd_peer_get_cgroup</refname>
dddbc695
LP
62 <refpurpose>Determine session, unit, owner of a session,
63 container/VM or slice of a specific PID or socket
64 peer</refpurpose>
798d3a52
ZJS
65 </refnamediv>
66
67 <refsynopsisdiv>
68 <funcsynopsis>
69 <funcsynopsisinfo>#include &lt;systemd/sd-login.h&gt;</funcsynopsisinfo>
70
71 <funcprototype>
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>
75 </funcprototype>
76
77 <funcprototype>
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>
81 </funcprototype>
82
83 <funcprototype>
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>
87 </funcprototype>
88
89 <funcprototype>
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>
93 </funcprototype>
94
95 <funcprototype>
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>
99 </funcprototype>
100
101 <funcprototype>
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>
105 </funcprototype>
106
dddbc695
LP
107 <funcprototype>
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>
111 </funcprototype>
112
f5aaf575
LP
113 <funcprototype>
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>
117 </funcprototype>
118
798d3a52
ZJS
119 <funcprototype>
120 <funcdef>int <function>sd_peer_get_session</function></funcdef>
121 <paramdef>int <parameter>fd</parameter></paramdef>
122 <paramdef>char **<parameter>session</parameter></paramdef>
123 </funcprototype>
124
125 <funcprototype>
126 <funcdef>int <function>sd_peer_get_unit</function></funcdef>
127 <paramdef>int <parameter>fd</parameter></paramdef>
128 <paramdef>char **<parameter>unit</parameter></paramdef>
129 </funcprototype>
130
131 <funcprototype>
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>
135 </funcprototype>
136
137 <funcprototype>
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>
141 </funcprototype>
142
143 <funcprototype>
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>
147 </funcprototype>
148
149 <funcprototype>
150 <funcdef>int <function>sd_peer_get_slice</function></funcdef>
151 <paramdef>int <parameter>fd</parameter></paramdef>
152 <paramdef>char **<parameter>slice</parameter></paramdef>
153 </funcprototype>
dddbc695
LP
154
155 <funcprototype>
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>
159 </funcprototype>
f5aaf575
LP
160
161 <funcprototype>
162 <funcdef>int <function>sd_peer_get_cgroup</function></funcdef>
163 <paramdef>int <parameter>fd</parameter></paramdef>
164 <paramdef>char **<parameter>cgroup</parameter></paramdef>
165 </funcprototype>
798d3a52
ZJS
166 </funcsynopsis>
167 </refsynopsisdiv>
168
169 <refsect1>
170 <title>Description</title>
171
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
b938cb90 179 being part of a login session, this function will fail with
707b66c6 180 -ENODATA. The returned string needs to be freed with the libc
882f407f
LP
181 <citerefentry
182 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
798d3a52
ZJS
183 call after use.</para>
184
185 <para><function>sd_pid_get_unit()</function> may be used to
dddbc695
LP
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
b938cb90
JE
191 processes not being part of a systemd system unit, this function
192 will fail with -ENODATA. (More specifically, this call will not
707b66c6
LP
193 work for kernel threads.) The returned string needs to be freed
194 with the libc <citerefentry
882f407f 195 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
798d3a52
ZJS
196 call after use.</para>
197
198 <para><function>sd_pid_get_user_unit()</function> may be used to
dddbc695
LP
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
b938cb90 201 similar to <function>sd_pid_get_unit()</function>, but applies to
dddbc695 202 user units instead of system units.</para>
798d3a52
ZJS
203
204 <para><function>sd_pid_get_owner_uid()</function> may be used to
dddbc695
LP
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
a8eaaee7 208 multiple login sessions of the same user, whereas
798d3a52
ZJS
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
b938cb90 211 of a user, this function will fail with -ENODATA.</para>
798d3a52
ZJS
212
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
882f407f
LP
217 <citerefentry
218 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
b938cb90 219 call after use. For processes not part of a VM or containers, this
707b66c6 220 function fails with -ENODATA.</para>
798d3a52
ZJS
221
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
226 with the libc
227 <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
228 call after use.</para>
229
a8eaaee7 230 <para>Similarly, <function>sd_pid_get_user_slice()</function>
dddbc695
LP
231 returns the user slice (as managed by the user's systemd instance)
232 of a process.</para>
233
f5aaf575
LP
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
b938cb90 238 returned. To find the actual control group path in the file system,
f5aaf575
LP
239 the returned path needs to be prefixed with
240 <filename>/sys/fs/cgroup/</filename> (if the unified control group
241 setup is used), or
242 <filename>/sys/fs/cgroup/<replaceable>HIERARCHY</replaceable>/</filename>
243 (if the legacy multi-hierarchy control group setup is used).</para>
244
798d3a52
ZJS
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>
248
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>,
dddbc695 253 <function>sd_peer_get_machine_name()</function>,
f5aaf575
LP
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>
798d3a52
ZJS
262 </refsect1>
263
264 <refsect1>
265 <title>Return Value</title>
266
267 <para>On success, these calls return 0 or a positive integer. On
268 failure, these calls return a negative errno-style error
269 code.</para>
270 </refsect1>
271
882f407f
LP
272 <refsect1>
273 <title>Errors</title>
274
275 <para>Returned errors may indicate the following problems:</para>
276
277 <variablelist>
278
279 <varlistentry>
707b66c6
LP
280 <term><constant>-ESRCH</constant></term>
281
282 <listitem><para>The specified PID does not refer to a running
283 process.</para>
284 </listitem>
285 </varlistentry>
286
287 <varlistentry>
288 <term><constant>-BADF</constant></term>
289
290 <listitem><para>The specified socket file descriptor was
291 invalid.</para></listitem>
292 </varlistentry>
293
294 <varlistentry>
295 <term><constant>-ENODATA</constant></term>
882f407f 296
a8eaaee7 297 <listitem><para>The given field is not specified for the described
882f407f
LP
298 process or peer.</para>
299 </listitem>
300 </varlistentry>
301
302 <varlistentry>
707b66c6 303 <term><constant>-EINVAL</constant></term>
882f407f 304
707b66c6 305 <listitem><para>An input parameter was invalid (out of range,
7ca41557 306 or NULL, where that is not accepted).</para></listitem>
882f407f
LP
307 </varlistentry>
308
309 <varlistentry>
310 <term><constant>-ENOMEM</constant></term>
311
312 <listitem><para>Memory allocation failed.</para></listitem>
313 </varlistentry>
314 </variablelist>
315 </refsect1>
316
798d3a52
ZJS
317 <refsect1>
318 <title>Notes</title>
319
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>,
dddbc695 326 <function>sd_pid_get_user_slice()</function>,
798d3a52
ZJS
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>,
dddbc695
LP
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>libsystemd</constant> <citerefentry
336 project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
798d3a52
ZJS
337 file.</para>
338
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>
344 </refsect1>
345
346 <refsect1>
347 <title>See Also</title>
348
349 <para>
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>
356 </para>
357 </refsect1>
0b3b020a
LP
358
359</refentry>