ASYNC_start_job.pod 12 KB

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