chiark / gitweb /
76ed80966e4ee21b779dd0bc49c2c4f9e495f1c9
[elogind.git] / man / sd_event_add_child.xml
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">
4
5 <!--
6   SPDX-License-Identifier: LGPL-2.1+
7
8   This file is part of systemd.
9
10   Copyright 2014 Zbigniew JÄ™drzejewski-Szmek
11 -->
12
13 <refentry id="sd_event_add_child" xmlns:xi="http://www.w3.org/2001/XInclude">
14
15   <refentryinfo>
16     <title>sd_event_add_child</title>
17     <productname>systemd</productname>
18
19     <authorgroup>
20       <author>
21         <contrib>More text</contrib>
22         <firstname>Zbigniew</firstname>
23         <surname>JÄ™drzejewski-Szmek</surname>
24         <email>zbyszek@in.waw.pl</email>
25       </author>
26     </authorgroup>
27   </refentryinfo>
28
29   <refmeta>
30     <refentrytitle>sd_event_add_child</refentrytitle>
31     <manvolnum>3</manvolnum>
32   </refmeta>
33
34   <refnamediv>
35     <refname>sd_event_add_child</refname>
36     <refname>sd_event_source_get_child_pid</refname>
37     <refname>sd_event_child_handler_t</refname>
38
39     <refpurpose>Add a child process state change event source to an event loop</refpurpose>
40   </refnamediv>
41
42   <refsynopsisdiv>
43     <funcsynopsis>
44       <funcsynopsisinfo>#include &lt;systemd/sd-event.h&gt;</funcsynopsisinfo>
45
46       <funcsynopsisinfo><token>typedef</token> struct sd_event_source sd_event_source;</funcsynopsisinfo>
47
48       <funcprototype>
49         <funcdef>typedef int (*<function>sd_event_child_handler_t</function>)</funcdef>
50         <paramdef>sd_event_source *<parameter>s</parameter></paramdef>
51         <paramdef>const siginfo_t *<parameter>si</parameter></paramdef>
52         <paramdef>void *<parameter>userdata</parameter></paramdef>
53       </funcprototype>
54
55       <funcprototype>
56         <funcdef>int <function>sd_event_add_child</function></funcdef>
57         <paramdef>sd_event *<parameter>event</parameter></paramdef>
58         <paramdef>sd_event_source **<parameter>source</parameter></paramdef>
59         <paramdef>pid_t <parameter>pid</parameter></paramdef>
60         <paramdef>int <parameter>options</parameter></paramdef>
61         <paramdef>sd_event_child_handler_t <parameter>handler</parameter></paramdef>
62         <paramdef>void *<parameter>userdata</parameter></paramdef>
63       </funcprototype>
64
65       <funcprototype>
66         <funcdef>int <function>sd_event_source_get_child_pid</function></funcdef>
67         <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
68         <paramdef>pid_t *<parameter>pid</parameter></paramdef>
69       </funcprototype>
70
71     </funcsynopsis>
72   </refsynopsisdiv>
73
74   <refsect1>
75     <title>Description</title>
76
77     <para><function>sd_event_add_child()</function> adds a new child
78     process state change event source to an event loop. The event loop
79     object is specified in the <parameter>event</parameter> parameter,
80     the event source object is returned in the
81     <parameter>source</parameter> parameter. The
82     <parameter>pid</parameter> parameter specifies the PID of the
83     process to watch. The <parameter>handler</parameter> must
84     reference a function to call when the process changes state. The
85     handler function will be passed the
86     <parameter>userdata</parameter> pointer, which may be chosen
87     freely by the caller. The handler also receives a pointer to a
88     <structname>siginfo_t</structname> structure containing
89     information about the child process event. The
90     <parameter>options</parameter> parameter determines which state
91     changes will be watched for. It must contain an OR-ed mask of
92     <constant>WEXITED</constant> (watch for the child process
93     terminating), <constant>WSTOPPED</constant> (watch for the child
94     process being stopped by a signal), and
95     <constant>WCONTINUED</constant> (watch for the child process being
96     resumed by a signal). See <citerefentry
97     project='man-pages'><refentrytitle>waitid</refentrytitle><manvolnum>2</manvolnum></citerefentry>
98     for further information.</para>
99
100     <para>Only a single handler may be installed for a specific
101     child process. The handler is enabled for a single event
102     (<constant>SD_EVENT_ONESHOT</constant>), but this may be changed
103     with
104     <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
105     If the handler function returns a negative error code, it will be
106     disabled after the invocation, even if the
107     <constant>SD_EVENT_ON</constant> mode was requested before.
108     </para>
109
110     <para>To destroy an event source object use
111     <citerefentry><refentrytitle>sd_event_source_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
112     but note that the event source is only removed from the event loop
113     when all references to the event source are dropped. To make sure
114     an event source does not fire anymore, even when there's still a
115     reference to it kept, consider setting the event source to
116     <constant>SD_EVENT_OFF</constant> with
117     <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
118
119     <para>If the second parameter of
120     <function>sd_event_add_child()</function> is passed as NULL no
121     reference to the event source object is returned. In this case the
122     event source is considered "floating", and will be destroyed
123     implicitly when the event loop itself is destroyed.</para>
124
125     <para>Note that the <parameter>handler</parameter> function is
126     invoked at a time where the child process is not reaped yet (and
127     thus still is exposed as a zombie process by the kernel). However,
128     the child will be reaped automatically after the function
129     returns. Child processes for which no child process state change
130     event sources are installed will not be reaped by the event loop
131     implementation.</para>
132
133     <para>If both a child process state change event source and a
134     <constant>SIGCHLD</constant> signal event source is installed in
135     the same event loop, the configured event source priorities decide
136     which event source is dispatched first. If the signal handler is
137     processed first, it should leave the child processes for which
138     child process state change event sources are installed unreaped.</para>
139
140     <para><function>sd_event_source_get_child_pid()</function>
141     retrieves the configured PID of a child process state change event
142     source created previously with
143     <function>sd_event_add_child()</function>. It takes the event
144     source object as the <parameter>source</parameter> parameter and a
145     pointer to a <type>pid_t</type> variable to return the process ID
146     in.
147     </para>
148   </refsect1>
149
150   <refsect1>
151     <title>Return Value</title>
152
153     <para>On success, these functions return 0 or a positive
154     integer. On failure, they return a negative errno-style error
155     code.</para>
156   </refsect1>
157
158   <refsect1>
159     <title>Errors</title>
160
161     <para>Returned errors may indicate the following problems:</para>
162
163     <variablelist>
164       <varlistentry>
165         <term><constant>-ENOMEM</constant></term>
166
167         <listitem><para>Not enough memory to allocate an object.</para></listitem>
168       </varlistentry>
169
170       <varlistentry>
171         <term><constant>-EINVAL</constant></term>
172
173         <listitem><para>An invalid argument has been passed. This includes
174         specifying an empty mask in <parameter>options</parameter> or a mask
175         which contains values different than a combination of
176         <constant>WEXITED</constant>, <constant>WSTOPPED</constant>, and
177         <constant>WCONTINUED</constant>.
178         </para></listitem>
179
180       </varlistentry>
181
182       <varlistentry>
183         <term><constant>-EBUSY</constant></term>
184
185         <listitem><para>A handler is already installed for this
186         child process.</para></listitem>
187
188       </varlistentry>
189
190       <varlistentry>
191         <term><constant>-ESTALE</constant></term>
192
193         <listitem><para>The event loop is already terminated.</para></listitem>
194
195       </varlistentry>
196
197       <varlistentry>
198         <term><constant>-ECHILD</constant></term>
199
200         <listitem><para>The event loop has been created in a different process.</para></listitem>
201
202       </varlistentry>
203
204       <varlistentry>
205         <term><constant>-EDOM</constant></term>
206
207         <listitem><para>The passed event source is not a child process event source.</para></listitem>
208       </varlistentry>
209
210     </variablelist>
211   </refsect1>
212
213   <xi:include href="libelogind-pkgconfig.xml" />
214
215   <refsect1>
216     <title>See Also</title>
217
218     <para>
219       <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
220       <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
221       <citerefentry><refentrytitle>sd_event_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
222       <citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
223       <citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
224       <citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
225       <citerefentry><refentrytitle>sd_event_add_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
226       <citerefentry><refentrytitle>sd_event_add_inotify</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
227       <citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
228       <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
229       <citerefentry><refentrytitle>sd_event_source_set_priority</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
230       <citerefentry><refentrytitle>sd_event_source_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
231       <citerefentry><refentrytitle>sd_event_source_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
232       <citerefentry project='man-pages'><refentrytitle>waitid</refentrytitle><manvolnum>2</manvolnum></citerefentry>
233     </para>
234   </refsect1>
235
236 </refentry>