| 1 |
<?xml version='1.0'?> |
| 2 |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
| 4 |
<!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
| 5 |
|
| 6 |
<refentry id="sd_pid_get_owner_uid" conditional='HAVE_PAM' |
| 7 |
xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 8 |
|
| 9 |
<refentryinfo> |
| 10 |
<title>sd_pid_get_owner_uid</title> |
| 11 |
<productname>systemd</productname> |
| 12 |
</refentryinfo> |
| 13 |
|
| 14 |
<refmeta> |
| 15 |
<refentrytitle>sd_pid_get_owner_uid</refentrytitle> |
| 16 |
<manvolnum>3</manvolnum> |
| 17 |
</refmeta> |
| 18 |
|
| 19 |
<refnamediv> |
| 20 |
<refname>sd_pid_get_owner_uid</refname> |
| 21 |
<refname>sd_pid_get_session</refname> |
| 22 |
<refname>sd_pid_get_user_unit</refname> |
| 23 |
<refname>sd_pid_get_unit</refname> |
| 24 |
<refname>sd_pid_get_machine_name</refname> |
| 25 |
<refname>sd_pid_get_slice</refname> |
| 26 |
<refname>sd_pid_get_user_slice</refname> |
| 27 |
<refname>sd_pid_get_cgroup</refname> |
| 28 |
<refname>sd_pidfd_get_owner_uid</refname> |
| 29 |
<refname>sd_pidfd_get_session</refname> |
| 30 |
<refname>sd_pidfd_get_user_unit</refname> |
| 31 |
<refname>sd_pidfd_get_unit</refname> |
| 32 |
<refname>sd_pidfd_get_machine_name</refname> |
| 33 |
<refname>sd_pidfd_get_slice</refname> |
| 34 |
<refname>sd_pidfd_get_user_slice</refname> |
| 35 |
<refname>sd_pidfd_get_cgroup</refname> |
| 36 |
<refname>sd_peer_get_owner_uid</refname> |
| 37 |
<refname>sd_peer_get_session</refname> |
| 38 |
<refname>sd_peer_get_user_unit</refname> |
| 39 |
<refname>sd_peer_get_unit</refname> |
| 40 |
<refname>sd_peer_get_machine_name</refname> |
| 41 |
<refname>sd_peer_get_slice</refname> |
| 42 |
<refname>sd_peer_get_user_slice</refname> |
| 43 |
<refname>sd_peer_get_cgroup</refname> |
| 44 |
<refpurpose>Determine the owner uid of the user unit or session, |
| 45 |
or the session, user unit, system unit, container/VM or slice that |
| 46 |
a specific PID or socket peer belongs to</refpurpose> |
| 47 |
</refnamediv> |
| 48 |
|
| 49 |
<refsynopsisdiv> |
| 50 |
<funcsynopsis> |
| 51 |
<funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo> |
| 52 |
|
| 53 |
<funcprototype> |
| 54 |
<funcdef>int <function>sd_pid_get_owner_uid</function></funcdef> |
| 55 |
<paramdef>pid_t <parameter>pid</parameter></paramdef> |
| 56 |
<paramdef>uid_t *<parameter>uid</parameter></paramdef> |
| 57 |
</funcprototype> |
| 58 |
|
| 59 |
<funcprototype> |
| 60 |
<funcdef>int <function>sd_pid_get_session</function></funcdef> |
| 61 |
<paramdef>pid_t <parameter>pid</parameter></paramdef> |
| 62 |
<paramdef>char **<parameter>session</parameter></paramdef> |
| 63 |
</funcprototype> |
| 64 |
|
| 65 |
<funcprototype> |
| 66 |
<funcdef>int <function>sd_pid_get_user_unit</function></funcdef> |
| 67 |
<paramdef>pid_t <parameter>pid</parameter></paramdef> |
| 68 |
<paramdef>char **<parameter>unit</parameter></paramdef> |
| 69 |
</funcprototype> |
| 70 |
|
| 71 |
<funcprototype> |
| 72 |
<funcdef>int <function>sd_pid_get_unit</function></funcdef> |
| 73 |
<paramdef>pid_t <parameter>pid</parameter></paramdef> |
| 74 |
<paramdef>char **<parameter>unit</parameter></paramdef> |
| 75 |
</funcprototype> |
| 76 |
|
| 77 |
<funcprototype> |
| 78 |
<funcdef>int <function>sd_pid_get_machine_name</function></funcdef> |
| 79 |
<paramdef>pid_t <parameter>pid</parameter></paramdef> |
| 80 |
<paramdef>char **<parameter>name</parameter></paramdef> |
| 81 |
</funcprototype> |
| 82 |
|
| 83 |
<funcprototype> |
| 84 |
<funcdef>int <function>sd_pid_get_slice</function></funcdef> |
| 85 |
<paramdef>pid_t <parameter>pid</parameter></paramdef> |
| 86 |
<paramdef>char **<parameter>slice</parameter></paramdef> |
| 87 |
</funcprototype> |
| 88 |
|
| 89 |
<funcprototype> |
| 90 |
<funcdef>int <function>sd_pid_get_user_slice</function></funcdef> |
| 91 |
<paramdef>pid_t <parameter>pid</parameter></paramdef> |
| 92 |
<paramdef>char **<parameter>slice</parameter></paramdef> |
| 93 |
</funcprototype> |
| 94 |
|
| 95 |
<funcprototype> |
| 96 |
<funcdef>int <function>sd_pid_get_cgroup</function></funcdef> |
| 97 |
<paramdef>pid_t <parameter>pid</parameter></paramdef> |
| 98 |
<paramdef>char **<parameter>cgroup</parameter></paramdef> |
| 99 |
</funcprototype> |
| 100 |
|
| 101 |
<funcprototype> |
| 102 |
<funcdef>int <function>sd_pidfd_get_owner_uid</function></funcdef> |
| 103 |
<paramdef>int <parameter>pidfd</parameter></paramdef> |
| 104 |
<paramdef>uid_t *<parameter>uid</parameter></paramdef> |
| 105 |
</funcprototype> |
| 106 |
|
| 107 |
<funcprototype> |
| 108 |
<funcdef>int <function>sd_pidfd_get_session</function></funcdef> |
| 109 |
<paramdef>int <parameter>pidfd</parameter></paramdef> |
| 110 |
<paramdef>char **<parameter>session</parameter></paramdef> |
| 111 |
</funcprototype> |
| 112 |
|
| 113 |
<funcprototype> |
| 114 |
<funcdef>int <function>sd_pidfd_get_user_unit</function></funcdef> |
| 115 |
<paramdef>int <parameter>pidfd</parameter></paramdef> |
| 116 |
<paramdef>char **<parameter>unit</parameter></paramdef> |
| 117 |
</funcprototype> |
| 118 |
|
| 119 |
<funcprototype> |
| 120 |
<funcdef>int <function>sd_pidfd_get_unit</function></funcdef> |
| 121 |
<paramdef>int <parameter>pidfd</parameter></paramdef> |
| 122 |
<paramdef>char **<parameter>unit</parameter></paramdef> |
| 123 |
</funcprototype> |
| 124 |
|
| 125 |
<funcprototype> |
| 126 |
<funcdef>int <function>sd_pidfd_get_machine_name</function></funcdef> |
| 127 |
<paramdef>int <parameter>pidfd</parameter></paramdef> |
| 128 |
<paramdef>char **<parameter>name</parameter></paramdef> |
| 129 |
</funcprototype> |
| 130 |
|
| 131 |
<funcprototype> |
| 132 |
<funcdef>int <function>sd_pidfd_get_slice</function></funcdef> |
| 133 |
<paramdef>int <parameter>pidfd</parameter></paramdef> |
| 134 |
<paramdef>char **<parameter>slice</parameter></paramdef> |
| 135 |
</funcprototype> |
| 136 |
|
| 137 |
<funcprototype> |
| 138 |
<funcdef>int <function>sd_pidfd_get_user_slice</function></funcdef> |
| 139 |
<paramdef>int <parameter>pidfd</parameter></paramdef> |
| 140 |
<paramdef>char **<parameter>slice</parameter></paramdef> |
| 141 |
</funcprototype> |
| 142 |
|
| 143 |
<funcprototype> |
| 144 |
<funcdef>int <function>sd_pidfd_get_cgroup</function></funcdef> |
| 145 |
<paramdef>int <parameter>pidfd</parameter></paramdef> |
| 146 |
<paramdef>char **<parameter>cgroup</parameter></paramdef> |
| 147 |
</funcprototype> |
| 148 |
|
| 149 |
<funcprototype> |
| 150 |
<funcdef>int <function>sd_peer_get_owner_uid</function></funcdef> |
| 151 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 152 |
<paramdef>uid_t *<parameter>uid</parameter></paramdef> |
| 153 |
</funcprototype> |
| 154 |
|
| 155 |
<funcprototype> |
| 156 |
<funcdef>int <function>sd_peer_get_session</function></funcdef> |
| 157 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 158 |
<paramdef>char **<parameter>session</parameter></paramdef> |
| 159 |
</funcprototype> |
| 160 |
|
| 161 |
<funcprototype> |
| 162 |
<funcdef>int <function>sd_peer_get_user_unit</function></funcdef> |
| 163 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 164 |
<paramdef>char **<parameter>unit</parameter></paramdef> |
| 165 |
</funcprototype> |
| 166 |
|
| 167 |
<funcprototype> |
| 168 |
<funcdef>int <function>sd_peer_get_unit</function></funcdef> |
| 169 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 170 |
<paramdef>char **<parameter>unit</parameter></paramdef> |
| 171 |
</funcprototype> |
| 172 |
|
| 173 |
<funcprototype> |
| 174 |
<funcdef>int <function>sd_peer_get_machine_name</function></funcdef> |
| 175 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 176 |
<paramdef>char **<parameter>name</parameter></paramdef> |
| 177 |
</funcprototype> |
| 178 |
|
| 179 |
<funcprototype> |
| 180 |
<funcdef>int <function>sd_peer_get_slice</function></funcdef> |
| 181 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 182 |
<paramdef>char **<parameter>slice</parameter></paramdef> |
| 183 |
</funcprototype> |
| 184 |
|
| 185 |
<funcprototype> |
| 186 |
<funcdef>int <function>sd_peer_get_user_slice</function></funcdef> |
| 187 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 188 |
<paramdef>char **<parameter>slice</parameter></paramdef> |
| 189 |
</funcprototype> |
| 190 |
|
| 191 |
<funcprototype> |
| 192 |
<funcdef>int <function>sd_peer_get_cgroup</function></funcdef> |
| 193 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 194 |
<paramdef>char **<parameter>cgroup</parameter></paramdef> |
| 195 |
</funcprototype> |
| 196 |
</funcsynopsis> |
| 197 |
</refsynopsisdiv> |
| 198 |
|
| 199 |
<refsect1> |
| 200 |
<title>Description</title> |
| 201 |
|
| 202 |
<para><function>sd_pid_get_owner_uid()</function> may be used to |
| 203 |
determine the Unix UID (user identifier) which owns the login |
| 204 |
session or systemd user unit of a process identified by the |
| 205 |
specified PID. For processes which are not part of a login session |
| 206 |
and not managed by a user manager, this function will fail with |
| 207 |
<constant>-ENODATA</constant>.</para> |
| 208 |
|
| 209 |
<para><function>sd_pid_get_session()</function> may be used to |
| 210 |
determine the login session identifier of a process identified by |
| 211 |
the specified process identifier. The session identifier is a |
| 212 |
short string, suitable for usage in file system paths. Please |
| 213 |
note the login session may be limited to a stub process or two. |
| 214 |
User processes may instead be started from their systemd user |
| 215 |
manager, e.g. GUI applications started using DBus activation, as |
| 216 |
well as service processes which are shared between multiple logins |
| 217 |
of the same user. For processes which are not part of a login |
| 218 |
session, this function will fail with <constant>-ENODATA</constant>. |
| 219 |
The returned string needs to be freed with the libc <citerefentry |
| 220 |
project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 221 |
call after use.</para> |
| 222 |
|
| 223 |
<para><function>sd_pid_get_user_unit()</function> may be used to |
| 224 |
determine the systemd user unit (i.e. user service or scope unit) |
| 225 |
identifier of a process identified by the specified PID. The |
| 226 |
unit name is a short string, suitable for usage in file system |
| 227 |
paths. For processes which are not managed by a user manager, this |
| 228 |
function will fail with <constant>-ENODATA</constant>. The |
| 229 |
returned string needs to be freed with the libc <citerefentry |
| 230 |
project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 231 |
call after use.</para> |
| 232 |
|
| 233 |
<para><function>sd_pid_get_unit()</function> may be used to |
| 234 |
determine the systemd system unit (i.e. system service or scope |
| 235 |
unit) identifier of a process identified by the specified PID. The |
| 236 |
unit name is a short string, suitable for usage in file system |
| 237 |
paths. Note that not all processes are part of a system |
| 238 |
unit/service. For processes not being part of a systemd system |
| 239 |
unit, this function will fail with <constant>-ENODATA</constant>. |
| 240 |
(More specifically, this call will not work for kernel threads.) |
| 241 |
The returned string needs to be freed with the libc <citerefentry |
| 242 |
project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 243 |
call after use.</para> |
| 244 |
|
| 245 |
<para><function>sd_pid_get_machine_name()</function> may be used |
| 246 |
to determine the name of the VM or container is a member of. The |
| 247 |
machine name is a short string, suitable for usage in file system |
| 248 |
paths. The returned string needs to be freed with the libc |
| 249 |
<citerefentry |
| 250 |
project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 251 |
call after use. For processes not part of a VM or container, this |
| 252 |
function fails with <constant>-ENODATA</constant>.</para> |
| 253 |
|
| 254 |
<para><function>sd_pid_get_slice()</function> may be used to |
| 255 |
determine the slice unit the process is a member of. See |
| 256 |
<citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry> |
| 257 |
for details about slices. The returned string needs to be freed |
| 258 |
with the libc |
| 259 |
<citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 260 |
call after use.</para> |
| 261 |
|
| 262 |
<para>Similarly, <function>sd_pid_get_user_slice()</function> |
| 263 |
returns the user slice (as managed by the user's systemd instance) |
| 264 |
of a process.</para> |
| 265 |
|
| 266 |
<para><function>sd_pid_get_cgroup()</function> returns the control group path of the specified process, |
| 267 |
relative to the root of the hierarchy. Returns the path without trailing slash, except for processes |
| 268 |
located in the root control group, where "/" is returned. To find the actual control group path |
| 269 |
in the file system, the returned path needs to be prefixed with <filename>/sys/fs/cgroup/</filename>.</para> |
| 270 |
|
| 271 |
<para>If the <varname>pid</varname> parameter of any of these |
| 272 |
functions is passed as 0, the operation is executed for the |
| 273 |
calling process.</para> |
| 274 |
|
| 275 |
<para>The <function>sd_pidfd_get_owner_uid()</function>, |
| 276 |
<function>sd_pidfd_get_session()</function>, |
| 277 |
<function>sd_pidfd_get_user_unit()</function>, |
| 278 |
<function>sd_pidfd_get_unit()</function>, |
| 279 |
<function>sd_pidfd_get_machine_name()</function>, |
| 280 |
<function>sd_pidfd_get_slice()</function>, |
| 281 |
<function>sd_pidfd_get_user_slice()</function> and |
| 282 |
<function>sd_pidfd_get_cgroup()</function> calls operate similarly to their PID counterparts, but accept a |
| 283 |
<constant>PIDFD</constant> instead of a <constant>PID</constant>, which means they are not subject to recycle |
| 284 |
race conditions as the process is pinned by the file descriptor during the whole duration of the invocation. |
| 285 |
Note that these require a kernel that supports <constant>PIDFD</constant>. A suitable file descriptor may be |
| 286 |
acquired via |
| 287 |
<citerefentry project='man-pages'><refentrytitle>pidfd_open</refentrytitle><manvolnum>2</manvolnum></citerefentry>.</para> |
| 288 |
|
| 289 |
<para>The <function>sd_peer_get_owner_uid()</function>, |
| 290 |
<function>sd_peer_get_session()</function>, |
| 291 |
<function>sd_peer_get_user_unit()</function>, |
| 292 |
<function>sd_peer_get_unit()</function>, |
| 293 |
<function>sd_peer_get_machine_name()</function>, |
| 294 |
<function>sd_peer_get_slice()</function>, |
| 295 |
<function>sd_peer_get_user_slice()</function> and |
| 296 |
<function>sd_peer_get_cgroup()</function> calls operate similarly to their PID counterparts, but accept a |
| 297 |
connected <constant>AF_UNIX</constant> socket and retrieve information about the connected peer process. |
| 298 |
Note that these fields are retrieved via <filename>/proc/</filename>, and hence are not suitable for |
| 299 |
authorization purposes, as they are subject to races.</para> |
| 300 |
</refsect1> |
| 301 |
|
| 302 |
<refsect1> |
| 303 |
<title>Return Value</title> |
| 304 |
|
| 305 |
<para>On success, these calls return 0 or a positive integer. On failure, these calls return a negative |
| 306 |
errno-style error code.</para> |
| 307 |
|
| 308 |
<refsect2> |
| 309 |
<title>Errors</title> |
| 310 |
|
| 311 |
<para>Returned errors may indicate the following problems:</para> |
| 312 |
|
| 313 |
<variablelist> |
| 314 |
|
| 315 |
<varlistentry> |
| 316 |
<term><constant>-ESRCH</constant></term> |
| 317 |
|
| 318 |
<listitem><para>The specified PID does not refer to a running process.</para> |
| 319 |
</listitem> |
| 320 |
</varlistentry> |
| 321 |
|
| 322 |
<varlistentry> |
| 323 |
<term><constant>-EBADF</constant></term> |
| 324 |
|
| 325 |
<listitem><para>The specified file descriptor is invalid.</para></listitem> |
| 326 |
</varlistentry> |
| 327 |
|
| 328 |
<varlistentry> |
| 329 |
<term><constant>-ENODATA</constant></term> |
| 330 |
|
| 331 |
<listitem><para>The given field is not specified for the described process or peer.</para> |
| 332 |
</listitem> |
| 333 |
</varlistentry> |
| 334 |
|
| 335 |
<varlistentry> |
| 336 |
<term><constant>-EINVAL</constant></term> |
| 337 |
|
| 338 |
<listitem><para>An input parameter was invalid (out of range, or <constant>NULL</constant>, where |
| 339 |
that is not accepted).</para></listitem> |
| 340 |
</varlistentry> |
| 341 |
|
| 342 |
<varlistentry> |
| 343 |
<term><constant>-ENOMEM</constant></term> |
| 344 |
|
| 345 |
<listitem><para>Memory allocation failed.</para></listitem> |
| 346 |
</varlistentry> |
| 347 |
</variablelist> |
| 348 |
</refsect2> |
| 349 |
</refsect1> |
| 350 |
|
| 351 |
<refsect1> |
| 352 |
<title>Notes</title> |
| 353 |
|
| 354 |
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/> |
| 355 |
|
| 356 |
<para>Note that the login session identifier as |
| 357 |
returned by <function>sd_pid_get_session()</function> |
| 358 |
is completely unrelated to the process session |
| 359 |
identifier as returned by |
| 360 |
<citerefentry><refentrytitle>getsid</refentrytitle><manvolnum>2</manvolnum></citerefentry>.</para> |
| 361 |
</refsect1> |
| 362 |
|
| 363 |
<refsect1> |
| 364 |
<title>History</title> |
| 365 |
<para><function>sd_peer_get_cgroup()</function>, |
| 366 |
<function>sd_peer_get_machine_name()</function>, |
| 367 |
<function>sd_peer_get_owner_uid()</function>, |
| 368 |
<function>sd_peer_get_session()</function>, |
| 369 |
<function>sd_peer_get_slice()</function>, |
| 370 |
<function>sd_peer_get_unit()</function>, |
| 371 |
<function>sd_peer_get_user_slice()</function>, |
| 372 |
<function>sd_peer_get_user_unit()</function>, |
| 373 |
<function>sd_pid_get_cgroup()</function>, |
| 374 |
<function>sd_pid_get_machine_name()</function>, |
| 375 |
<function>sd_pid_get_owner_uid()</function>, |
| 376 |
<function>sd_pid_get_session()</function>, |
| 377 |
<function>sd_pid_get_slice()</function>, |
| 378 |
<function>sd_pid_get_unit()</function>, |
| 379 |
<function>sd_pid_get_user_slice()</function>, and |
| 380 |
<function>sd_pid_get_user_unit()</function> were added in version 236.</para> |
| 381 |
<para><function>sd_pidfd_get_owner_uid()</function>, |
| 382 |
<function>sd_pidfd_get_session()</function>, |
| 383 |
<function>sd_pidfd_get_user_unit()</function>, |
| 384 |
<function>sd_pidfd_get_unit()</function>, |
| 385 |
<function>sd_pidfd_get_machine_name()</function>, |
| 386 |
<function>sd_pidfd_get_slice()</function>, |
| 387 |
<function>sd_pidfd_get_user_slice()</function>, and |
| 388 |
<function>sd_pidfd_get_cgroup()</function> were added in version 253.</para> |
| 389 |
</refsect1> |
| 390 |
|
| 391 |
<refsect1> |
| 392 |
<title>See Also</title> |
| 393 |
|
| 394 |
<para><simplelist type="inline"> |
| 395 |
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> |
| 396 |
<member><citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 397 |
<member><citerefentry><refentrytitle>sd_session_is_active</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 398 |
<member><citerefentry><refentrytitle>getsid</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> |
| 399 |
<member><citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry></member> |
| 400 |
<member><citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry></member> |
| 401 |
</simplelist></para> |
| 402 |
</refsect1> |
| 403 |
|
| 404 |
</refentry> |