Debian Codegraph @

upstream/debian-unstable/man3/ASYNC_start_job.3ssl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "ASYNC_START_JOB 3SSL"
58 .TH ASYNC_START_JOB 3SSL 2026-04-07 3.6.2 OpenSSL
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 ASYNC_get_wait_ctx,
65 ASYNC_init_thread, ASYNC_cleanup_thread, ASYNC_start_job, ASYNC_pause_job,
66 ASYNC_get_current_job, ASYNC_block_pause, ASYNC_unblock_pause, ASYNC_is_capable,
67 ASYNC_stack_alloc_fn, ASYNC_stack_free_fn, ASYNC_set_mem_functions, ASYNC_get_mem_functions
68 \&\- asynchronous job management functions
69 .SH SYNOPSIS
70 .IX Header "SYNOPSIS"
71 .Vb 1
72 \& #include <openssl/async.h>
73 \&
74 \& int ASYNC_init_thread(size_t max_size, size_t init_size);
75 \& void ASYNC_cleanup_thread(void);
76 \&
77 \& int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *ctx, int *ret,
78 \& int (*func)(void *), void *args, size_t size);
79 \& int ASYNC_pause_job(void);
80 \&
81 \& ASYNC_JOB *ASYNC_get_current_job(void);
82 \& ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job);
83 \& void ASYNC_block_pause(void);
84 \& void ASYNC_unblock_pause(void);
85 \&
86 \& int ASYNC_is_capable(void);
87 \&
88 \& typedef void *(*ASYNC_stack_alloc_fn)(size_t *num);
89 \& typedef void (*ASYNC_stack_free_fn)(void *addr);
90 \& int ASYNC_set_mem_functions(ASYNC_stack_alloc_fn alloc_fn,
91 \& ASYNC_stack_free_fn free_fn);
92 \& void ASYNC_get_mem_functions(ASYNC_stack_alloc_fn *alloc_fn,
93 \& ASYNC_stack_free_fn *free_fn);
94 .Ve
95 .SH DESCRIPTION
96 .IX Header "DESCRIPTION"
97 OpenSSL implements asynchronous capabilities through an \fBASYNC_JOB\fR. This
98 represents code that can be started and executes until some event occurs. At
99 that point the code can be paused and control returns to user code until some
100 subsequent event indicates that the job can be resumed. It's OpenSSL
101 specific implementation of cooperative multitasking.
102 .PP
103 The creation of an \fBASYNC_JOB\fR is a relatively expensive operation. Therefore,
104 for efficiency reasons, jobs can be created up front and reused many times. They
105 are held in a pool until they are needed, at which point they are removed from
106 the pool, used, and then returned to the pool when the job completes. If the
107 user application is multi-threaded, then \fBASYNC_init_thread()\fR may be called for
108 each thread that will initiate asynchronous jobs. Before
109 user code exits per-thread resources need to be cleaned up. This will normally
110 occur automatically (see \fBOPENSSL_init_crypto\fR\|(3)) but may be explicitly
111 initiated by using \fBASYNC_cleanup_thread()\fR. No asynchronous jobs must be
112 outstanding for the thread when \fBASYNC_cleanup_thread()\fR is called. Failing to
113 ensure this will result in memory leaks.
114 .PP
115 The \fImax_size\fR argument limits the number of \fBASYNC_JOB\fRs that will be held in
116 the pool. If \fImax_size\fR is set to 0 then no upper limit is set. When an
117 \&\fBASYNC_JOB\fR is needed but there are none available in the pool already then one
118 will be automatically created, as long as the total of \fBASYNC_JOB\fRs managed by
119 the pool does not exceed \fImax_size\fR. When the pool is first initialised
120 \&\fIinit_size\fR \fBASYNC_JOB\fRs will be created immediately. If \fBASYNC_init_thread()\fR
121 is not called before the pool is first used then it will be called automatically
122 with a \fImax_size\fR of 0 (no upper limit) and an \fIinit_size\fR of 0 (no
123 \&\fBASYNC_JOB\fRs created up front).
124 .PP
125 An asynchronous job is started by calling the \fBASYNC_start_job()\fR function.
126 Initially \fI*job\fR should be NULL. \fIctx\fR should point to an \fBASYNC_WAIT_CTX\fR
127 object created through the \fBASYNC_WAIT_CTX_new\fR\|(3) function. \fIret\fR should
128 point to a location where the return value of the asynchronous function should
129 be stored on completion of the job. \fIfunc\fR represents the function that should
130 be started asynchronously. The data pointed to by \fIargs\fR and of size \fIsize\fR
131 will be copied and then passed as an argument to \fIfunc\fR when the job starts.
132 ASYNC_start_job will return one of the following values:
133 .IP \fBASYNC_ERR\fR 4
134 .IX Item "ASYNC_ERR"
135 An error occurred trying to start the job. Check the OpenSSL error queue (e.g.
136 see \fBERR_print_errors\fR\|(3)) for more details.
137 .IP \fBASYNC_NO_JOBS\fR 4
138 .IX Item "ASYNC_NO_JOBS"
139 There are no jobs currently available in the pool. This call can be retried
140 again at a later time.
141 .IP \fBASYNC_PAUSE\fR 4
142 .IX Item "ASYNC_PAUSE"
143 The job was successfully started but was "paused" before it completed (see
144 \&\fBASYNC_pause_job()\fR below). A handle to the job is placed in \fI*job\fR. Other work
145 can be performed (if desired) and the job restarted at a later time. To restart
146 a job call \fBASYNC_start_job()\fR again passing the job handle in \fI*job\fR. The
147 \&\fIfunc\fR, \fIargs\fR and \fIsize\fR parameters will be ignored when restarting a job.
148 When restarting a job \fBASYNC_start_job()\fR \fBmust\fR be called from the same thread
149 that the job was originally started from. \fBASYNC_WAIT_CTX\fR is used to
150 know when a job is ready to be restarted.
151 .IP \fBASYNC_FINISH\fR 4
152 .IX Item "ASYNC_FINISH"
153 The job completed. \fI*job\fR will be NULL and the return value from \fIfunc\fR will
154 be placed in \fI*ret\fR.
155 .PP
156 At any one time there can be a maximum of one job actively running per thread
157 (you can have many that are paused). \fBASYNC_get_current_job()\fR can be used to get
158 a pointer to the currently executing \fBASYNC_JOB\fR. If no job is currently
159 executing then this will return NULL.
160 .PP
161 If executing within the context of a job (i.e. having been called directly or
162 indirectly by the function "func" passed as an argument to \fBASYNC_start_job()\fR)
163 then \fBASYNC_pause_job()\fR will immediately return control to the calling
164 application with \fBASYNC_PAUSE\fR returned from the \fBASYNC_start_job()\fR call. A
165 subsequent call to ASYNC_start_job passing in the relevant \fBASYNC_JOB\fR in the
166 \&\fI*job\fR parameter will resume execution from the \fBASYNC_pause_job()\fR call. If
167 \&\fBASYNC_pause_job()\fR is called whilst not within the context of a job then no
168 action is taken and \fBASYNC_pause_job()\fR returns immediately.
169 .PP
170 \&\fBASYNC_get_wait_ctx()\fR can be used to get a pointer to the \fBASYNC_WAIT_CTX\fR
171 for the \fIjob\fR (see \fBASYNC_WAIT_CTX_new\fR\|(3)).
172 \&\fBASYNC_WAIT_CTX\fRs contain two different ways to notify
173 applications that a job is ready to be resumed. One is a "wait" file
174 descriptor, and the other is a "callback" mechanism.
175 .PP
176 The "wait" file descriptor associated with \fBASYNC_WAIT_CTX\fR is used for
177 applications to wait for the file descriptor to be ready for "read" using a
178 system function call such as \fBselect\fR\|(2) or \fBpoll\fR\|(2) (being ready for "read"
179 indicates
180 that the job should be resumed). If no file descriptor is made available then
181 an application will have to periodically "poll" the job by attempting to restart
182 it to see if it is ready to continue.
183 .PP
184 \&\fBASYNC_WAIT_CTX\fRs also have a "callback" mechanism to notify applications. The
185 callback is set by an application, and it will be automatically called when an
186 engine completes a cryptography operation, so that the application can resume
187 the paused work flow without polling. An engine could be written to look whether
188 the callback has been set. If it has then it would use the callback mechanism
189 in preference to the file descriptor notifications. If a callback is not set
190 then the engine may use file descriptor based notifications. Please note that
191 not all engines may support the callback mechanism, so the callback may not be
192 used even if it has been set. See \fBASYNC_WAIT_CTX_new()\fR for more details.
193 .PP
194 The \fBASYNC_block_pause()\fR function will prevent the currently active job from
195 pausing. The block will remain in place until a subsequent call to
196 \&\fBASYNC_unblock_pause()\fR. These functions can be nested, e.g. if you call
197 \&\fBASYNC_block_pause()\fR twice then you must call \fBASYNC_unblock_pause()\fR twice in
198 order to re-enable pausing. If these functions are called while there is no
199 currently active job then they have no effect. This functionality can be useful
200 to avoid deadlock scenarios. For example during the execution of an \fBASYNC_JOB\fR
201 an application acquires a lock. It then calls some cryptographic function which
202 invokes \fBASYNC_pause_job()\fR. This returns control back to the code that created
203 the \fBASYNC_JOB\fR. If that code then attempts to acquire the same lock before
204 resuming the original job then a deadlock can occur. By calling
205 \&\fBASYNC_block_pause()\fR immediately after acquiring the lock and
206 \&\fBASYNC_unblock_pause()\fR immediately before releasing it then this situation cannot
207 occur.
208 .PP
209 Some platforms cannot support async operations. The \fBASYNC_is_capable()\fR function
210 can be used to detect whether the current platform is async capable or not.
211 .PP
212 Custom memory allocation functions are supported for the POSIX platform.
213 Custom memory allocation functions allow alternative methods of allocating
214 stack memory such as mmap, or using stack memory from the current thread.
215 Using an ASYNC_stack_alloc_fn callback also allows manipulation of the stack
216 size, which defaults to 32k.
217 The stack size can be altered by allocating a stack of a size different to
218 the requested size, and passing back the new stack size in the callback's \fI*num\fR
219 parameter.
220 .SH "RETURN VALUES"
221 .IX Header "RETURN VALUES"
222 ASYNC_init_thread returns 1 on success or 0 otherwise.
223 .PP
224 ASYNC_start_job returns one of \fBASYNC_ERR\fR, \fBASYNC_NO_JOBS\fR, \fBASYNC_PAUSE\fR or
225 \&\fBASYNC_FINISH\fR as described above.
226 .PP
227 ASYNC_pause_job returns 0 if an error occurred or 1 on success. If called when
228 not within the context of an \fBASYNC_JOB\fR then this is counted as success so 1
229 is returned.
230 .PP
231 ASYNC_get_current_job returns a pointer to the currently executing \fBASYNC_JOB\fR
232 or NULL if not within the context of a job.
233 .PP
234 \&\fBASYNC_get_wait_ctx()\fR returns a pointer to the \fBASYNC_WAIT_CTX\fR for the job.
235 .PP
236 \&\fBASYNC_is_capable()\fR returns 1 if the current platform is async capable or 0
237 otherwise.
238 .PP
239 ASYNC_set_mem_functions returns 1 if custom stack allocators are supported by
240 the current platform and no allocations have already occurred or 0 otherwise.
241 .SH NOTES
242 .IX Header "NOTES"
243 On Windows platforms the \fI<openssl/async.h>\fR header is dependent on some
244 of the types customarily made available by including \fI<windows.h>\fR. The
245 application developer is likely to require control over when the latter
246 is included, commonly as one of the first included headers. Therefore,
247 it is defined as an application developer's responsibility to include
248 \&\fI<windows.h>\fR prior to \fI<openssl/async.h>\fR.
249 .SH EXAMPLES
250 .IX Header "EXAMPLES"
251 The following example demonstrates how to use most of the core async APIs:
252 .PP
253 .Vb 7
254 \& #ifdef _WIN32
255 \& # include <windows.h>
256 \& #endif
257 \& #include <stdio.h>
258 \& #include <unistd.h>
259 \& #include <openssl/async.h>
260 \& #include <openssl/crypto.h>
261 \&
262 \& int unique = 0;
263 \&
264 \& void cleanup(ASYNC_WAIT_CTX *ctx, const void *key, OSSL_ASYNC_FD r, void *vw)
265 \& {
266 \& OSSL_ASYNC_FD *w = (OSSL_ASYNC_FD *)vw;
267 \&
268 \& close(r);
269 \& close(*w);
270 \& OPENSSL_free(w);
271 \& }
272 \&
273 \& int jobfunc(void *arg)
274 \& {
275 \& ASYNC_JOB *currjob;
276 \& unsigned char *msg;
277 \& int pipefds[2] = {0, 0};
278 \& OSSL_ASYNC_FD *wptr;
279 \& char buf = \*(AqX\*(Aq;
280 \&
281 \& currjob = ASYNC_get_current_job();
282 \& if (currjob != NULL) {
283 \& printf("Executing within a job\en");
284 \& } else {
285 \& printf("Not executing within a job \- should not happen\en");
286 \& return 0;
287 \& }
288 \&
289 \& msg = (unsigned char *)arg;
290 \& printf("Passed in message is: %s\en", msg);
291 \&
292 \& /*
293 \& * Create a way to inform the calling thread when this job is ready
294 \& * to resume, in this example we\*(Aqre using file descriptors.
295 \& * For offloading the task to an asynchronous ENGINE it\*(Aqs not necessary,
296 \& * the ENGINE should handle that internally.
297 \& */
298 \&
299 \& if (pipe(pipefds) != 0) {
300 \& printf("Failed to create pipe\en");
301 \& return 0;
302 \& }
303 \& wptr = OPENSSL_malloc(sizeof(OSSL_ASYNC_FD));
304 \& if (wptr == NULL) {
305 \& printf("Failed to malloc\en");
306 \& return 0;
307 \& }
308 \& *wptr = pipefds[1];
309 \& ASYNC_WAIT_CTX_set_wait_fd(ASYNC_get_wait_ctx(currjob), &unique,
310 \& pipefds[0], wptr, cleanup);
311 \&
312 \& /*
313 \& * Normally some external event (like a network read being ready,
314 \& * disk access being finished, or some hardware offload operation
315 \& * completing) would cause this to happen at some
316 \& * later point \- but we do it here for demo purposes, i.e.
317 \& * immediately signalling that the job is ready to be woken up after
318 \& * we return to main via ASYNC_pause_job().
319 \& */
320 \& write(pipefds[1], &buf, 1);
321 \&
322 \& /*
323 \& * Return control back to main just before calling a blocking
324 \& * method. The main thread will wait until pipefds[0] is ready
325 \& * for reading before returning control to this thread.
326 \& */
327 \& ASYNC_pause_job();
328 \&
329 \& /* Perform the blocking call (it won\*(Aqt block with this example code) */
330 \& read(pipefds[0], &buf, 1);
331 \&
332 \& printf ("Resumed the job after a pause\en");
333 \&
334 \& return 1;
335 \& }
336 \&
337 \& int main(void)
338 \& {
339 \& ASYNC_JOB *job = NULL;
340 \& ASYNC_WAIT_CTX *ctx = NULL;
341 \& int ret;
342 \& OSSL_ASYNC_FD waitfd;
343 \& fd_set waitfdset;
344 \& size_t numfds;
345 \& unsigned char msg[13] = "Hello world!";
346 \&
347 \& printf("Starting...\en");
348 \&
349 \& ctx = ASYNC_WAIT_CTX_new();
350 \& if (ctx == NULL) {
351 \& printf("Failed to create ASYNC_WAIT_CTX\en");
352 \& abort();
353 \& }
354 \&
355 \& for (;;) {
356 \& switch (ASYNC_start_job(&job, ctx, &ret, jobfunc, msg, sizeof(msg))) {
357 \& case ASYNC_ERR:
358 \& case ASYNC_NO_JOBS:
359 \& printf("An error occurred\en");
360 \& goto end;
361 \& case ASYNC_PAUSE:
362 \& printf("Job was paused\en");
363 \& break;
364 \& case ASYNC_FINISH:
365 \& printf("Job finished with return value %d\en", ret);
366 \& goto end;
367 \& }
368 \&
369 \& /* Get the file descriptor we can use to wait for the job
370 \& * to be ready to be woken up
371 \& */
372 \& printf("Waiting for the job to be woken up\en");
373 \&
374 \& if (!ASYNC_WAIT_CTX_get_all_fds(ctx, NULL, &numfds)
375 \& || numfds > 1) {
376 \& printf("Unexpected number of fds\en");
377 \& abort();
378 \& }
379 \& ASYNC_WAIT_CTX_get_all_fds(ctx, &waitfd, &numfds);
380 \& FD_ZERO(&waitfdset);
381 \& FD_SET(waitfd, &waitfdset);
382 \&
383 \& /* Wait for the job to be ready for wakeup */
384 \& select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
385 \& }
386 \&
387 \& end:
388 \& ASYNC_WAIT_CTX_free(ctx);
389 \& printf("Finishing\en");
390 \&
391 \& return 0;
392 \& }
393 .Ve
394 .PP
395 The expected output from executing the above example program is:
396 .PP
397 .Vb 8
398 \& Starting...
399 \& Executing within a job
400 \& Passed in message is: Hello world!
401 \& Job was paused
402 \& Waiting for the job to be woken up
403 \& Resumed the job after a pause
404 \& Job finished with return value 1
405 \& Finishing
406 .Ve
407 .SH "SEE ALSO"
408 .IX Header "SEE ALSO"
409 \&\fBcrypto\fR\|(7), \fBERR_print_errors\fR\|(3)
410 .SH HISTORY
411 .IX Header "HISTORY"
412 ASYNC_init_thread, ASYNC_cleanup_thread,
413 ASYNC_start_job, ASYNC_pause_job, ASYNC_get_current_job, \fBASYNC_get_wait_ctx()\fR,
414 \&\fBASYNC_block_pause()\fR, \fBASYNC_unblock_pause()\fR and \fBASYNC_is_capable()\fR were first
415 added in OpenSSL 1.1.0.
416 \&\fBASYNC_set_mem_functions()\fR, \fBASYNC_get_mem_functions()\fR were added
417 in OpenSSL 3.2.
418 .SH COPYRIGHT
419 .IX Header "COPYRIGHT"
420 Copyright 2015\-2024 The OpenSSL Project Authors. All Rights Reserved.
421 .PP
422 Licensed under the Apache License 2.0 (the "License"). You may not use
423 this file except in compliance with the License. You can obtain a copy
424 in the file LICENSE in the source distribution or at
425 <https://www.openssl.org/source/license.html>.