1 <?xml version='
1.0'
?> <!--*-nxml-*-->
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_peer_get_session
</refname>
54 <refname>sd_peer_get_unit
</refname>
55 <refname>sd_peer_get_user_unit
</refname>
56 <refname>sd_peer_get_owner_uid
</refname>
57 <refname>sd_peer_get_machine_name
</refname>
58 <refname>sd_peer_get_slice
</refname>
59 <refname>sd_peer_get_user_slice
</refname>
60 <refpurpose>Determine session, unit, owner of a session,
61 container/VM or slice of a specific PID or socket
67 <funcsynopsisinfo>#include
<systemd/sd-login.h
></funcsynopsisinfo>
70 <funcdef>int
<function>sd_pid_get_session
</function></funcdef>
71 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
72 <paramdef>char **
<parameter>session
</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_user_unit
</function></funcdef>
83 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
84 <paramdef>char **
<parameter>unit
</parameter></paramdef>
88 <funcdef>int
<function>sd_pid_get_owner_uid
</function></funcdef>
89 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
90 <paramdef>uid_t *
<parameter>uid
</parameter></paramdef>
94 <funcdef>int
<function>sd_pid_get_machine_name
</function></funcdef>
95 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
96 <paramdef>char **
<parameter>name
</parameter></paramdef>
100 <funcdef>int
<function>sd_pid_get_slice
</function></funcdef>
101 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
102 <paramdef>char **
<parameter>slice
</parameter></paramdef>
106 <funcdef>int
<function>sd_pid_get_user_slice
</function></funcdef>
107 <paramdef>pid_t
<parameter>pid
</parameter></paramdef>
108 <paramdef>char **
<parameter>slice
</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_unit
</function></funcdef>
119 <paramdef>int
<parameter>fd
</parameter></paramdef>
120 <paramdef>char **
<parameter>unit
</parameter></paramdef>
124 <funcdef>int
<function>sd_peer_get_user_unit
</function></funcdef>
125 <paramdef>int
<parameter>fd
</parameter></paramdef>
126 <paramdef>char **
<parameter>unit
</parameter></paramdef>
130 <funcdef>int
<function>sd_peer_get_owner_uid
</function></funcdef>
131 <paramdef>int
<parameter>fd
</parameter></paramdef>
132 <paramdef>uid_t *
<parameter>uid
</parameter></paramdef>
136 <funcdef>int
<function>sd_peer_get_machine_name
</function></funcdef>
137 <paramdef>int
<parameter>fd
</parameter></paramdef>
138 <paramdef>char **
<parameter>name
</parameter></paramdef>
142 <funcdef>int
<function>sd_peer_get_slice
</function></funcdef>
143 <paramdef>int
<parameter>fd
</parameter></paramdef>
144 <paramdef>char **
<parameter>slice
</parameter></paramdef>
148 <funcdef>int
<function>sd_peer_get_user_slice
</function></funcdef>
149 <paramdef>int
<parameter>fd
</parameter></paramdef>
150 <paramdef>char **
<parameter>slice
</parameter></paramdef>
156 <title>Description
</title>
158 <para><function>sd_pid_get_session()
</function> may be used to
159 determine the login session identifier of a process identified by
160 the specified process identifier. The session identifier is a
161 short string, suitable for usage in file system paths. Note that
162 not all processes are part of a login session (e.g. system service
163 processes, user processes that are shared between multiple
164 sessions of the same user, or kernel threads). For processes not
165 being part of a login session this function will fail with
166 -ENXIO. The returned string needs to be freed with the libc
168 project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
169 call after use.
</para>
171 <para><function>sd_pid_get_unit()
</function> may be used to
172 determine the systemd system unit (i.e. system service or scope
173 unit) identifier of a process identified by the specified PID. The
174 unit name is a short string, suitable for usage in file system
175 paths. Note that not all processes are part of a system
176 unit/service (e.g. user processes, or kernel threads). For
177 processes not being part of a systemd system unit this function
178 will fail with -ENXIO (More specifically: this call will not work
179 for kernel threads.) The returned string needs to be freed with
180 the libc
<citerefentry
181 project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
182 call after use.
</para>
184 <para><function>sd_pid_get_user_unit()
</function> may be used to
185 determine the systemd user unit (i.e. user service or scope unit)
186 identifier of a process identified by the specified PID. This is
187 similar to
<function>sd_pid_get_unit()
</function> but applies to
188 user units instead of system units.
</para>
190 <para><function>sd_pid_get_owner_uid()
</function> may be used to
191 determine the Unix UID (user identifier) of the owner of the
192 session of a process identified the specified PID. Note that this
193 function will succeed for user processes which are shared between
194 multiple login sessions of the same user, where
195 <function>sd_pid_get_session()
</function> will fail. For processes
196 not being part of a login session and not being a shared process
197 of a user this function will fail with -ENXIO.
</para>
199 <para><function>sd_pid_get_machine_name()
</function> may be used
200 to determine the name of the VM or container is a member of. The
201 machine name is a short string, suitable for usage in file system
202 paths. The returned string needs to be freed with the libc
204 project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
205 call after use. For processes not part of a VM or containers this
206 function fails with -ENXIO.
</para>
208 <para><function>sd_pid_get_slice()
</function> may be used to
209 determine the slice unit the process is a member of. See
210 <citerefentry><refentrytitle>systemd.slice
</refentrytitle><manvolnum>5</manvolnum></citerefentry>
211 for details about slices. The returned string needs to be freed
213 <citerefentry project='man-pages'
><refentrytitle>free
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
214 call after use.
</para>
216 <para>Similar,
<function>sd_pid_get_user_slice()
</function>
217 returns the user slice (as managed by the user's systemd instance)
220 <para>If the
<varname>pid
</varname> parameter of any of these
221 functions is passed as
0, the operation is executed for the
222 calling process.
</para>
224 <para>The
<function>sd_peer_get_session()
</function>,
225 <function>sd_peer_get_unit()
</function>,
226 <function>sd_peer_get_user_unit()
</function>,
227 <function>sd_peer_get_owner_uid()
</function>,
228 <function>sd_peer_get_machine_name()
</function>,
229 <function>sd_peer_get_slice()
</function> and
230 <function>sd_peer_get_user_slice()
</function> calls operate
231 similar to their PID counterparts, but operate on a connected
232 AF_UNIX socket and retrieve information about the connected peer
233 process. Note that these fields are retrieved via
234 <filename>/proc
</filename>, and hence are not suitable for
235 authorization purposes, as they are subject to races.
</para>
239 <title>Return Value
</title>
241 <para>On success, these calls return
0 or a positive integer. On
242 failure, these calls return a negative errno-style error
247 <title>Errors
</title>
249 <para>Returned errors may indicate the following problems:
</para>
254 <term><constant>-ENXIO
</constant></term>
256 <listitem><para>Given field is not specified for the described
257 process or peer.
</para>
262 <term><constant>-ESRCH
</constant></term>
264 <listitem><para>The specified PID does not refer to a running
270 <term><constant>-ENOMEM
</constant></term>
272 <listitem><para>Memory allocation failed.
</para></listitem>
280 <para>The
<function>sd_pid_get_session()
</function>,
281 <function>sd_pid_get_unit()
</function>,
282 <function>sd_pid_get_user_unit()
</function>,
283 <function>sd_pid_get_owner_uid()
</function>,
284 <function>sd_pid_get_machine_name()
</function>,
285 <function>sd_pid_get_slice()
</function>,
286 <function>sd_pid_get_user_slice()
</function>,
287 <function>sd_peer_get_session()
</function>,
288 <function>sd_peer_get_unit()
</function>,
289 <function>sd_peer_get_user_unit()
</function>,
290 <function>sd_peer_get_owner_uid()
</function>,
291 <function>sd_peer_get_machine_name()
</function>,
292 <function>sd_peer_get_slice()
</function> and
293 <function>sd_peer_get_user_slice()
</function> interfaces are
294 available as a shared library, which can be compiled and linked to
295 with the
<constant>libsystemd
</constant> <citerefentry
296 project='die-net'
><refentrytitle>pkg-config
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
299 <para>Note that the login session identifier as
300 returned by
<function>sd_pid_get_session()
</function>
301 is completely unrelated to the process session
302 identifier as returned by
303 <citerefentry><refentrytitle>getsid
</refentrytitle><manvolnum>2</manvolnum></citerefentry>.
</para>
307 <title>See Also
</title>
310 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
311 <citerefentry><refentrytitle>sd-login
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
312 <citerefentry><refentrytitle>sd_session_is_active
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
313 <citerefentry><refentrytitle>getsid
</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
314 <citerefentry><refentrytitle>systemd.slice
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
315 <citerefentry><refentrytitle>systemd-machined.service
</refentrytitle><manvolnum>8</manvolnum></citerefentry>