ASYNC_start_job.pod 13 KB

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