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