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