Blame doc/man3/ASYNC_start_job.pod

Packit c4476c
=pod
Packit c4476c
Packit c4476c
=head1 NAME
Packit c4476c
Packit c4476c
ASYNC_get_wait_ctx,
Packit c4476c
ASYNC_init_thread, ASYNC_cleanup_thread, ASYNC_start_job, ASYNC_pause_job,
Packit c4476c
ASYNC_get_current_job, ASYNC_block_pause, ASYNC_unblock_pause, ASYNC_is_capable
Packit c4476c
- asynchronous job management functions
Packit c4476c
Packit c4476c
=head1 SYNOPSIS
Packit c4476c
Packit c4476c
 #include <openssl/async.h>
Packit c4476c
Packit c4476c
 int ASYNC_init_thread(size_t max_size, size_t init_size);
Packit c4476c
 void ASYNC_cleanup_thread(void);
Packit c4476c
Packit c4476c
 int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *ctx, int *ret,
Packit c4476c
                     int (*func)(void *), void *args, size_t size);
Packit c4476c
 int ASYNC_pause_job(void);
Packit c4476c
Packit c4476c
 ASYNC_JOB *ASYNC_get_current_job(void);
Packit c4476c
 ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job);
Packit c4476c
 void ASYNC_block_pause(void);
Packit c4476c
 void ASYNC_unblock_pause(void);
Packit c4476c
Packit c4476c
 int ASYNC_is_capable(void);
Packit c4476c
Packit c4476c
=head1 DESCRIPTION
Packit c4476c
Packit c4476c
OpenSSL implements asynchronous capabilities through an ASYNC_JOB. This
Packit c4476c
represents code that can be started and executes until some event occurs. At
Packit c4476c
that point the code can be paused and control returns to user code until some
Packit c4476c
subsequent event indicates that the job can be resumed.
Packit c4476c
Packit c4476c
The creation of an ASYNC_JOB is a relatively expensive operation. Therefore, for
Packit c4476c
efficiency reasons, jobs can be created up front and reused many times. They are
Packit c4476c
held in a pool until they are needed, at which point they are removed from the
Packit c4476c
pool, used, and then returned to the pool when the job completes. If the user
Packit c4476c
application is multi-threaded, then ASYNC_init_thread() may be called for each
Packit c4476c
thread that will initiate asynchronous jobs. Before
Packit c4476c
user code exits per-thread resources need to be cleaned up. This will normally
Packit c4476c
occur automatically (see L<OPENSSL_init_crypto(3)>) but may be explicitly
Packit c4476c
initiated by using ASYNC_cleanup_thread(). No asynchronous jobs must be
Packit c4476c
outstanding for the thread when ASYNC_cleanup_thread() is called. Failing to
Packit c4476c
ensure this will result in memory leaks.
Packit c4476c
Packit c4476c
The B<max_size> argument limits the number of ASYNC_JOBs that will be held in
Packit c4476c
the pool. If B<max_size> is set to 0 then no upper limit is set. When an
Packit c4476c
ASYNC_JOB is needed but there are none available in the pool already then one
Packit c4476c
will be automatically created, as long as the total of ASYNC_JOBs managed by the
Packit c4476c
pool does not exceed B<max_size>. When the pool is first initialised
Packit c4476c
B<init_size> ASYNC_JOBs will be created immediately. If ASYNC_init_thread() is
Packit c4476c
not called before the pool is first used then it will be called automatically
Packit c4476c
with a B<max_size> of 0 (no upper limit) and an B<init_size> of 0 (no ASYNC_JOBs
Packit c4476c
created up front).
Packit c4476c
Packit c4476c
An asynchronous job is started by calling the ASYNC_start_job() function.
Packit c4476c
Initially B<*job> should be NULL. B<ctx> should point to an ASYNC_WAIT_CTX
Packit c4476c
object created through the L<ASYNC_WAIT_CTX_new(3)> function. B<ret> should
Packit c4476c
point to a location where the return value of the asynchronous function should
Packit c4476c
be stored on completion of the job. B<func> represents the function that should
Packit c4476c
be started asynchronously. The data pointed to by B<args> and of size B<size>
Packit c4476c
will be copied and then passed as an argument to B<func> when the job starts.
Packit c4476c
ASYNC_start_job will return one of the following values:
Packit c4476c
Packit c4476c
=over 4
Packit c4476c
Packit c4476c
=item B<ASYNC_ERR>
Packit c4476c
Packit c4476c
An error occurred trying to start the job. Check the OpenSSL error queue (e.g.
Packit c4476c
see L<ERR_print_errors(3)>) for more details.
Packit c4476c
Packit c4476c
=item B<ASYNC_NO_JOBS>
Packit c4476c
Packit c4476c
There are no jobs currently available in the pool. This call can be retried
Packit c4476c
again at a later time.
Packit c4476c
Packit c4476c
=item B<ASYNC_PAUSE>
Packit c4476c
Packit c4476c
The job was successfully started but was "paused" before it completed (see
Packit c4476c
ASYNC_pause_job() below). A handle to the job is placed in B<*job>. Other work
Packit c4476c
can be performed (if desired) and the job restarted at a later time. To restart
Packit c4476c
a job call ASYNC_start_job() again passing the job handle in B<*job>. The
Packit c4476c
B<func>, B<args> and B<size> parameters will be ignored when restarting a job.
Packit c4476c
When restarting a job ASYNC_start_job() B<must> be called from the same thread
Packit c4476c
that the job was originally started from.
Packit c4476c
Packit c4476c
=item B<ASYNC_FINISH>
Packit c4476c
Packit c4476c
The job completed. B<*job> will be NULL and the return value from B<func> will
Packit c4476c
be placed in B<*ret>.
Packit c4476c
Packit c4476c
=back
Packit c4476c
Packit c4476c
At any one time there can be a maximum of one job actively running per thread
Packit c4476c
(you can have many that are paused). ASYNC_get_current_job() can be used to get
Packit c4476c
a pointer to the currently executing ASYNC_JOB. If no job is currently executing
Packit c4476c
then this will return NULL.
Packit c4476c
Packit c4476c
If executing within the context of a job (i.e. having been called directly or
Packit c4476c
indirectly by the function "func" passed as an argument to ASYNC_start_job())
Packit c4476c
then ASYNC_pause_job() will immediately return control to the calling
Packit c4476c
application with ASYNC_PAUSE returned from the ASYNC_start_job() call. A
Packit c4476c
subsequent call to ASYNC_start_job passing in the relevant ASYNC_JOB in the
Packit c4476c
B<*job> parameter will resume execution from the ASYNC_pause_job() call. If
Packit c4476c
ASYNC_pause_job() is called whilst not within the context of a job then no
Packit c4476c
action is taken and ASYNC_pause_job() returns immediately.
Packit c4476c
Packit c4476c
ASYNC_get_wait_ctx() can be used to get a pointer to the ASYNC_WAIT_CTX
Packit c4476c
for the B<job>. ASYNC_WAIT_CTXs can have a "wait" file descriptor associated
Packit c4476c
with them. Applications can wait for the file descriptor to be ready for "read"
Packit c4476c
using a system function call such as select or poll (being ready for "read"
Packit c4476c
indicates that the job should be resumed). If no file descriptor is made
Packit c4476c
available then an application will have to periodically "poll" the job by
Packit c4476c
attempting to restart it to see if it is ready to continue.
Packit c4476c
Packit c4476c
An example of typical usage might be an async capable engine. User code would
Packit c4476c
initiate cryptographic operations. The engine would initiate those operations
Packit c4476c
asynchronously and then call L<ASYNC_WAIT_CTX_set_wait_fd(3)> followed by
Packit c4476c
ASYNC_pause_job() to return control to the user code. The user code can then
Packit c4476c
perform other tasks or wait for the job to be ready by calling "select" or other
Packit c4476c
similar function on the wait file descriptor. The engine can signal to the user
Packit c4476c
code that the job should be resumed by making the wait file descriptor
Packit c4476c
"readable". Once resumed the engine should clear the wake signal on the wait
Packit c4476c
file descriptor.
Packit c4476c
Packit c4476c
The ASYNC_block_pause() function will prevent the currently active job from
Packit c4476c
pausing. The block will remain in place until a subsequent call to
Packit c4476c
ASYNC_unblock_pause(). These functions can be nested, e.g. if you call
Packit c4476c
ASYNC_block_pause() twice then you must call ASYNC_unblock_pause() twice in
Packit c4476c
order to re-enable pausing. If these functions are called while there is no
Packit c4476c
currently active job then they have no effect. This functionality can be useful
Packit c4476c
to avoid deadlock scenarios. For example during the execution of an ASYNC_JOB an
Packit c4476c
application acquires a lock. It then calls some cryptographic function which
Packit c4476c
invokes ASYNC_pause_job(). This returns control back to the code that created
Packit c4476c
the ASYNC_JOB. If that code then attempts to acquire the same lock before
Packit c4476c
resuming the original job then a deadlock can occur. By calling
Packit c4476c
ASYNC_block_pause() immediately after acquiring the lock and
Packit c4476c
ASYNC_unblock_pause() immediately before releasing it then this situation cannot
Packit c4476c
occur.
Packit c4476c
Packit c4476c
Some platforms cannot support async operations. The ASYNC_is_capable() function
Packit c4476c
can be used to detect whether the current platform is async capable or not.
Packit c4476c
Packit c4476c
=head1 RETURN VALUES
Packit c4476c
Packit c4476c
ASYNC_init_thread returns 1 on success or 0 otherwise.
Packit c4476c
Packit c4476c
ASYNC_start_job returns one of ASYNC_ERR, ASYNC_NO_JOBS, ASYNC_PAUSE or
Packit c4476c
ASYNC_FINISH as described above.
Packit c4476c
Packit c4476c
ASYNC_pause_job returns 0 if an error occurred or 1 on success. If called when
Packit c4476c
not within the context of an ASYNC_JOB then this is counted as success so 1 is
Packit c4476c
returned.
Packit c4476c
Packit c4476c
ASYNC_get_current_job returns a pointer to the currently executing ASYNC_JOB or
Packit c4476c
NULL if not within the context of a job.
Packit c4476c
Packit c4476c
ASYNC_get_wait_ctx() returns a pointer to the ASYNC_WAIT_CTX for the job.
Packit c4476c
Packit c4476c
ASYNC_is_capable() returns 1 if the current platform is async capable or 0
Packit c4476c
otherwise.
Packit c4476c
Packit c4476c
=head1 NOTES
Packit c4476c
Packit c4476c
On Windows platforms the openssl/async.h header is dependent on some
Packit c4476c
of the types customarily made available by including windows.h. The
Packit c4476c
application developer is likely to require control over when the latter
Packit c4476c
is included, commonly as one of the first included headers. Therefore
Packit c4476c
it is defined as an application developer's responsibility to include
Packit c4476c
windows.h prior to async.h.
Packit c4476c
Packit c4476c
=head1 EXAMPLES
Packit c4476c
Packit c4476c
The following example demonstrates how to use most of the core async APIs:
Packit c4476c
Packit c4476c
 #ifdef _WIN32
Packit c4476c
 # include <windows.h>
Packit c4476c
 #endif
Packit c4476c
 #include <stdio.h>
Packit c4476c
 #include <unistd.h>
Packit c4476c
 #include <openssl/async.h>
Packit c4476c
 #include <openssl/crypto.h>
Packit c4476c
Packit c4476c
 int unique = 0;
Packit c4476c
Packit c4476c
 void cleanup(ASYNC_WAIT_CTX *ctx, const void *key, OSSL_ASYNC_FD r, void *vw)
Packit c4476c
 {
Packit c4476c
     OSSL_ASYNC_FD *w = (OSSL_ASYNC_FD *)vw;
Packit c4476c
Packit c4476c
     close(r);
Packit c4476c
     close(*w);
Packit c4476c
     OPENSSL_free(w);
Packit c4476c
 }
Packit c4476c
Packit c4476c
 int jobfunc(void *arg)
Packit c4476c
 {
Packit c4476c
     ASYNC_JOB *currjob;
Packit c4476c
     unsigned char *msg;
Packit c4476c
     int pipefds[2] = {0, 0};
Packit c4476c
     OSSL_ASYNC_FD *wptr;
Packit c4476c
     char buf = 'X';
Packit c4476c
Packit c4476c
     currjob = ASYNC_get_current_job();
Packit c4476c
     if (currjob != NULL) {
Packit c4476c
         printf("Executing within a job\n");
Packit c4476c
     } else {
Packit c4476c
         printf("Not executing within a job - should not happen\n");
Packit c4476c
         return 0;
Packit c4476c
     }
Packit c4476c
Packit c4476c
     msg = (unsigned char *)arg;
Packit c4476c
     printf("Passed in message is: %s\n", msg);
Packit c4476c
Packit c4476c
     if (pipe(pipefds) != 0) {
Packit c4476c
         printf("Failed to create pipe\n");
Packit c4476c
         return 0;
Packit c4476c
     }
Packit c4476c
     wptr = OPENSSL_malloc(sizeof(OSSL_ASYNC_FD));
Packit c4476c
     if (wptr == NULL) {
Packit c4476c
         printf("Failed to malloc\n");
Packit c4476c
         return 0;
Packit c4476c
     }
Packit c4476c
     *wptr = pipefds[1];
Packit c4476c
     ASYNC_WAIT_CTX_set_wait_fd(ASYNC_get_wait_ctx(currjob), &unique,
Packit c4476c
                                pipefds[0], wptr, cleanup);
Packit c4476c
Packit c4476c
     /*
Packit c4476c
      * Normally some external event would cause this to happen at some
Packit c4476c
      * later point - but we do it here for demo purposes, i.e.
Packit c4476c
      * immediately signalling that the job is ready to be woken up after
Packit c4476c
      * we return to main via ASYNC_pause_job().
Packit c4476c
      */
Packit c4476c
     write(pipefds[1], &buf, 1);
Packit c4476c
Packit c4476c
     /* Return control back to main */
Packit c4476c
     ASYNC_pause_job();
Packit c4476c
Packit c4476c
     /* Clear the wake signal */
Packit c4476c
     read(pipefds[0], &buf, 1);
Packit c4476c
Packit c4476c
     printf ("Resumed the job after a pause\n");
Packit c4476c
Packit c4476c
     return 1;
Packit c4476c
 }
Packit c4476c
Packit c4476c
 int main(void)
Packit c4476c
 {
Packit c4476c
     ASYNC_JOB *job = NULL;
Packit c4476c
     ASYNC_WAIT_CTX *ctx = NULL;
Packit c4476c
     int ret;
Packit c4476c
     OSSL_ASYNC_FD waitfd;
Packit c4476c
     fd_set waitfdset;
Packit c4476c
     size_t numfds;
Packit c4476c
     unsigned char msg[13] = "Hello world!";
Packit c4476c
Packit c4476c
     printf("Starting...\n");
Packit c4476c
Packit c4476c
     ctx = ASYNC_WAIT_CTX_new();
Packit c4476c
     if (ctx == NULL) {
Packit c4476c
         printf("Failed to create ASYNC_WAIT_CTX\n");
Packit c4476c
         abort();
Packit c4476c
     }
Packit c4476c
Packit c4476c
     for (;;) {
Packit c4476c
         switch (ASYNC_start_job(&job, ctx, &ret, jobfunc, msg, sizeof(msg))) {
Packit c4476c
         case ASYNC_ERR:
Packit c4476c
         case ASYNC_NO_JOBS:
Packit c4476c
             printf("An error occurred\n");
Packit c4476c
             goto end;
Packit c4476c
         case ASYNC_PAUSE:
Packit c4476c
             printf("Job was paused\n");
Packit c4476c
             break;
Packit c4476c
         case ASYNC_FINISH:
Packit c4476c
             printf("Job finished with return value %d\n", ret);
Packit c4476c
             goto end;
Packit c4476c
         }
Packit c4476c
Packit c4476c
         /* Wait for the job to be woken */
Packit c4476c
         printf("Waiting for the job to be woken up\n");
Packit c4476c
Packit c4476c
         if (!ASYNC_WAIT_CTX_get_all_fds(ctx, NULL, &numfds)
Packit c4476c
                 || numfds > 1) {
Packit c4476c
             printf("Unexpected number of fds\n");
Packit c4476c
             abort();
Packit c4476c
         }
Packit c4476c
         ASYNC_WAIT_CTX_get_all_fds(ctx, &waitfd, &numfds);
Packit c4476c
         FD_ZERO(&waitfdset);
Packit c4476c
         FD_SET(waitfd, &waitfdset);
Packit c4476c
         select(waitfd + 1, &waitfdset, NULL, NULL, NULL);
Packit c4476c
     }
Packit c4476c
Packit c4476c
 end:
Packit c4476c
     ASYNC_WAIT_CTX_free(ctx);
Packit c4476c
     printf("Finishing\n");
Packit c4476c
Packit c4476c
     return 0;
Packit c4476c
 }
Packit c4476c
Packit c4476c
The expected output from executing the above example program is:
Packit c4476c
Packit c4476c
 Starting...
Packit c4476c
 Executing within a job
Packit c4476c
 Passed in message is: Hello world!
Packit c4476c
 Job was paused
Packit c4476c
 Waiting for the job to be woken up
Packit c4476c
 Resumed the job after a pause
Packit c4476c
 Job finished with return value 1
Packit c4476c
 Finishing
Packit c4476c
Packit c4476c
=head1 SEE ALSO
Packit c4476c
Packit c4476c
L<crypto(7)>, L<ERR_print_errors(3)>
Packit c4476c
Packit c4476c
=head1 HISTORY
Packit c4476c
Packit c4476c
ASYNC_init_thread, ASYNC_cleanup_thread,
Packit c4476c
ASYNC_start_job, ASYNC_pause_job, ASYNC_get_current_job, ASYNC_get_wait_ctx(),
Packit c4476c
ASYNC_block_pause(), ASYNC_unblock_pause() and ASYNC_is_capable() were first
Packit c4476c
added in OpenSSL 1.1.0.
Packit c4476c
Packit c4476c
=head1 COPYRIGHT
Packit c4476c
Packit c4476c
Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
Packit c4476c
Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
this file except in compliance with the License.  You can obtain a copy
Packit c4476c
in the file LICENSE in the source distribution or at
Packit c4476c
L<https://www.openssl.org/source/license.html>.
Packit c4476c
Packit c4476c
=cut