Apache Portable Runtime
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Modules
Pages
include
apr_thread_proc.h
Go to the documentation of this file.
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
#ifndef APR_THREAD_PROC_H
18
#define APR_THREAD_PROC_H
19
20
/**
21
* @file apr_thread_proc.h
22
* @brief APR Thread and Process Library
23
*/
24
25
#include "
apr.h
"
26
#include "
apr_file_io.h
"
27
#include "
apr_pools.h
"
28
#include "
apr_errno.h
"
29
30
#if APR_HAVE_STRUCT_RLIMIT
31
#include <sys/time.h>
32
#include <sys/resource.h>
33
#endif
34
35
#ifdef __cplusplus
36
extern
"C"
{
37
#endif
/* __cplusplus */
38
39
/**
40
* @defgroup apr_thread_proc Threads and Process Functions
41
* @ingroup APR
42
* @{
43
*/
44
45
typedef
enum
{
46
APR_SHELLCMD
,
/**< use the shell to invoke the program */
47
APR_PROGRAM
,
/**< invoke the program directly, no copied env */
48
APR_PROGRAM_ENV
,
/**< invoke the program, replicating our environment */
49
APR_PROGRAM_PATH
,
/**< find program on PATH, use our environment */
50
APR_SHELLCMD_ENV
/**< use the shell to invoke the program,
51
* replicating our environment
52
*/
53
}
apr_cmdtype_e
;
54
55
typedef
enum
{
56
APR_WAIT
,
/**< wait for the specified process to finish */
57
APR_NOWAIT
/**< do not wait -- just see if it has finished */
58
}
apr_wait_how_e
;
59
60
/* I am specifically calling out the values so that the macros below make
61
* more sense. Yes, I know I don't need to, but I am hoping this makes what
62
* I am doing more clear. If you want to add more reasons to exit, continue
63
* to use bitmasks.
64
*/
65
typedef
enum
{
66
APR_PROC_EXIT
= 1,
/**< process exited normally */
67
APR_PROC_SIGNAL
= 2,
/**< process exited due to a signal */
68
APR_PROC_SIGNAL_CORE
= 4
/**< process exited and dumped a core file */
69
}
apr_exit_why_e
;
70
71
/** did we exit the process */
72
#define APR_PROC_CHECK_EXIT(x) (x & APR_PROC_EXIT)
73
/** did we get a signal */
74
#define APR_PROC_CHECK_SIGNALED(x) (x & APR_PROC_SIGNAL)
75
/** did we get core */
76
#define APR_PROC_CHECK_CORE_DUMP(x) (x & APR_PROC_SIGNAL_CORE)
77
78
/** @see apr_procattr_io_set */
79
#define APR_NO_PIPE 0
80
/** @see apr_procattr_io_set and apr_file_pipe_create_ex */
81
#define APR_FULL_BLOCK 1
82
/** @see apr_procattr_io_set and apr_file_pipe_create_ex */
83
#define APR_FULL_NONBLOCK 2
84
/** @see apr_procattr_io_set */
85
#define APR_PARENT_BLOCK 3
86
/** @see apr_procattr_io_set */
87
#define APR_CHILD_BLOCK 4
88
/** @see apr_procattr_io_set */
89
#define APR_NO_FILE 8
90
91
/** @see apr_file_pipe_create_ex */
92
#define APR_READ_BLOCK 3
93
/** @see apr_file_pipe_create_ex */
94
#define APR_WRITE_BLOCK 4
95
96
/** @see apr_procattr_io_set
97
* @note Win32 only effective with version 1.2.12, portably introduced in 1.3.0
98
*/
99
#define APR_NO_FILE 8
100
101
/** @see apr_procattr_limit_set */
102
#define APR_LIMIT_CPU 0
103
/** @see apr_procattr_limit_set */
104
#define APR_LIMIT_MEM 1
105
/** @see apr_procattr_limit_set */
106
#define APR_LIMIT_NPROC 2
107
/** @see apr_procattr_limit_set */
108
#define APR_LIMIT_NOFILE 3
109
110
/**
111
* @defgroup APR_OC Other Child Flags
112
* @{
113
*/
114
#define APR_OC_REASON_DEATH 0
/**< child has died, caller must call
115
* unregister still */
116
#define APR_OC_REASON_UNWRITABLE 1
/**< write_fd is unwritable */
117
#define APR_OC_REASON_RESTART 2
/**< a restart is occurring, perform
118
* any necessary cleanup (including
119
* sending a special signal to child)
120
*/
121
#define APR_OC_REASON_UNREGISTER 3
/**< unregister has been called, do
122
* whatever is necessary (including
123
* kill the child) */
124
#define APR_OC_REASON_LOST 4
/**< somehow the child exited without
125
* us knowing ... buggy os? */
126
#define APR_OC_REASON_RUNNING 5
/**< a health check is occurring,
127
* for most maintainence functions
128
* this is a no-op.
129
*/
130
/** @} */
131
132
/** The APR process type */
133
typedef
struct
apr_proc_t
{
134
/** The process ID */
135
pid_t
pid
;
136
/** Parent's side of pipe to child's stdin */
137
apr_file_t
*
in
;
138
/** Parent's side of pipe to child's stdout */
139
apr_file_t
*
out
;
140
/** Parent's side of pipe to child's stdouterr */
141
apr_file_t
*
err
;
142
#if APR_HAS_PROC_INVOKED || defined(DOXYGEN)
143
/** Diagnositics/debugging string of the command invoked for
144
* this process [only present if APR_HAS_PROC_INVOKED is true]
145
* @remark Only enabled on Win32 by default.
146
* @bug This should either always or never be present in release
147
* builds - since it breaks binary compatibility. We may enable
148
* it always in APR 1.0 yet leave it undefined in most cases.
149
*/
150
char
*
invoked
;
151
#endif
152
#if defined(WIN32) || defined(DOXYGEN)
153
/** (Win32 only) Creator's handle granting access to the process
154
* @remark This handle is closed and reset to NULL in every case
155
* corresponding to a waitpid() on Unix which returns the exit status.
156
* Therefore Win32 correspond's to Unix's zombie reaping characteristics
157
* and avoids potential handle leaks.
158
*/
159
HANDLE
hproc
;
160
#endif
161
}
apr_proc_t
;
162
163
/**
164
* The prototype for APR child errfn functions. (See the description
165
* of apr_procattr_child_errfn_set() for more information.)
166
* It is passed the following parameters:
167
* @param pool Pool associated with the apr_proc_t. If your child
168
* error function needs user data, associate it with this
169
* pool.
170
* @param err APR error code describing the error
171
* @param description Text description of type of processing which failed
172
*/
173
typedef
void (
apr_child_errfn_t
)(
apr_pool_t
*proc,
apr_status_t
err,
174
const
char
*description);
175
176
/** Opaque Thread structure. */
177
typedef
struct
apr_thread_t
apr_thread_t
;
178
179
/** Opaque Thread attributes structure. */
180
typedef
struct
apr_threadattr_t
apr_threadattr_t
;
181
182
/** Opaque Process attributes structure. */
183
typedef
struct
apr_procattr_t
apr_procattr_t
;
184
185
/** Opaque control variable for one-time atomic variables. */
186
typedef
struct
apr_thread_once_t
apr_thread_once_t
;
187
188
/** Opaque thread private address space. */
189
typedef
struct
apr_threadkey_t
apr_threadkey_t
;
190
191
/** Opaque record of child process. */
192
typedef
struct
apr_other_child_rec_t
apr_other_child_rec_t
;
193
194
/**
195
* The prototype for any APR thread worker functions.
196
*/
197
typedef
void
*(
APR_THREAD_FUNC
*
apr_thread_start_t
)(
apr_thread_t
*,
void
*);
198
199
typedef
enum
{
200
APR_KILL_NEVER
,
/**< process is never killed (i.e., never sent
201
* any signals), but it will be reaped if it exits
202
* before the pool is cleaned up */
203
APR_KILL_ALWAYS
,
/**< process is sent SIGKILL on apr_pool_t cleanup */
204
APR_KILL_AFTER_TIMEOUT
,
/**< SIGTERM, wait 3 seconds, SIGKILL */
205
APR_JUST_WAIT
,
/**< wait forever for the process to complete */
206
APR_KILL_ONLY_ONCE
/**< send SIGTERM and then wait */
207
}
apr_kill_conditions_e
;
208
209
/* Thread Function definitions */
210
211
#if APR_HAS_THREADS
212
213
/**
214
* Create and initialize a new threadattr variable
215
* @param new_attr The newly created threadattr.
216
* @param cont The pool to use
217
*/
218
APR_DECLARE
(
apr_status_t
)
apr_threadattr_create
(
apr_threadattr_t
**new_attr,
219
apr_pool_t
*cont);
220
221
/**
222
* Set if newly created threads should be created in detached state.
223
* @param attr The threadattr to affect
224
* @param on Non-zero if detached threads should be created.
225
*/
226
APR_DECLARE
(
apr_status_t
)
apr_threadattr_detach_set
(
apr_threadattr_t
*attr,
227
apr_int32_t on);
228
229
/**
230
* Get the detach state for this threadattr.
231
* @param attr The threadattr to reference
232
* @return APR_DETACH if threads are to be detached, or APR_NOTDETACH
233
* if threads are to be joinable.
234
*/
235
APR_DECLARE
(
apr_status_t
)
apr_threadattr_detach_get
(
apr_threadattr_t
*attr);
236
237
/**
238
* Set the stack size of newly created threads.
239
* @param attr The threadattr to affect
240
* @param stacksize The stack size in bytes
241
*/
242
APR_DECLARE
(
apr_status_t
)
apr_threadattr_stacksize_set
(
apr_threadattr_t
*attr,
243
apr_size_t stacksize);
244
245
/**
246
* Set the stack guard area size of newly created threads.
247
* @param attr The threadattr to affect
248
* @param guardsize The stack guard area size in bytes
249
* @note Thread library implementations commonly use a "guard area"
250
* after each thread's stack which is not readable or writable such that
251
* stack overflows cause a segfault; this consumes e.g. 4K of memory
252
* and increases memory management overhead. Setting the guard area
253
* size to zero hence trades off reliable behaviour on stack overflow
254
* for performance. */
255
APR_DECLARE
(
apr_status_t
)
apr_threadattr_guardsize_set
(
apr_threadattr_t
*attr,
256
apr_size_t guardsize);
257
258
/**
259
* Create a new thread of execution
260
* @param new_thread The newly created thread handle.
261
* @param attr The threadattr to use to determine how to create the thread
262
* @param func The function to start the new thread in
263
* @param data Any data to be passed to the starting function
264
* @param cont The pool to use
265
*/
266
APR_DECLARE
(
apr_status_t
)
apr_thread_create
(
apr_thread_t
**new_thread,
267
apr_threadattr_t
*attr,
268
apr_thread_start_t
func,
269
void
*data,
apr_pool_t
*cont);
270
271
/**
272
* stop the current thread
273
* @param thd The thread to stop
274
* @param retval The return value to pass back to any thread that cares
275
*/
276
APR_DECLARE
(
apr_status_t
)
apr_thread_exit
(
apr_thread_t
*thd,
277
apr_status_t
retval);
278
279
/**
280
* block until the desired thread stops executing.
281
* @param retval The return value from the dead thread.
282
* @param thd The thread to join
283
*/
284
APR_DECLARE
(
apr_status_t
)
apr_thread_join
(
apr_status_t
*retval,
285
apr_thread_t
*thd);
286
287
/**
288
* force the current thread to yield the processor
289
*/
290
APR_DECLARE
(
void
)
apr_thread_yield
(
void
);
291
292
/**
293
* Initialize the control variable for apr_thread_once. If this isn't
294
* called, apr_initialize won't work.
295
* @param control The control variable to initialize
296
* @param p The pool to allocate data from.
297
*/
298
APR_DECLARE
(
apr_status_t
)
apr_thread_once_init
(
apr_thread_once_t
**control,
299
apr_pool_t
*p);
300
301
/**
302
* Run the specified function one time, regardless of how many threads
303
* call it.
304
* @param control The control variable. The same variable should
305
* be passed in each time the function is tried to be
306
* called. This is how the underlying functions determine
307
* if the function has ever been called before.
308
* @param func The function to call.
309
*/
310
APR_DECLARE
(
apr_status_t
)
apr_thread_once
(
apr_thread_once_t
*control,
311
void
(*func)(
void
));
312
313
/**
314
* detach a thread
315
* @param thd The thread to detach
316
*/
317
APR_DECLARE
(
apr_status_t
)
apr_thread_detach
(
apr_thread_t
*thd);
318
319
/**
320
* Return user data associated with the current thread.
321
* @param data The user data associated with the thread.
322
* @param key The key to associate with the data
323
* @param thread The currently open thread.
324
*/
325
APR_DECLARE
(
apr_status_t
)
apr_thread_data_get
(
void
**data, const
char
*key,
326
apr_thread_t
*thread);
327
328
/**
329
* Set user data associated with the current thread.
330
* @param data The user data to associate with the thread.
331
* @param key The key to use for associating the data with the thread
332
* @param cleanup The cleanup routine to use when the thread is destroyed.
333
* @param thread The currently open thread.
334
*/
335
APR_DECLARE
(
apr_status_t
)
apr_thread_data_set
(
void
*data, const
char
*key,
336
apr_status_t
(*cleanup) (
void
*),
337
apr_thread_t
*thread);
338
339
/**
340
* Create and initialize a new thread private address space
341
* @param key The thread private handle.
342
* @param dest The destructor to use when freeing the private memory.
343
* @param cont The pool to use
344
*/
345
APR_DECLARE
(
apr_status_t
)
apr_threadkey_private_create
(
apr_threadkey_t
**key,
346
void
(*dest)(
void
*),
347
apr_pool_t
*cont);
348
349
/**
350
* Get a pointer to the thread private memory
351
* @param new_mem The data stored in private memory
352
* @param key The handle for the desired thread private memory
353
*/
354
APR_DECLARE
(
apr_status_t
)
apr_threadkey_private_get
(
void
**new_mem,
355
apr_threadkey_t
*key);
356
357
/**
358
* Set the data to be stored in thread private memory
359
* @param priv The data to be stored in private memory
360
* @param key The handle for the desired thread private memory
361
*/
362
APR_DECLARE
(
apr_status_t
)
apr_threadkey_private_set
(
void
*priv,
363
apr_threadkey_t
*key);
364
365
/**
366
* Free the thread private memory
367
* @param key The handle for the desired thread private memory
368
*/
369
APR_DECLARE
(
apr_status_t
)
apr_threadkey_private_delete
(
apr_threadkey_t
*key);
370
371
/**
372
* Return the pool associated with the current threadkey.
373
* @param data The user data associated with the threadkey.
374
* @param key The key associated with the data
375
* @param threadkey The currently open threadkey.
376
*/
377
APR_DECLARE
(
apr_status_t
)
apr_threadkey_data_get
(
void
**data, const
char
*key,
378
apr_threadkey_t
*threadkey);
379
380
/**
381
* Return the pool associated with the current threadkey.
382
* @param data The data to set.
383
* @param key The key to associate with the data.
384
* @param cleanup The cleanup routine to use when the file is destroyed.
385
* @param threadkey The currently open threadkey.
386
*/
387
APR_DECLARE
(
apr_status_t
)
apr_threadkey_data_set
(
void
*data, const
char
*key,
388
apr_status_t
(*cleanup) (
void
*),
389
apr_threadkey_t
*threadkey);
390
391
#endif
392
393
/**
394
* Create and initialize a new procattr variable
395
* @param new_attr The newly created procattr.
396
* @param cont The pool to use
397
*/
398
APR_DECLARE
(
apr_status_t
)
apr_procattr_create
(
apr_procattr_t
**new_attr,
399
apr_pool_t
*cont);
400
401
/**
402
* Determine if any of stdin, stdout, or stderr should be linked to pipes
403
* when starting a child process.
404
* @param attr The procattr we care about.
405
* @param in Should stdin be a pipe back to the parent?
406
* @param out Should stdout be a pipe back to the parent?
407
* @param err Should stderr be a pipe back to the parent?
408
* @note If APR_NO_PIPE, there will be no special channel, the child
409
* inherits the parent's corresponding stdio stream. If APR_NO_FILE is
410
* specified, that corresponding stream is closed in the child (and will
411
* be INVALID_HANDLE_VALUE when inspected on Win32). This can have ugly
412
* side effects, as the next file opened in the child on Unix will fall
413
* into the stdio stream fd slot!
414
*/
415
APR_DECLARE
(
apr_status_t
)
apr_procattr_io_set
(
apr_procattr_t
*attr,
416
apr_int32_t in, apr_int32_t out,
417
apr_int32_t err);
418
419
/**
420
* Set the child_in and/or parent_in values to existing apr_file_t values.
421
* @param attr The procattr we care about.
422
* @param child_in apr_file_t value to use as child_in. Must be a valid file.
423
* @param parent_in apr_file_t value to use as parent_in. Must be a valid file.
424
* @remark This is NOT a required initializer function. This is
425
* useful if you have already opened a pipe (or multiple files)
426
* that you wish to use, perhaps persistently across multiple
427
* process invocations - such as a log file. You can save some
428
* extra function calls by not creating your own pipe since this
429
* creates one in the process space for you.
430
* @bug Note that calling this function with two NULL files on some platforms
431
* creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
432
* is it supported. @see apr_procattr_io_set instead for simple pipes.
433
*/
434
APR_DECLARE
(
apr_status_t
)
apr_procattr_child_in_set
(struct
apr_procattr_t
*attr,
435
apr_file_t
*child_in,
436
apr_file_t
*parent_in);
437
438
/**
439
* Set the child_out and parent_out values to existing apr_file_t values.
440
* @param attr The procattr we care about.
441
* @param child_out apr_file_t value to use as child_out. Must be a valid file.
442
* @param parent_out apr_file_t value to use as parent_out. Must be a valid file.
443
* @remark This is NOT a required initializer function. This is
444
* useful if you have already opened a pipe (or multiple files)
445
* that you wish to use, perhaps persistently across multiple
446
* process invocations - such as a log file.
447
* @bug Note that calling this function with two NULL files on some platforms
448
* creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
449
* is it supported. @see apr_procattr_io_set instead for simple pipes.
450
*/
451
APR_DECLARE
(
apr_status_t
)
apr_procattr_child_out_set
(struct
apr_procattr_t
*attr,
452
apr_file_t
*child_out,
453
apr_file_t
*parent_out);
454
455
/**
456
* Set the child_err and parent_err values to existing apr_file_t values.
457
* @param attr The procattr we care about.
458
* @param child_err apr_file_t value to use as child_err. Must be a valid file.
459
* @param parent_err apr_file_t value to use as parent_err. Must be a valid file.
460
* @remark This is NOT a required initializer function. This is
461
* useful if you have already opened a pipe (or multiple files)
462
* that you wish to use, perhaps persistently across multiple
463
* process invocations - such as a log file.
464
* @bug Note that calling this function with two NULL files on some platforms
465
* creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
466
* is it supported. @see apr_procattr_io_set instead for simple pipes.
467
*/
468
APR_DECLARE
(
apr_status_t
)
apr_procattr_child_err_set
(struct
apr_procattr_t
*attr,
469
apr_file_t
*child_err,
470
apr_file_t
*parent_err);
471
472
/**
473
* Set which directory the child process should start executing in.
474
* @param attr The procattr we care about.
475
* @param dir Which dir to start in. By default, this is the same dir as
476
* the parent currently resides in, when the createprocess call
477
* is made.
478
*/
479
APR_DECLARE
(
apr_status_t
)
apr_procattr_dir_set
(
apr_procattr_t
*attr,
480
const
char
*dir);
481
482
/**
483
* Set what type of command the child process will call.
484
* @param attr The procattr we care about.
485
* @param cmd The type of command. One of:
486
* <PRE>
487
* APR_SHELLCMD -- Anything that the shell can handle
488
* APR_PROGRAM -- Executable program (default)
489
* APR_PROGRAM_ENV -- Executable program, copy environment
490
* APR_PROGRAM_PATH -- Executable program on PATH, copy env
491
* </PRE>
492
*/
493
APR_DECLARE
(
apr_status_t
)
apr_procattr_cmdtype_set
(
apr_procattr_t
*attr,
494
apr_cmdtype_e
cmd);
495
496
/**
497
* Determine if the child should start in detached state.
498
* @param attr The procattr we care about.
499
* @param detach Should the child start in detached state? Default is no.
500
*/
501
APR_DECLARE
(
apr_status_t
)
apr_procattr_detach_set
(
apr_procattr_t
*attr,
502
apr_int32_t detach);
503
504
#if APR_HAVE_STRUCT_RLIMIT
505
/**
506
* Set the Resource Utilization limits when starting a new process.
507
* @param attr The procattr we care about.
508
* @param what Which limit to set, one of:
509
* <PRE>
510
* APR_LIMIT_CPU
511
* APR_LIMIT_MEM
512
* APR_LIMIT_NPROC
513
* APR_LIMIT_NOFILE
514
* </PRE>
515
* @param limit Value to set the limit to.
516
*/
517
APR_DECLARE
(
apr_status_t
) apr_procattr_limit_set(
apr_procattr_t
*attr,
518
apr_int32_t what,
519
struct rlimit *limit);
520
#endif
521
522
/**
523
* Specify an error function to be called in the child process if APR
524
* encounters an error in the child prior to running the specified program.
525
* @param attr The procattr describing the child process to be created.
526
* @param errfn The function to call in the child process.
527
* @remark At the present time, it will only be called from apr_proc_create()
528
* on platforms where fork() is used. It will never be called on other
529
* platforms, on those platforms apr_proc_create() will return the error
530
* in the parent process rather than invoke the callback in the now-forked
531
* child process.
532
*/
533
APR_DECLARE
(
apr_status_t
)
apr_procattr_child_errfn_set
(
apr_procattr_t
*attr,
534
apr_child_errfn_t
*errfn);
535
536
/**
537
* Specify that apr_proc_create() should do whatever it can to report
538
* failures to the caller of apr_proc_create(), rather than find out in
539
* the child.
540
* @param attr The procattr describing the child process to be created.
541
* @param chk Flag to indicate whether or not extra work should be done
542
* to try to report failures to the caller.
543
* @remark This flag only affects apr_proc_create() on platforms where
544
* fork() is used. This leads to extra overhead in the calling
545
* process, but that may help the application handle such
546
* errors more gracefully.
547