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+
9 <refentry id=
"sd_pid_get_owner_uid" conditional='HAVE_PAM'
10 xmlns:
xi=
"http://www.w3.org/2001/XInclude">
13 <title>sd_pid_get_owner_uid
</title>
14 <productname>systemd
</productname>
18 <contrib>Developer
</contrib>
19 <firstname>Lennart
</firstname>
20 <surname>Poettering
</surname>
21 <email>lennart@poettering.net
</email>
27 <refentrytitle>sd_pid_get_owner_uid
</refentrytitle>
28 <manvolnum>3</manvolnum>
32 <refname>sd_pid_get_owner_uid
</refname>
33 <refname>sd_pid_get_session
</refname>
34 <refname>sd_pid_get_user_unit
</refname>
35 <refname>sd_pid_get_unit
</refname>
36 <refname>sd_pid_get_machine_name
</refname>
37 <refname>sd_pid_get_slice
</refname>
38 <refname>sd_pid_get_user_slice
</refname>
39 <refname>sd_pid_get_cgroup
</refname>
40 <refname>sd_peer_get_owner_uid
</refname>
41 <refname>sd_peer_get_session
</refname>
42 <refname>sd_peer_get_user_unit
</refname>
43 <refname>sd_peer_get_unit
</refname>
44 <refname>sd_peer_get_machine_name
</refname>
45 <refname>sd_peer_get_slice
</refname>
46 <refname>sd_peer_get_user_slice
</refname>
47 <refname>sd_peer_get_cgroup
</refname>
48 <refpurpose>Determine the owner uid of the user unit or session,
49 or the session, user unit, system unit, container/VM or slice that
50 a specific PID or socket peer belongs to.
</refpurpose>
55 <funcsynopsisinfo>#include
<systemd/sd-login.h
></funcsynopsisinfo>
58 <funcdef>int
<function>sd_pid_get_owner_uid
</function></funcdef>
59 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
60 <paramdef>uid_t *
<parameter>uid
</parameter></paramdef>
64 <funcdef>int
<function>sd_pid_get_session
</function></funcdef>
65 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
66 <paramdef>char **
<parameter>session
</parameter></paramdef>
70 <funcdef>int
<function>sd_pid_get_user_unit
</function></funcdef>
71 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
72 <paramdef>char **
<parameter>unit
</parameter></paramdef>
76 <funcdef>int
<function>sd_pid_get_unit
</function></funcdef>
77 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
78 <paramdef>char **
<parameter>unit
</parameter></paramdef>
82 <funcdef>int
<function>sd_pid_get_machine_name
</function></funcdef>
83 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
84 <paramdef>char **
<parameter>name
</parameter></paramdef>
88 <funcdef>int
<function>sd_pid_get_slice
</function></funcdef>
89 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
90 <paramdef>char **
<parameter>slice
</parameter></paramdef>
94 <funcdef>int
<function>sd_pid_get_user_slice
</function></funcdef>
95 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
96 <paramdef>char **
<parameter>slice
</parameter></paramdef>
100 <funcdef>int
<function>sd_pid_get_cgroup
</function></funcdef>
101 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
102 <paramdef>char **
<parameter>cgroup
</parameter></paramdef>
106 <funcdef>int
<function>sd_peer_get_owner_uid
</function></funcdef>
107 <paramdef>int
<parameter>fd
</parameter></paramdef>
108 <paramdef>uid_t *
<parameter>uid
</parameter></paramdef>
112 <funcdef>int
<function>sd_peer_get_session
</function></funcdef>
113 <paramdef>int
<parameter>fd
</parameter></paramdef>
114 <paramdef>char **
<parameter>session
</parameter></paramdef>
118 <funcdef>int
<function>sd_peer_get_user_unit
</function></funcdef>
119 <paramdef>int
<parameter>fd
</parameter></paramdef>
120 <paramdef>char **
<parameter>unit
</parameter></paramdef>
124 <funcdef>int
<function>sd_peer_get_unit
</function></funcdef>
125 <paramdef>int
<parameter>fd
</parameter></paramdef>
126 <paramdef>char **
<parameter>unit
</parameter></paramdef>
130 <funcdef>int
<function>sd_peer_get_machine_name
</function></funcdef>
131 <paramdef>int
<parameter>fd
</parameter></paramdef>
132 <paramdef>char **
<parameter>name
</parameter></paramdef>
136 <funcdef>int
<function>sd_peer_get_slice
</function></funcdef>
137 <paramdef>int
<parameter>fd
</parameter></paramdef>
138 <paramdef>char **
<parameter>slice
</parameter></paramdef>
142 <funcdef>int
<function>sd_peer_get_user_slice
</function></funcdef>
143 <paramdef>int
<parameter>fd
</parameter></paramdef>
144 <paramdef>char **
<parameter>slice
</parameter></paramdef>
148 <funcdef>int
<function>sd_peer_get_cgroup
</function></funcdef>
149 <paramdef>int
<parameter>fd
</parameter></paramdef>
150 <paramdef>char **
<parameter>cgroup
</parameter></paramdef>
156 <title>Description
</title>
158 <para><function>sd_pid_get_owner_uid()
</function> may be used to
159 determine the Unix UID (user identifier) which owns the login
160 session or systemd user unit of a process identified by the
161 specified PID. For processes which are not part of a login session
162 and not managed by a user manager, this function will fail with
163 <constant>-ENODATA
</constant>.
</para>
165 <para><function>sd_pid_get_session()
</function> may be used to
166 determine the login session identifier of a process identified by
167 the specified process identifier. The session identifier is a
168 short string, suitable for usage in file system paths. Please
169 note the login session may be limited to a stub process or two.
170 User processes may instead be started from their systemd user
171 manager, e.g. GUI applications started using DBus activation, as
172 well as service processes which are shared between multiple logins
173 of the same user. For processes which are not part of a login
174 session, this function will fail with
<constant>-ENODATA
</constant>.
175 The returned string needs to be freed with the libc
<citerefentry
176 project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
177 call after use.
</para>
179 <para><function>sd_pid_get_user_unit()
</function> may be used to
180 determine the systemd user unit (i.e. user service or scope unit)
181 identifier of a process identified by the specified PID. The
182 unit name is a short string, suitable for usage in file system
183 paths. For processes which are not managed by a user manager, this
184 function will fail with
<constant>-ENODATA
</constant>. The
185 returned string needs to be freed with the libc
<citerefentry
186 project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
187 call after use.
</para>
189 <para><function>sd_pid_get_unit()
</function> may be used to
190 determine the systemd system unit (i.e. system service or scope
191 unit) identifier of a process identified by the specified PID. The
192 unit name is a short string, suitable for usage in file system
193 paths. Note that not all processes are part of a system
194 unit/service. For processes not being part of a systemd system
195 unit, this function will fail with
<constant>-ENODATA
</constant>.
196 (More specifically, this call will not work for kernel threads.)
197 The returned string needs to be freed with the libc
<citerefentry
198 project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
199 call after use.
</para>
201 <para><function>sd_pid_get_machine_name()
</function> may be used
202 to determine the name of the VM or container is a member of. The
203 machine name is a short string, suitable for usage in file system
204 paths. The returned string needs to be freed with the libc
206 project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
207 call after use. For processes not part of a VM or container, this
208 function fails with
<constant>-ENODATA
</constant>.
</para>
210 <para><function>sd_pid_get_slice()
</function> may be used to
211 determine the slice unit the process is a member of. See
212 <citerefentry><refentrytitle>systemd.slice
</refentrytitle><manvolnum>5</manvolnum></citerefentry>
213 for details about slices. The returned string needs to be freed
215 <citerefentry project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
216 call after use.
</para>
218 <para>Similarly,
<function>sd_pid_get_user_slice()
</function>
219 returns the user slice (as managed by the user's systemd instance)
222 <para><function>sd_pid_get_cgroup()
</function> returns the control
223 group path of the specified process, relative to the root of the
224 hierarchy. Returns the path without trailing slash, except for
225 processes located in the root control group, where
"/" is
226 returned. To find the actual control group path in the file system,
227 the returned path needs to be prefixed with
228 <filename>/sys/fs/cgroup/
</filename> (if the unified control group
230 <filename>/sys/fs/cgroup/
<replaceable>HIERARCHY
</replaceable>/
</filename>
231 (if the legacy multi-hierarchy control group setup is used).
</para>
233 <para>If the
<varname>pid
</varname> parameter of any of these
234 functions is passed as
0, the operation is executed for the
235 calling process.
</para>
237 <para>The
<function>sd_peer_get_owner_uid()
</function>,
238 <function>sd_peer_get_session()
</function>,
239 <function>sd_peer_get_user_unit()
</function>,
240 <function>sd_peer_get_unit()
</function>,
241 <function>sd_peer_get_machine_name()
</function>,
242 <function>sd_peer_get_slice()
</function>,
243 <function>sd_peer_get_user_slice()
</function> and
244 <function>sd_peer_get_cgroup()
</function> calls operate similar to
245 their PID counterparts, but operate on a connected AF_UNIX socket
246 and retrieve information about the connected peer process. Note
247 that these fields are retrieved via
<filename>/proc
</filename>,
248 and hence are not suitable for authorization purposes, as they are
249 subject to races.
</para>
253 <title>Return Value
</title>
255 <para>On success, these calls return
0 or a positive integer. On
256 failure, these calls return a negative errno-style error
261 <title>Errors
</title>
263 <para>Returned errors may indicate the following problems:
</para>
268 <term><constant>-ESRCH
</constant></term>
270 <listitem><para>The specified PID does not refer to a running
276 <term><constant>-EBADF
</constant></term>
278 <listitem><para>The specified socket file descriptor was
279 invalid.
</para></listitem>
283 <term><constant>-ENODATA
</constant></term>
285 <listitem><para>The given field is not specified for the described
286 process or peer.
</para>
291 <term><constant>-EINVAL
</constant></term>
293 <listitem><para>An input parameter was invalid (out of range,
294 or NULL, where that is not accepted).
</para></listitem>
298 <term><constant>-ENOMEM
</constant></term>
300 <listitem><para>Memory allocation failed.
</para></listitem>
308 <xi:include href=
"libsystemd-pkgconfig.xml" xpointer=
"pkgconfig-text"/>
310 <para>Note that the login session identifier as
311 returned by
<function>sd_pid_get_session()
</function>
312 is completely unrelated to the process session
313 identifier as returned by
314 <citerefentry><refentrytitle>getsid
</refentrytitle><manvolnum>2</manvolnum></citerefentry>.
</para>
318 <title>See Also
</title>
321 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
322 <citerefentry><refentrytitle>sd-login
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
323 <citerefentry><refentrytitle>sd_session_is_active
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
324 <citerefentry><refentrytitle>getsid
</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
325 <citerefentry><refentrytitle>systemd.slice
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
326 <citerefentry><refentrytitle>systemd-machined.service
</refentrytitle><manvolnum>8</manvolnum></citerefentry>