]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - secure/lib/libcrypto/man/man3/ASYNC_start_job.3
MFC: r359486
[FreeBSD/FreeBSD.git] / secure / lib / libcrypto / man / man3 / ASYNC_start_job.3
1 .\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.40)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 .    ds C`
42 .    ds C'
43 'br\}
44 .\"
45 .\" Escape single quotes in literal strings from groff's Unicode transform.
46 .ie \n(.g .ds Aq \(aq
47 .el       .ds Aq '
48 .\"
49 .\" If the F register is >0, we'll generate index entries on stderr for
50 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
51 .\" entries marked with X<> in POD.  Of course, you'll have to process the
52 .\" output yourself in some meaningful fashion.
53 .\"
54 .\" Avoid warning from groff about undefined register 'F'.
55 .de IX
56 ..
57 .nr rF 0
58 .if \n(.g .if rF .nr rF 1
59 .if (\n(rF:(\n(.g==0)) \{\
60 .    if \nF \{\
61 .        de IX
62 .        tm Index:\\$1\t\\n%\t"\\$2"
63 ..
64 .        if !\nF==2 \{\
65 .            nr % 0
66 .            nr F 2
67 .        \}
68 .    \}
69 .\}
70 .rr rF
71 .\"
72 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
73 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
74 .    \" fudge factors for nroff and troff
75 .if n \{\
76 .    ds #H 0
77 .    ds #V .8m
78 .    ds #F .3m
79 .    ds #[ \f1
80 .    ds #] \fP
81 .\}
82 .if t \{\
83 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
84 .    ds #V .6m
85 .    ds #F 0
86 .    ds #[ \&
87 .    ds #] \&
88 .\}
89 .    \" simple accents for nroff and troff
90 .if n \{\
91 .    ds ' \&
92 .    ds ` \&
93 .    ds ^ \&
94 .    ds , \&
95 .    ds ~ ~
96 .    ds /
97 .\}
98 .if t \{\
99 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
100 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
101 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
102 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
103 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
104 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
105 .\}
106 .    \" troff and (daisy-wheel) nroff accents
107 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
108 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
109 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
110 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
111 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
112 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
113 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
114 .ds ae a\h'-(\w'a'u*4/10)'e
115 .ds Ae A\h'-(\w'A'u*4/10)'E
116 .    \" corrections for vroff
117 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
118 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
119 .    \" for low resolution devices (crt and lpr)
120 .if \n(.H>23 .if \n(.V>19 \
121 \{\
122 .    ds : e
123 .    ds 8 ss
124 .    ds o a
125 .    ds d- d\h'-1'\(ga
126 .    ds D- D\h'-1'\(hy
127 .    ds th \o'bp'
128 .    ds Th \o'LP'
129 .    ds ae ae
130 .    ds Ae AE
131 .\}
132 .rm #[ #] #H #V #F C
133 .\" ========================================================================
134 .\"
135 .IX Title "ASYNC_START_JOB 3"
136 .TH ASYNC_START_JOB 3 "2020-03-31" "1.1.1f" "OpenSSL"
137 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
138 .\" way too many mistakes in technical documents.
139 .if n .ad l
140 .nh
141 .SH "NAME"
142 ASYNC_get_wait_ctx, ASYNC_init_thread, ASYNC_cleanup_thread, ASYNC_start_job, ASYNC_pause_job, ASYNC_get_current_job, ASYNC_block_pause, ASYNC_unblock_pause, ASYNC_is_capable \&\- asynchronous job management functions
143 .SH "SYNOPSIS"
144 .IX Header "SYNOPSIS"
145 .Vb 1
146 \& #include <openssl/async.h>
147 \&
148 \& int ASYNC_init_thread(size_t max_size, size_t init_size);
149 \& void ASYNC_cleanup_thread(void);
150 \&
151 \& int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *ctx, int *ret,
152 \&                     int (*func)(void *), void *args, size_t size);
153 \& int ASYNC_pause_job(void);
154 \&
155 \& ASYNC_JOB *ASYNC_get_current_job(void);
156 \& ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job);
157 \& void ASYNC_block_pause(void);
158 \& void ASYNC_unblock_pause(void);
159 \&
160 \& int ASYNC_is_capable(void);
161 .Ve
162 .SH "DESCRIPTION"
163 .IX Header "DESCRIPTION"
164 OpenSSL implements asynchronous capabilities through an \s-1ASYNC_JOB.\s0 This
165 represents code that can be started and executes until some event occurs. At
166 that point the code can be paused and control returns to user code until some
167 subsequent event indicates that the job can be resumed.
168 .PP
169 The creation of an \s-1ASYNC_JOB\s0 is a relatively expensive operation. Therefore, for
170 efficiency reasons, jobs can be created up front and reused many times. They are
171 held in a pool until they are needed, at which point they are removed from the
172 pool, used, and then returned to the pool when the job completes. If the user
173 application is multi-threaded, then \fBASYNC_init_thread()\fR may be called for each
174 thread that will initiate asynchronous jobs. Before
175 user code exits per-thread resources need to be cleaned up. This will normally
176 occur automatically (see \fBOPENSSL_init_crypto\fR\|(3)) but may be explicitly
177 initiated by using \fBASYNC_cleanup_thread()\fR. No asynchronous jobs must be
178 outstanding for the thread when \fBASYNC_cleanup_thread()\fR is called. Failing to
179 ensure this will result in memory leaks.
180 .PP
181 The \fBmax_size\fR argument limits the number of ASYNC_JOBs that will be held in
182 the pool. If \fBmax_size\fR is set to 0 then no upper limit is set. When an
183 \&\s-1ASYNC_JOB\s0 is needed but there are none available in the pool already then one
184 will be automatically created, as long as the total of ASYNC_JOBs managed by the
185 pool does not exceed \fBmax_size\fR. When the pool is first initialised
186 \&\fBinit_size\fR ASYNC_JOBs will be created immediately. If \fBASYNC_init_thread()\fR is
187 not called before the pool is first used then it will be called automatically
188 with a \fBmax_size\fR of 0 (no upper limit) and an \fBinit_size\fR of 0 (no ASYNC_JOBs
189 created up front).
190 .PP
191 An asynchronous job is started by calling the \fBASYNC_start_job()\fR function.
192 Initially \fB*job\fR should be \s-1NULL.\s0 \fBctx\fR should point to an \s-1ASYNC_WAIT_CTX\s0
193 object created through the \fBASYNC_WAIT_CTX_new\fR\|(3) function. \fBret\fR should
194 point to a location where the return value of the asynchronous function should
195 be stored on completion of the job. \fBfunc\fR represents the function that should
196 be started asynchronously. The data pointed to by \fBargs\fR and of size \fBsize\fR
197 will be copied and then passed as an argument to \fBfunc\fR when the job starts.
198 ASYNC_start_job will return one of the following values:
199 .IP "\fB\s-1ASYNC_ERR\s0\fR" 4
200 .IX Item "ASYNC_ERR"
201 An error occurred trying to start the job. Check the OpenSSL error queue (e.g.
202 see \fBERR_print_errors\fR\|(3)) for more details.
203 .IP "\fB\s-1ASYNC_NO_JOBS\s0\fR" 4
204 .IX Item "ASYNC_NO_JOBS"
205 There are no jobs currently available in the pool. This call can be retried
206 again at a later time.
207 .IP "\fB\s-1ASYNC_PAUSE\s0\fR" 4
208 .IX Item "ASYNC_PAUSE"
209 The job was successfully started but was \*(L"paused\*(R" before it completed (see
210 \&\fBASYNC_pause_job()\fR below). A handle to the job is placed in \fB*job\fR. Other work
211 can be performed (if desired) and the job restarted at a later time. To restart
212 a job call \fBASYNC_start_job()\fR again passing the job handle in \fB*job\fR. The
213 \&\fBfunc\fR, \fBargs\fR and \fBsize\fR parameters will be ignored when restarting a job.
214 When restarting a job \fBASYNC_start_job()\fR \fBmust\fR be called from the same thread
215 that the job was originally started from.
216 .IP "\fB\s-1ASYNC_FINISH\s0\fR" 4
217 .IX Item "ASYNC_FINISH"
218 The job completed. \fB*job\fR will be \s-1NULL\s0 and the return value from \fBfunc\fR will
219 be placed in \fB*ret\fR.
220 .PP
221 At any one time there can be a maximum of one job actively running per thread
222 (you can have many that are paused). \fBASYNC_get_current_job()\fR can be used to get
223 a pointer to the currently executing \s-1ASYNC_JOB.\s0 If no job is currently executing
224 then this will return \s-1NULL.\s0
225 .PP
226 If executing within the context of a job (i.e. having been called directly or
227 indirectly by the function \*(L"func\*(R" passed as an argument to \fBASYNC_start_job()\fR)
228 then \fBASYNC_pause_job()\fR will immediately return control to the calling
229 application with \s-1ASYNC_PAUSE\s0 returned from the \fBASYNC_start_job()\fR call. A
230 subsequent call to ASYNC_start_job passing in the relevant \s-1ASYNC_JOB\s0 in the
231 \&\fB*job\fR parameter will resume execution from the \fBASYNC_pause_job()\fR call. If
232 \&\fBASYNC_pause_job()\fR is called whilst not within the context of a job then no
233 action is taken and \fBASYNC_pause_job()\fR returns immediately.
234 .PP
235 \&\fBASYNC_get_wait_ctx()\fR can be used to get a pointer to the \s-1ASYNC_WAIT_CTX\s0
236 for the \fBjob\fR. ASYNC_WAIT_CTXs can have a \*(L"wait\*(R" file descriptor associated
237 with them. Applications can wait for the file descriptor to be ready for \*(L"read\*(R"
238 using a system function call such as select or poll (being ready for \*(L"read\*(R"
239 indicates that the job should be resumed). If no file descriptor is made
240 available then an application will have to periodically \*(L"poll\*(R" the job by
241 attempting to restart it to see if it is ready to continue.
242 .PP
243 An example of typical usage might be an async capable engine. User code would
244 initiate cryptographic operations. The engine would initiate those operations
245 asynchronously and then call \fBASYNC_WAIT_CTX_set_wait_fd\fR\|(3) followed by
246 \&\fBASYNC_pause_job()\fR to return control to the user code. The user code can then
247 perform other tasks or wait for the job to be ready by calling \*(L"select\*(R" or other
248 similar function on the wait file descriptor. The engine can signal to the user
249 code that the job should be resumed by making the wait file descriptor
250 \&\*(L"readable\*(R". Once resumed the engine should clear the wake signal on the wait
251 file descriptor.
252 .PP
253 The \fBASYNC_block_pause()\fR function will prevent the currently active job from
254 pausing. The block will remain in place until a subsequent call to
255 \&\fBASYNC_unblock_pause()\fR. These functions can be nested, e.g. if you call
256 \&\fBASYNC_block_pause()\fR twice then you must call \fBASYNC_unblock_pause()\fR twice in
257 order to re-enable pausing. If these functions are called while there is no
258 currently active job then they have no effect. This functionality can be useful
259 to avoid deadlock scenarios. For example during the execution of an \s-1ASYNC_JOB\s0 an
260 application acquires a lock. It then calls some cryptographic function which
261 invokes \fBASYNC_pause_job()\fR. This returns control back to the code that created
262 the \s-1ASYNC_JOB.\s0 If that code then attempts to acquire the same lock before
263 resuming the original job then a deadlock can occur. By calling
264 \&\fBASYNC_block_pause()\fR immediately after acquiring the lock and
265 \&\fBASYNC_unblock_pause()\fR immediately before releasing it then this situation cannot
266 occur.
267 .PP
268 Some platforms cannot support async operations. The \fBASYNC_is_capable()\fR function
269 can be used to detect whether the current platform is async capable or not.
270 .SH "RETURN VALUES"
271 .IX Header "RETURN VALUES"
272 ASYNC_init_thread returns 1 on success or 0 otherwise.
273 .PP
274 ASYNC_start_job returns one of \s-1ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE\s0 or
275 \&\s-1ASYNC_FINISH\s0 as described above.
276 .PP
277 ASYNC_pause_job returns 0 if an error occurred or 1 on success. If called when
278 not within the context of an \s-1ASYNC_JOB\s0 then this is counted as success so 1 is
279 returned.
280 .PP
281 ASYNC_get_current_job returns a pointer to the currently executing \s-1ASYNC_JOB\s0 or
282 \&\s-1NULL\s0 if not within the context of a job.
283 .PP
284 \&\fBASYNC_get_wait_ctx()\fR returns a pointer to the \s-1ASYNC_WAIT_CTX\s0 for the job.
285 .PP
286 \&\fBASYNC_is_capable()\fR returns 1 if the current platform is async capable or 0
287 otherwise.
288 .SH "NOTES"
289 .IX Header "NOTES"
290 On Windows platforms the openssl/async.h header is dependent on some
291 of the types customarily made available by including windows.h. The
292 application developer is likely to require control over when the latter
293 is included, commonly as one of the first included headers. Therefore
294 it is defined as an application developer's responsibility to include
295 windows.h prior to async.h.
296 .SH "EXAMPLES"
297 .IX Header "EXAMPLES"
298 The following example demonstrates how to use most of the core async APIs:
299 .PP
300 .Vb 7
301 \& #ifdef _WIN32
302 \& # include <windows.h>
303 \& #endif
304 \& #include <stdio.h>
305 \& #include <unistd.h>
306 \& #include <openssl/async.h>
307 \& #include <openssl/crypto.h>
308 \&
309 \& int unique = 0;
310 \&
311 \& void cleanup(ASYNC_WAIT_CTX *ctx, const void *key, OSSL_ASYNC_FD r, void *vw)
312 \& {
313 \&     OSSL_ASYNC_FD *w = (OSSL_ASYNC_FD *)vw;
314 \&
315 \&     close(r);
316 \&     close(*w);
317 \&     OPENSSL_free(w);
318 \& }
319 \&
320 \& int jobfunc(void *arg)
321 \& {
322 \&     ASYNC_JOB *currjob;
323 \&     unsigned char *msg;
324 \&     int pipefds[2] = {0, 0};
325 \&     OSSL_ASYNC_FD *wptr;
326 \&     char buf = \*(AqX\*(Aq;
327 \&
328 \&     currjob = ASYNC_get_current_job();
329 \&     if (currjob != NULL) {
330 \&         printf("Executing within a job\en");
331 \&     } else {
332 \&         printf("Not executing within a job \- should not happen\en");
333 \&         return 0;
334 \&     }
335 \&
336 \&     msg = (unsigned char *)arg;
337 \&     printf("Passed in message is: %s\en", msg);
338 \&
339 \&     if (pipe(pipefds) != 0) {
340 \&         printf("Failed to create pipe\en");
341 \&         return 0;
342 \&     }
343 \&     wptr = OPENSSL_malloc(sizeof(OSSL_ASYNC_FD));
344 \&     if (wptr == NULL) {
345 \&         printf("Failed to malloc\en");
346 \&         return 0;
347 \&     }
348 \&     *wptr = pipefds[1];
349 \&     ASYNC_WAIT_CTX_set_wait_fd(ASYNC_get_wait_ctx(currjob), &unique,
350 \&                                pipefds[0], wptr, cleanup);
351 \&
352 \&     /*
353 \&      * Normally some external event would cause this to happen at some
354 \&      * later point \- but we do it here for demo purposes, i.e.
355 \&      * immediately signalling that the job is ready to be woken up after
356 \&      * we return to main via ASYNC_pause_job().
357 \&      */
358 \&     write(pipefds[1], &buf, 1);
359 \&
360 \&     /* Return control back to main */
361 \&     ASYNC_pause_job();
362 \&
363 \&     /* Clear the wake signal */
364 \&     read(pipefds[0], &buf, 1);
365 \&
366 \&     printf ("Resumed the job after a pause\en");
367 \&
368 \&     return 1;
369 \& }
370 \&
371 \& int main(void)
372 \& {
373 \&     ASYNC_JOB *job = NULL;
374 \&     ASYNC_WAIT_CTX *ctx = NULL;
375 \&     int ret;
376 \&     OSSL_ASYNC_FD waitfd;
377 \&     fd_set waitfdset;
378 \&     size_t numfds;
379 \&     unsigned char msg[13] = "Hello world!";
380 \&
381 \&     printf("Starting...\en");
382 \&
383 \&     ctx = ASYNC_WAIT_CTX_new();
384 \&     if (ctx == NULL) {
385 \&         printf("Failed to create ASYNC_WAIT_CTX\en");
386 \&         abort();
387 \&     }
388 \&
389 \&     for (;;) {
390 \&         switch (ASYNC_start_job(&job, ctx, &ret, jobfunc, msg, sizeof(msg))) {
391 \&         case ASYNC_ERR:
392 \&         case ASYNC_NO_JOBS:
393 \&             printf("An error occurred\en");
394 \&             goto end;
395 \&         case ASYNC_PAUSE:
396 \&             printf("Job was paused\en");
397 \&             break;
398 \&         case ASYNC_FINISH:
399 \&             printf("Job finished with return value %d\en", ret);
400 \&             goto end;
401 \&         }
402 \&
403 \&         /* Wait for the job to be woken */
404 \&         printf("Waiting for the job to be woken up\en");
405 \&
406 \&         if (!ASYNC_WAIT_CTX_get_all_fds(ctx, NULL, &numfds)
407 \&                 || numfds > 1) {
408 \&             printf("Unexpected number of fds\en");
409 \&             abort();
410 \&         }
411 \&         ASYNC_WAIT_CTX_get_all_fds(ctx, &waitfd, &numfds);
412 \&         FD_ZERO(&waitfdset);
413 \&         FD_SET(waitfd, &waitfdset);
414 \&         select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
415 \&     }
416 \&
417 \& end:
418 \&     ASYNC_WAIT_CTX_free(ctx);
419 \&     printf("Finishing\en");
420 \&
421 \&     return 0;
422 \& }
423 .Ve
424 .PP
425 The expected output from executing the above example program is:
426 .PP
427 .Vb 8
428 \& Starting...
429 \& Executing within a job
430 \& Passed in message is: Hello world!
431 \& Job was paused
432 \& Waiting for the job to be woken up
433 \& Resumed the job after a pause
434 \& Job finished with return value 1
435 \& Finishing
436 .Ve
437 .SH "SEE ALSO"
438 .IX Header "SEE ALSO"
439 \&\fBcrypto\fR\|(7), \fBERR_print_errors\fR\|(3)
440 .SH "HISTORY"
441 .IX Header "HISTORY"
442 ASYNC_init_thread, ASYNC_cleanup_thread,
443 ASYNC_start_job, ASYNC_pause_job, ASYNC_get_current_job, \fBASYNC_get_wait_ctx()\fR,
444 \&\fBASYNC_block_pause()\fR, \fBASYNC_unblock_pause()\fR and \fBASYNC_is_capable()\fR were first
445 added in OpenSSL 1.1.0.
446 .SH "COPYRIGHT"
447 .IX Header "COPYRIGHT"
448 Copyright 2015\-2019 The OpenSSL Project Authors. All Rights Reserved.
449 .PP
450 Licensed under the OpenSSL license (the \*(L"License\*(R").  You may not use
451 this file except in compliance with the License.  You can obtain a copy
452 in the file \s-1LICENSE\s0 in the source distribution or at
453 <https://www.openssl.org/source/license.html>.