1*4724848cSchristos=pod 2*4724848cSchristos 3*4724848cSchristos=head1 NAME 4*4724848cSchristos 5*4724848cSchristosASYNC_get_wait_ctx, 6*4724848cSchristosASYNC_init_thread, ASYNC_cleanup_thread, ASYNC_start_job, ASYNC_pause_job, 7*4724848cSchristosASYNC_get_current_job, ASYNC_block_pause, ASYNC_unblock_pause, ASYNC_is_capable 8*4724848cSchristos- asynchronous job management functions 9*4724848cSchristos 10*4724848cSchristos=head1 SYNOPSIS 11*4724848cSchristos 12*4724848cSchristos #include <openssl/async.h> 13*4724848cSchristos 14*4724848cSchristos int ASYNC_init_thread(size_t max_size, size_t init_size); 15*4724848cSchristos void ASYNC_cleanup_thread(void); 16*4724848cSchristos 17*4724848cSchristos int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *ctx, int *ret, 18*4724848cSchristos int (*func)(void *), void *args, size_t size); 19*4724848cSchristos int ASYNC_pause_job(void); 20*4724848cSchristos 21*4724848cSchristos ASYNC_JOB *ASYNC_get_current_job(void); 22*4724848cSchristos ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job); 23*4724848cSchristos void ASYNC_block_pause(void); 24*4724848cSchristos void ASYNC_unblock_pause(void); 25*4724848cSchristos 26*4724848cSchristos int ASYNC_is_capable(void); 27*4724848cSchristos 28*4724848cSchristos=head1 DESCRIPTION 29*4724848cSchristos 30*4724848cSchristosOpenSSL implements asynchronous capabilities through an ASYNC_JOB. This 31*4724848cSchristosrepresents code that can be started and executes until some event occurs. At 32*4724848cSchristosthat point the code can be paused and control returns to user code until some 33*4724848cSchristossubsequent event indicates that the job can be resumed. 34*4724848cSchristos 35*4724848cSchristosThe creation of an ASYNC_JOB is a relatively expensive operation. Therefore, for 36*4724848cSchristosefficiency reasons, jobs can be created up front and reused many times. They are 37*4724848cSchristosheld in a pool until they are needed, at which point they are removed from the 38*4724848cSchristospool, used, and then returned to the pool when the job completes. If the user 39*4724848cSchristosapplication is multi-threaded, then ASYNC_init_thread() may be called for each 40*4724848cSchristosthread that will initiate asynchronous jobs. Before 41*4724848cSchristosuser code exits per-thread resources need to be cleaned up. This will normally 42*4724848cSchristosoccur automatically (see L<OPENSSL_init_crypto(3)>) but may be explicitly 43*4724848cSchristosinitiated by using ASYNC_cleanup_thread(). No asynchronous jobs must be 44*4724848cSchristosoutstanding for the thread when ASYNC_cleanup_thread() is called. Failing to 45*4724848cSchristosensure this will result in memory leaks. 46*4724848cSchristos 47*4724848cSchristosThe B<max_size> argument limits the number of ASYNC_JOBs that will be held in 48*4724848cSchristosthe pool. If B<max_size> is set to 0 then no upper limit is set. When an 49*4724848cSchristosASYNC_JOB is needed but there are none available in the pool already then one 50*4724848cSchristoswill be automatically created, as long as the total of ASYNC_JOBs managed by the 51*4724848cSchristospool does not exceed B<max_size>. When the pool is first initialised 52*4724848cSchristosB<init_size> ASYNC_JOBs will be created immediately. If ASYNC_init_thread() is 53*4724848cSchristosnot called before the pool is first used then it will be called automatically 54*4724848cSchristoswith a B<max_size> of 0 (no upper limit) and an B<init_size> of 0 (no ASYNC_JOBs 55*4724848cSchristoscreated up front). 56*4724848cSchristos 57*4724848cSchristosAn asynchronous job is started by calling the ASYNC_start_job() function. 58*4724848cSchristosInitially B<*job> should be NULL. B<ctx> should point to an ASYNC_WAIT_CTX 59*4724848cSchristosobject created through the L<ASYNC_WAIT_CTX_new(3)> function. B<ret> should 60*4724848cSchristospoint to a location where the return value of the asynchronous function should 61*4724848cSchristosbe stored on completion of the job. B<func> represents the function that should 62*4724848cSchristosbe started asynchronously. The data pointed to by B<args> and of size B<size> 63*4724848cSchristoswill be copied and then passed as an argument to B<func> when the job starts. 64*4724848cSchristosASYNC_start_job will return one of the following values: 65*4724848cSchristos 66*4724848cSchristos=over 4 67*4724848cSchristos 68*4724848cSchristos=item B<ASYNC_ERR> 69*4724848cSchristos 70*4724848cSchristosAn error occurred trying to start the job. Check the OpenSSL error queue (e.g. 71*4724848cSchristossee L<ERR_print_errors(3)>) for more details. 72*4724848cSchristos 73*4724848cSchristos=item B<ASYNC_NO_JOBS> 74*4724848cSchristos 75*4724848cSchristosThere are no jobs currently available in the pool. This call can be retried 76*4724848cSchristosagain at a later time. 77*4724848cSchristos 78*4724848cSchristos=item B<ASYNC_PAUSE> 79*4724848cSchristos 80*4724848cSchristosThe job was successfully started but was "paused" before it completed (see 81*4724848cSchristosASYNC_pause_job() below). A handle to the job is placed in B<*job>. Other work 82*4724848cSchristoscan be performed (if desired) and the job restarted at a later time. To restart 83*4724848cSchristosa job call ASYNC_start_job() again passing the job handle in B<*job>. The 84*4724848cSchristosB<func>, B<args> and B<size> parameters will be ignored when restarting a job. 85*4724848cSchristosWhen restarting a job ASYNC_start_job() B<must> be called from the same thread 86*4724848cSchristosthat the job was originally started from. 87*4724848cSchristos 88*4724848cSchristos=item B<ASYNC_FINISH> 89*4724848cSchristos 90*4724848cSchristosThe job completed. B<*job> will be NULL and the return value from B<func> will 91*4724848cSchristosbe placed in B<*ret>. 92*4724848cSchristos 93*4724848cSchristos=back 94*4724848cSchristos 95*4724848cSchristosAt any one time there can be a maximum of one job actively running per thread 96*4724848cSchristos(you can have many that are paused). ASYNC_get_current_job() can be used to get 97*4724848cSchristosa pointer to the currently executing ASYNC_JOB. If no job is currently executing 98*4724848cSchristosthen this will return NULL. 99*4724848cSchristos 100*4724848cSchristosIf executing within the context of a job (i.e. having been called directly or 101*4724848cSchristosindirectly by the function "func" passed as an argument to ASYNC_start_job()) 102*4724848cSchristosthen ASYNC_pause_job() will immediately return control to the calling 103*4724848cSchristosapplication with ASYNC_PAUSE returned from the ASYNC_start_job() call. A 104*4724848cSchristossubsequent call to ASYNC_start_job passing in the relevant ASYNC_JOB in the 105*4724848cSchristosB<*job> parameter will resume execution from the ASYNC_pause_job() call. If 106*4724848cSchristosASYNC_pause_job() is called whilst not within the context of a job then no 107*4724848cSchristosaction is taken and ASYNC_pause_job() returns immediately. 108*4724848cSchristos 109*4724848cSchristosASYNC_get_wait_ctx() can be used to get a pointer to the ASYNC_WAIT_CTX 110*4724848cSchristosfor the B<job>. ASYNC_WAIT_CTXs can have a "wait" file descriptor associated 111*4724848cSchristoswith them. Applications can wait for the file descriptor to be ready for "read" 112*4724848cSchristosusing a system function call such as select or poll (being ready for "read" 113*4724848cSchristosindicates that the job should be resumed). If no file descriptor is made 114*4724848cSchristosavailable then an application will have to periodically "poll" the job by 115*4724848cSchristosattempting to restart it to see if it is ready to continue. 116*4724848cSchristos 117*4724848cSchristosAn example of typical usage might be an async capable engine. User code would 118*4724848cSchristosinitiate cryptographic operations. The engine would initiate those operations 119*4724848cSchristosasynchronously and then call L<ASYNC_WAIT_CTX_set_wait_fd(3)> followed by 120*4724848cSchristosASYNC_pause_job() to return control to the user code. The user code can then 121*4724848cSchristosperform other tasks or wait for the job to be ready by calling "select" or other 122*4724848cSchristossimilar function on the wait file descriptor. The engine can signal to the user 123*4724848cSchristoscode that the job should be resumed by making the wait file descriptor 124*4724848cSchristos"readable". Once resumed the engine should clear the wake signal on the wait 125*4724848cSchristosfile descriptor. 126*4724848cSchristos 127*4724848cSchristosThe ASYNC_block_pause() function will prevent the currently active job from 128*4724848cSchristospausing. The block will remain in place until a subsequent call to 129*4724848cSchristosASYNC_unblock_pause(). These functions can be nested, e.g. if you call 130*4724848cSchristosASYNC_block_pause() twice then you must call ASYNC_unblock_pause() twice in 131*4724848cSchristosorder to re-enable pausing. If these functions are called while there is no 132*4724848cSchristoscurrently active job then they have no effect. This functionality can be useful 133*4724848cSchristosto avoid deadlock scenarios. For example during the execution of an ASYNC_JOB an 134*4724848cSchristosapplication acquires a lock. It then calls some cryptographic function which 135*4724848cSchristosinvokes ASYNC_pause_job(). This returns control back to the code that created 136*4724848cSchristosthe ASYNC_JOB. If that code then attempts to acquire the same lock before 137*4724848cSchristosresuming the original job then a deadlock can occur. By calling 138*4724848cSchristosASYNC_block_pause() immediately after acquiring the lock and 139*4724848cSchristosASYNC_unblock_pause() immediately before releasing it then this situation cannot 140*4724848cSchristosoccur. 141*4724848cSchristos 142*4724848cSchristosSome platforms cannot support async operations. The ASYNC_is_capable() function 143*4724848cSchristoscan be used to detect whether the current platform is async capable or not. 144*4724848cSchristos 145*4724848cSchristos=head1 RETURN VALUES 146*4724848cSchristos 147*4724848cSchristosASYNC_init_thread returns 1 on success or 0 otherwise. 148*4724848cSchristos 149*4724848cSchristosASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or 150*4724848cSchristosASYNC_FINISH as described above. 151*4724848cSchristos 152*4724848cSchristosASYNC_pause_job returns 0 if an error occurred or 1 on success. If called when 153*4724848cSchristosnot within the context of an ASYNC_JOB then this is counted as success so 1 is 154*4724848cSchristosreturned. 155*4724848cSchristos 156*4724848cSchristosASYNC_get_current_job returns a pointer to the currently executing ASYNC_JOB or 157*4724848cSchristosNULL if not within the context of a job. 158*4724848cSchristos 159*4724848cSchristosASYNC_get_wait_ctx() returns a pointer to the ASYNC_WAIT_CTX for the job. 160*4724848cSchristos 161*4724848cSchristosASYNC_is_capable() returns 1 if the current platform is async capable or 0 162*4724848cSchristosotherwise. 163*4724848cSchristos 164*4724848cSchristos=head1 NOTES 165*4724848cSchristos 166*4724848cSchristosOn Windows platforms the openssl/async.h header is dependent on some 167*4724848cSchristosof the types customarily made available by including windows.h. The 168*4724848cSchristosapplication developer is likely to require control over when the latter 169*4724848cSchristosis included, commonly as one of the first included headers. Therefore, 170*4724848cSchristosit is defined as an application developer's responsibility to include 171*4724848cSchristoswindows.h prior to async.h. 172*4724848cSchristos 173*4724848cSchristos=head1 EXAMPLES 174*4724848cSchristos 175*4724848cSchristosThe following example demonstrates how to use most of the core async APIs: 176*4724848cSchristos 177*4724848cSchristos #ifdef _WIN32 178*4724848cSchristos # include <windows.h> 179*4724848cSchristos #endif 180*4724848cSchristos #include <stdio.h> 181*4724848cSchristos #include <unistd.h> 182*4724848cSchristos #include <openssl/async.h> 183*4724848cSchristos #include <openssl/crypto.h> 184*4724848cSchristos 185*4724848cSchristos int unique = 0; 186*4724848cSchristos 187*4724848cSchristos void cleanup(ASYNC_WAIT_CTX *ctx, const void *key, OSSL_ASYNC_FD r, void *vw) 188*4724848cSchristos { 189*4724848cSchristos OSSL_ASYNC_FD *w = (OSSL_ASYNC_FD *)vw; 190*4724848cSchristos 191*4724848cSchristos close(r); 192*4724848cSchristos close(*w); 193*4724848cSchristos OPENSSL_free(w); 194*4724848cSchristos } 195*4724848cSchristos 196*4724848cSchristos int jobfunc(void *arg) 197*4724848cSchristos { 198*4724848cSchristos ASYNC_JOB *currjob; 199*4724848cSchristos unsigned char *msg; 200*4724848cSchristos int pipefds[2] = {0, 0}; 201*4724848cSchristos OSSL_ASYNC_FD *wptr; 202*4724848cSchristos char buf = 'X'; 203*4724848cSchristos 204*4724848cSchristos currjob = ASYNC_get_current_job(); 205*4724848cSchristos if (currjob != NULL) { 206*4724848cSchristos printf("Executing within a job\n"); 207*4724848cSchristos } else { 208*4724848cSchristos printf("Not executing within a job - should not happen\n"); 209*4724848cSchristos return 0; 210*4724848cSchristos } 211*4724848cSchristos 212*4724848cSchristos msg = (unsigned char *)arg; 213*4724848cSchristos printf("Passed in message is: %s\n", msg); 214*4724848cSchristos 215*4724848cSchristos if (pipe(pipefds) != 0) { 216*4724848cSchristos printf("Failed to create pipe\n"); 217*4724848cSchristos return 0; 218*4724848cSchristos } 219*4724848cSchristos wptr = OPENSSL_malloc(sizeof(OSSL_ASYNC_FD)); 220*4724848cSchristos if (wptr == NULL) { 221*4724848cSchristos printf("Failed to malloc\n"); 222*4724848cSchristos return 0; 223*4724848cSchristos } 224*4724848cSchristos *wptr = pipefds[1]; 225*4724848cSchristos ASYNC_WAIT_CTX_set_wait_fd(ASYNC_get_wait_ctx(currjob), &unique, 226*4724848cSchristos pipefds[0], wptr, cleanup); 227*4724848cSchristos 228*4724848cSchristos /* 229*4724848cSchristos * Normally some external event would cause this to happen at some 230*4724848cSchristos * later point - but we do it here for demo purposes, i.e. 231*4724848cSchristos * immediately signalling that the job is ready to be woken up after 232*4724848cSchristos * we return to main via ASYNC_pause_job(). 233*4724848cSchristos */ 234*4724848cSchristos write(pipefds[1], &buf, 1); 235*4724848cSchristos 236*4724848cSchristos /* Return control back to main */ 237*4724848cSchristos ASYNC_pause_job(); 238*4724848cSchristos 239*4724848cSchristos /* Clear the wake signal */ 240*4724848cSchristos read(pipefds[0], &buf, 1); 241*4724848cSchristos 242*4724848cSchristos printf ("Resumed the job after a pause\n"); 243*4724848cSchristos 244*4724848cSchristos return 1; 245*4724848cSchristos } 246*4724848cSchristos 247*4724848cSchristos int main(void) 248*4724848cSchristos { 249*4724848cSchristos ASYNC_JOB *job = NULL; 250*4724848cSchristos ASYNC_WAIT_CTX *ctx = NULL; 251*4724848cSchristos int ret; 252*4724848cSchristos OSSL_ASYNC_FD waitfd; 253*4724848cSchristos fd_set waitfdset; 254*4724848cSchristos size_t numfds; 255*4724848cSchristos unsigned char msg[13] = "Hello world!"; 256*4724848cSchristos 257*4724848cSchristos printf("Starting...\n"); 258*4724848cSchristos 259*4724848cSchristos ctx = ASYNC_WAIT_CTX_new(); 260*4724848cSchristos if (ctx == NULL) { 261*4724848cSchristos printf("Failed to create ASYNC_WAIT_CTX\n"); 262*4724848cSchristos abort(); 263*4724848cSchristos } 264*4724848cSchristos 265*4724848cSchristos for (;;) { 266*4724848cSchristos switch (ASYNC_start_job(&job, ctx, &ret, jobfunc, msg, sizeof(msg))) { 267*4724848cSchristos case ASYNC_ERR: 268*4724848cSchristos case ASYNC_NO_JOBS: 269*4724848cSchristos printf("An error occurred\n"); 270*4724848cSchristos goto end; 271*4724848cSchristos case ASYNC_PAUSE: 272*4724848cSchristos printf("Job was paused\n"); 273*4724848cSchristos break; 274*4724848cSchristos case ASYNC_FINISH: 275*4724848cSchristos printf("Job finished with return value %d\n", ret); 276*4724848cSchristos goto end; 277*4724848cSchristos } 278*4724848cSchristos 279*4724848cSchristos /* Wait for the job to be woken */ 280*4724848cSchristos printf("Waiting for the job to be woken up\n"); 281*4724848cSchristos 282*4724848cSchristos if (!ASYNC_WAIT_CTX_get_all_fds(ctx, NULL, &numfds) 283*4724848cSchristos || numfds > 1) { 284*4724848cSchristos printf("Unexpected number of fds\n"); 285*4724848cSchristos abort(); 286*4724848cSchristos } 287*4724848cSchristos ASYNC_WAIT_CTX_get_all_fds(ctx, &waitfd, &numfds); 288*4724848cSchristos FD_ZERO(&waitfdset); 289*4724848cSchristos FD_SET(waitfd, &waitfdset); 290*4724848cSchristos select(waitfd + 1, &waitfdset, NULL, NULL, NULL); 291*4724848cSchristos } 292*4724848cSchristos 293*4724848cSchristos end: 294*4724848cSchristos ASYNC_WAIT_CTX_free(ctx); 295*4724848cSchristos printf("Finishing\n"); 296*4724848cSchristos 297*4724848cSchristos return 0; 298*4724848cSchristos } 299*4724848cSchristos 300*4724848cSchristosThe expected output from executing the above example program is: 301*4724848cSchristos 302*4724848cSchristos Starting... 303*4724848cSchristos Executing within a job 304*4724848cSchristos Passed in message is: Hello world! 305*4724848cSchristos Job was paused 306*4724848cSchristos Waiting for the job to be woken up 307*4724848cSchristos Resumed the job after a pause 308*4724848cSchristos Job finished with return value 1 309*4724848cSchristos Finishing 310*4724848cSchristos 311*4724848cSchristos=head1 SEE ALSO 312*4724848cSchristos 313*4724848cSchristosL<crypto(7)>, L<ERR_print_errors(3)> 314*4724848cSchristos 315*4724848cSchristos=head1 HISTORY 316*4724848cSchristos 317*4724848cSchristosASYNC_init_thread, ASYNC_cleanup_thread, 318*4724848cSchristosASYNC_start_job, ASYNC_pause_job, ASYNC_get_current_job, ASYNC_get_wait_ctx(), 319*4724848cSchristosASYNC_block_pause(), ASYNC_unblock_pause() and ASYNC_is_capable() were first 320*4724848cSchristosadded in OpenSSL 1.1.0. 321*4724848cSchristos 322*4724848cSchristos=head1 COPYRIGHT 323*4724848cSchristos 324*4724848cSchristosCopyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved. 325*4724848cSchristos 326*4724848cSchristosLicensed under the OpenSSL license (the "License"). You may not use 327*4724848cSchristosthis file except in compliance with the License. You can obtain a copy 328*4724848cSchristosin the file LICENSE in the source distribution or at 329*4724848cSchristosL<https://www.openssl.org/source/license.html>. 330*4724848cSchristos 331*4724848cSchristos=cut 332