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 SPDX-License-Identifier: LGPL-2.1+
8 Copyright 2010 Lennart Poettering
11 <refentry id="sd_pid_get_owner_uid" conditional='HAVE_PAM'
12 xmlns:xi="http://www.w3.org/2001/XInclude">
15 <title>sd_pid_get_owner_uid</title>
16 <productname>systemd</productname>
20 <contrib>Developer</contrib>
21 <firstname>Lennart</firstname>
22 <surname>Poettering</surname>
23 <email>lennart@poettering.net</email>
29 <refentrytitle>sd_pid_get_owner_uid</refentrytitle>
30 <manvolnum>3</manvolnum>
34 <refname>sd_pid_get_owner_uid</refname>
35 <refname>sd_pid_get_session</refname>
36 <refname>sd_pid_get_user_unit</refname>
37 <refname>sd_pid_get_unit</refname>
38 <refname>sd_pid_get_machine_name</refname>
39 <refname>sd_pid_get_slice</refname>
40 <refname>sd_pid_get_user_slice</refname>
41 <refname>sd_pid_get_cgroup</refname>
42 <refname>sd_peer_get_owner_uid</refname>
43 <refname>sd_peer_get_session</refname>
44 <refname>sd_peer_get_user_unit</refname>
45 <refname>sd_peer_get_unit</refname>
46 <refname>sd_peer_get_machine_name</refname>
47 <refname>sd_peer_get_slice</refname>
48 <refname>sd_peer_get_user_slice</refname>
49 <refname>sd_peer_get_cgroup</refname>
50 <refpurpose>Determine the owner uid of the user unit or session,
51 or the session, user unit, system unit, container/VM or slice that
52 a specific PID or socket peer belongs to.</refpurpose>
57 <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
60 <funcdef>int <function>sd_pid_get_owner_uid</function></funcdef>
61 <paramdef>pid_t <parameter>pid</parameter></paramdef>
62 <paramdef>uid_t *<parameter>uid</parameter></paramdef>
66 <funcdef>int <function>sd_pid_get_session</function></funcdef>
67 <paramdef>pid_t <parameter>pid</parameter></paramdef>
68 <paramdef>char **<parameter>session</parameter></paramdef>
72 <funcdef>int <function>sd_pid_get_user_unit</function></funcdef>
73 <paramdef>pid_t <parameter>pid</parameter></paramdef>
74 <paramdef>char **<parameter>unit</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_machine_name</function></funcdef>
85 <paramdef>pid_t <parameter>pid</parameter></paramdef>
86 <paramdef>char **<parameter>name</parameter></paramdef>
90 <funcdef>int <function>sd_pid_get_slice</function></funcdef>
91 <paramdef>pid_t <parameter>pid</parameter></paramdef>
92 <paramdef>char **<parameter>slice</parameter></paramdef>
96 <funcdef>int <function>sd_pid_get_user_slice</function></funcdef>
97 <paramdef>pid_t <parameter>pid</parameter></paramdef>
98 <paramdef>char **<parameter>slice</parameter></paramdef>
102 <funcdef>int <function>sd_pid_get_cgroup</function></funcdef>
103 <paramdef>pid_t <parameter>pid</parameter></paramdef>
104 <paramdef>char **<parameter>cgroup</parameter></paramdef>
108 <funcdef>int <function>sd_peer_get_owner_uid</function></funcdef>
109 <paramdef>int <parameter>fd</parameter></paramdef>
110 <paramdef>uid_t *<parameter>uid</parameter></paramdef>
114 <funcdef>int <function>sd_peer_get_session</function></funcdef>
115 <paramdef>int <parameter>fd</parameter></paramdef>
116 <paramdef>char **<parameter>session</parameter></paramdef>
120 <funcdef>int <function>sd_peer_get_user_unit</function></funcdef>
121 <paramdef>int <parameter>fd</parameter></paramdef>
122 <paramdef>char **<parameter>unit</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_machine_name</function></funcdef>
133 <paramdef>int <parameter>fd</parameter></paramdef>
134 <paramdef>char **<parameter>name</parameter></paramdef>
138 <funcdef>int <function>sd_peer_get_slice</function></funcdef>
139 <paramdef>int <parameter>fd</parameter></paramdef>
140 <paramdef>char **<parameter>slice</parameter></paramdef>
144 <funcdef>int <function>sd_peer_get_user_slice</function></funcdef>
145 <paramdef>int <parameter>fd</parameter></paramdef>
146 <paramdef>char **<parameter>slice</parameter></paramdef>
150 <funcdef>int <function>sd_peer_get_cgroup</function></funcdef>
151 <paramdef>int <parameter>fd</parameter></paramdef>
152 <paramdef>char **<parameter>cgroup</parameter></paramdef>
158 <title>Description</title>
160 <para><function>sd_pid_get_owner_uid()</function> may be used to
161 determine the Unix UID (user identifier) which owns the login
162 session or systemd user unit of a process identified by the
163 specified PID. For processes which are not part of a login session
164 and not managed by a user manager, this function will fail with
165 <constant>-ENODATA</constant>.</para>
167 <para><function>sd_pid_get_session()</function> may be used to
168 determine the login session identifier of a process identified by
169 the specified process identifier. The session identifier is a
170 short string, suitable for usage in file system paths. Please
171 note the login session may be limited to a stub process or two.
172 User processes may instead be started from their systemd user
173 manager, e.g. GUI applications started using DBus activation, as
174 well as service processes which are shared between multiple logins
175 of the same user. For processes which are not part of a login
176 session, this function will fail with <constant>-ENODATA</constant>.
177 The returned string needs to be freed with the libc <citerefentry
178 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
179 call after use.</para>
181 <para><function>sd_pid_get_user_unit()</function> may be used to
182 determine the systemd user unit (i.e. user service or scope unit)
183 identifier of a process identified by the specified PID. The
184 unit name is a short string, suitable for usage in file system
185 paths. For processes which are not managed by a user manager, this
186 function will fail with <constant>-ENODATA</constant>. The
187 returned string needs to be freed with the libc <citerefentry
188 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
189 call after use.</para>
191 <para><function>sd_pid_get_unit()</function> may be used to
192 determine the systemd system unit (i.e. system service or scope
193 unit) identifier of a process identified by the specified PID. The
194 unit name is a short string, suitable for usage in file system
195 paths. Note that not all processes are part of a system
196 unit/service. For processes not being part of a systemd system
197 unit, this function will fail with <constant>-ENODATA</constant>.
198 (More specifically, this call will not work for kernel threads.)
199 The returned string needs to be freed with the libc <citerefentry
200 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
201 call after use.</para>
203 <para><function>sd_pid_get_machine_name()</function> may be used
204 to determine the name of the VM or container is a member of. The
205 machine name is a short string, suitable for usage in file system
206 paths. The returned string needs to be freed with the libc
208 project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
209 call after use. For processes not part of a VM or container, this
210 function fails with <constant>-ENODATA</constant>.</para>
212 <para><function>sd_pid_get_slice()</function> may be used to
213 determine the slice unit the process is a member of. See
214 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>
215 for details about slices. The returned string needs to be freed
217 <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
218 call after use.</para>
220 <para>Similarly, <function>sd_pid_get_user_slice()</function>
221 returns the user slice (as managed by the user's systemd instance)
224 <para><function>sd_pid_get_cgroup()</function> returns the control
225 group path of the specified process, relative to the root of the
226 hierarchy. Returns the path without trailing slash, except for
227 processes located in the root control group, where "/" is
228 returned. To find the actual control group path in the file system,
229 the returned path needs to be prefixed with
230 <filename>/sys/fs/cgroup/</filename> (if the unified control group
232 <filename>/sys/fs/cgroup/<replaceable>HIERARCHY</replaceable>/</filename>
233 (if the legacy multi-hierarchy control group setup is used).</para>
235 <para>If the <varname>pid</varname> parameter of any of these
236 functions is passed as 0, the operation is executed for the
237 calling process.</para>
239 <para>The <function>sd_peer_get_owner_uid()</function>,
240 <function>sd_peer_get_session()</function>,
241 <function>sd_peer_get_user_unit()</function>,
242 <function>sd_peer_get_unit()</function>,
243 <function>sd_peer_get_machine_name()</function>,
244 <function>sd_peer_get_slice()</function>,
245 <function>sd_peer_get_user_slice()</function> and
246 <function>sd_peer_get_cgroup()</function> calls operate similar to
247 their PID counterparts, but operate on a connected AF_UNIX socket
248 and retrieve information about the connected peer process. Note
249 that these fields are retrieved via <filename>/proc</filename>,
250 and hence are not suitable for authorization purposes, as they are
251 subject to races.</para>
255 <title>Return Value</title>
257 <para>On success, these calls return 0 or a positive integer. On
258 failure, these calls return a negative errno-style error
263 <title>Errors</title>
265 <para>Returned errors may indicate the following problems:</para>
270 <term><constant>-ESRCH</constant></term>
272 <listitem><para>The specified PID does not refer to a running
278 <term><constant>-EBADF</constant></term>
280 <listitem><para>The specified socket file descriptor was
281 invalid.</para></listitem>
285 <term><constant>-ENODATA</constant></term>
287 <listitem><para>The given field is not specified for the described
288 process or peer.</para>
293 <term><constant>-EINVAL</constant></term>
295 <listitem><para>An input parameter was invalid (out of range,
296 or NULL, where that is not accepted).</para></listitem>
300 <term><constant>-ENOMEM</constant></term>
302 <listitem><para>Memory allocation failed.</para></listitem>
310 <xi:include href="libelogind-pkgconfig.xml" xpointer="pkgconfig-text"/>
312 <para>Note that the login session identifier as
313 returned by <function>sd_pid_get_session()</function>
314 is completely unrelated to the process session
315 identifier as returned by
316 <citerefentry><refentrytitle>getsid</refentrytitle><manvolnum>2</manvolnum></citerefentry>.</para>
320 <title>See Also</title>
323 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
324 <citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
325 <citerefentry><refentrytitle>sd_session_is_active</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
326 <citerefentry><refentrytitle>getsid</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
327 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
328 <citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>