2 * Copyright (c) 2000 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * This file contains several functions to doing reads and writes.
23 * It was written so that a single function could be called in a test
24 * program and only a io type field value would have to change to
25 * do different types of io. There is even a couple of functions that
26 * will allow you to parse a string to determine the iotype.
28 * This file contains functions for writing/reading to/from open files
31 * Functions declared in this module - see individual function code for
34 * int stride_bounds(int offset, int stride, int nstrides,
35 * int bytes_per_stride, int *min, int *max);
37 * int lio_write_buffer(int fd, int method, char *buffer, int size,
38 * char **errmsg, long wrd);
39 * int lio_read_buffer(int fd, int method, char *buffer, int size,
40 * char **errmsg, long wrd);
41 * int lio_parse_io_arg1(char *string)
42 * void lio_help1(char *prefix);
44 * int lio_parse_io_arg2(char *string, char **badtoken)
45 * void lio_help2(char *prefix);
47 * int lio_set_debug(int level);
50 * struct lio_info_type Lio_info1[];
51 * struct lio_info_type Lio_info2[];
53 * Author : Richard Logan
61 #include <sys/types.h>
64 #include <sys/param.h>
66 #include <sys/types.h>
69 #include <sys/uio.h> /* readv(2)/writev(2) */
72 #include <stdlib.h> /* atoi, abs */
74 #include "tlibio.h" /* defines LIO* marcos */
77 #define PATH_MAX MAXPATHLEN
80 #if 0 /* disabled until it's needed -- roehrich 6/11/97 */
81 #define BUG1_workaround 1 /* Work around a condition where aio_return gives
82 * a value of zero but there is no errno followup
83 * and the read/write operation actually did its
90 static void lio_async_signal_handler();
94 * Define the structure as used in lio_parse_arg1 and lio_help1
96 struct lio_info_type Lio_info1[] = {
97 { "s", LIO_IO_SYNC, "sync i/o" },
98 { "p", LIO_IO_ASYNC|LIO_WAIT_SIGACTIVE, "async i/o using a loop to wait for a signal" },
99 { "b", LIO_IO_ASYNC|LIO_WAIT_SIGPAUSE, "async i/o using pause" },
100 { "a", LIO_IO_ASYNC|LIO_WAIT_RECALL, "async i/o using recall/aio_suspend" },
102 LIO_RANDOM|LIO_IO_TYPES|LIO_WAIT_TYPES, "random i/o types and wait methods" },
104 LIO_RANDOM|LIO_IO_TYPES|LIO_WAIT_TYPES, "random i/o types and wait methods" },
105 { "l", LIO_IO_SLISTIO|LIO_WAIT_RECALL, "single stride sync listio" },
106 { "L", LIO_IO_ALISTIO|LIO_WAIT_RECALL, "single stride async listio using recall" },
107 { "X", LIO_IO_ALISTIO|LIO_WAIT_SIGPAUSE, "single stride async listio using pause" },
108 { "v", LIO_IO_SYNCV, "single buffer sync readv/writev" },
109 { "P", LIO_IO_SYNCP, "sync pread/pwrite" },
113 * Define the structure used by lio_parse_arg2 and lio_help2
115 struct lio_info_type Lio_info2[] = {
116 { "sync", LIO_IO_SYNC, "sync i/o (read/write)"},
117 { "async", LIO_IO_ASYNC, "async i/o (reada/writea/aio_read/aio_write)" },
118 { "slistio", LIO_IO_SLISTIO, "single stride sync listio" },
119 { "alistio", LIO_IO_ALISTIO, "single stride async listio" },
120 { "syncv", LIO_IO_SYNCV, "single buffer sync readv/writev"},
121 { "syncp", LIO_IO_SYNCP, "pread/pwrite"},
122 { "active", LIO_WAIT_ACTIVE, "spin on status/control values" },
123 { "recall", LIO_WAIT_RECALL, "use recall(2)/aio_suspend(3) to wait for i/o to complete" },
124 { "sigactive", LIO_WAIT_SIGACTIVE, "spin waiting for signal" },
125 { "sigpause", LIO_WAIT_SIGPAUSE, "call pause(2) to wait for signal" },
126 /* nowait is a touchy thing, it's an accident that this implementation worked at all. 6/27/97 roehrich */
127 /* { "nowait", LIO_WAIT_NONE, "do not wait for async io to complete" },*/
128 { "random", LIO_RANDOM, "set random bit" },
130 LIO_RANDOM|LIO_IO_TYPES|LIO_WAIT_TYPES,
131 "all random i/o types and wait methods (except nowait)" },
134 char Lio_SysCall[PATH_MAX]; /* string containing last i/o system call */
136 static volatile int Received_signal = 0; /* number of signals received */
137 static volatile int Rec_signal;
138 static char Errormsg[500];
139 static int Debug_level = 0;
143 /***********************************************************************
146 * Determine the bounds of a strided request, normalized to offset. Returns
147 * the number of bytes needed to satisfy the request, and optionally sets
148 * *min and *max to the mininum and maximum bytes referenced, normalized
151 * Returns -1 on error - the only possible error conditions are illegal values
152 * for nstrides and/or bytes_per_stride - both parameters must be >= 0.
155 ***********************************************************************/
158 stride_bounds(offset, stride, nstrides, bytes_per_stride, min, max)
162 int bytes_per_stride;
166 int nbytes, min_byte, max_byte;
172 if (nstrides < 0 || bytes_per_stride < 0) {
177 stride = bytes_per_stride;
181 * Determine the # of bytes needed to satisfy the request. This
182 * value, along with the offset argument, determines the min and max
187 nbytes = abs(stride) * (nstrides-1) + bytes_per_stride;
190 max_byte = offset + bytes_per_stride - 1;
191 min_byte = max_byte - nbytes + 1;
194 max_byte = min_byte + nbytes - 1;
208 /***********************************************************************
209 * This function will allow someone to set the debug level.
210 ***********************************************************************/
212 lio_set_debug(int level)
221 /***********************************************************************
222 * This function will parse a string and return desired io-method.
223 * Only the first character of the string is used.
225 * This function does not provide for meaningful option arguments,
226 * but it supports current growfiles/btlk interface.
229 ***********************************************************************/
231 lio_parse_io_arg1(char *string)
238 * Determine if token is a valid string.
240 for(ind=0; ind<sizeof(Lio_info1)/sizeof(struct lio_info_type); ind++) {
241 if ( strcmp(string, Lio_info1[ind].token) == 0 ) {
242 mask |= Lio_info1[ind].bits;
256 /***********************************************************************
257 * This function will print a help message describing the characters
258 * that can be parsed by lio_parse_io_arg1().
259 * They will be printed one per line.
261 ***********************************************************************/
263 lio_help1(char *prefix)
267 for(ind=0; ind<sizeof(Lio_info1)/sizeof(struct lio_info_type); ind++) {
268 printf("%s %s : %s\n", prefix,
269 Lio_info1[ind].token, Lio_info1[ind].desc);
275 /***********************************************************************
276 * This function will parse a string and return the desired io-method.
277 * This function will take a comma separated list of io type and wait
278 * method tokens as defined in Lio_info2[]. If a token does not match
279 * any of the tokens in Lio_info2[], it will be coverted to a number.
280 * If it was a number, those bits are also set.
283 ***********************************************************************/
285 lio_parse_io_arg2(char *string, char **badtoken)
287 char *token = string;
301 for (; ((*cc != ',') && (*cc != '\0')); cc++);
308 * Determine if token is a valid string or number and if
309 * so, add the bits to the mask.
311 for(ind=0; ind<sizeof(Lio_info2)/sizeof(struct lio_info_type); ind++) {
312 if ( strcmp(token, Lio_info2[ind].token) == 0 ) {
313 mask |= Lio_info2[ind].bits;
320 * If token does not match one of the defined tokens, determine
321 * if it is a number, if so, add the bits.
324 if (sscanf(token, "%i%c", &tmp, &chr) == 1 ) {
332 if (!found) { /* token is not valid */
333 if ( badtoken != NULL)
347 /***********************************************************************
348 * This function will print a help message describing the tokens
349 * that can be parsed by lio_parse_io_arg2().
350 * It will print them one per line.
353 ***********************************************************************/
355 lio_help2(char *prefix)
359 for(ind=0; ind<sizeof(Lio_info2)/sizeof(struct lio_info_type); ind++) {
360 printf("%s %s : %s\n", prefix,
361 Lio_info2[ind].token, Lio_info2[ind].desc);
367 /***********************************************************************
368 * This is an internal signal handler.
369 * If the handler is called, it will increment the Received_signal
371 ***********************************************************************/
373 lio_async_signal_handler(int sig)
376 printf("DEBUG %s/%d: received signal %d, a signal caught %d times\n",
377 __FILE__, __LINE__, sig, Received_signal+1);
385 /***********************************************************************
387 * This function will randomly choose an io type and wait method
388 * from set of io types and wait methods. Since this information
389 * is stored in a bitmask, it randomly chooses an io type from
390 * the io type bits specified and does the same for wait methods.
393 * This function will return a value with all non choosen io type
394 * and wait method bits cleared. The LIO_RANDOM bit is also
395 * cleared. All other bits are left unchanged.
398 ***********************************************************************/
400 lio_random_methods(long curr_mask)
403 long random_bit(long);
405 /* remove random select, io type, and wait method bits from curr_mask */
406 mask = curr_mask & (~(LIO_IO_TYPES | LIO_WAIT_TYPES | LIO_RANDOM));
408 /* randomly select io type from specified io types */
409 mask = mask | random_bit(curr_mask & LIO_IO_TYPES);
411 /* randomly select wait methods from specified wait methods */
412 mask = mask | random_bit(curr_mask & LIO_WAIT_TYPES);
417 /***********************************************************************
418 * Generic write function
419 * This function can be used to do a write using write(2), writea(2),
420 * aio_write(3), writev(2), pwrite(2),
421 * or single stride listio(2)/lio_listio(3).
422 * By setting the desired bits in the method
423 * bitmask, the caller can control the type of write and the wait method
424 * that will be used. If no io type bits are set, write will be used.
426 * If async io was attempted and no wait method bits are set then the
427 * wait method is: recall(2) for writea(2) and listio(2); aio_suspend(3) for
428 * aio_write(3) and lio_listio(3).
430 * If multiple wait methods are specified,
431 * only one wait method will be used. The order is predetermined.
433 * If the call specifies a signal and one of the two signal wait methods,
434 * a signal handler for the signal is set. This will reset an already
435 * set handler for this signal.
437 * If the LIO_RANDOM method bit is set, this function will randomly
438 * choose a io type and wait method from bits in the method argument.
440 * If an error is encountered, an error message will be generated
441 * in a internal static buffer. If errmsg is not NULL, it will
442 * be updated to point to the static buffer, allowing the caller
443 * to print the error message.
446 * If a system call fails, -errno is returned.
447 * If LIO_WAIT_NONE bit is set, the return value is the return value
448 * of the system call.
449 * If the io did not fail, the amount of data written is returned.
450 * If the size the system call say was written is different
451 * then what was asked to be written, errmsg is updated for
452 * this error condition. The return value is still the amount
453 * the system call says was written.
456 ***********************************************************************/
458 lio_write_buffer(fd, method, buffer, size, sig, errmsg, wrd)
459 int fd; /* open file descriptor */
460 int method; /* contains io type and wait method bitmask */
461 char *buffer; /* pointer to buffer */
462 int size; /* the size of the io */
463 int sig; /* signal to use if async io */
464 char **errmsg; /* char pointer that will be updated to point to err message */
465 long wrd; /* to allow future features, use zero for now */
467 int ret = 0; /* syscall return or used to get random method */
469 int omethod = method;
470 int listio_cmd; /* Holds the listio/lio_listio cmd */
472 struct iovec iov; /* iovec for writev(2) */
475 * If LIO_RANDOM bit specified, get new method randomly.
477 if ( method & LIO_RANDOM ) {
478 if( Debug_level > 3 )
479 printf("DEBUG %s/%d: method mask to choose from: %#o\n", __FILE__, __LINE__, method );
480 method = lio_random_methods(method);
481 if ( Debug_level > 2 )
482 printf("DEBUG %s/%d: random chosen method %#o\n", __FILE__, __LINE__, method);
485 if ( errmsg != NULL )
488 Rec_signal=Received_signal; /* get the current number of signals received */
489 bzero(&iov, sizeof(struct iovec));
490 iov.iov_base = buffer;
494 * If the LIO_USE_SIGNAL bit is not set, only use the signal
495 * if the LIO_WAIT_SIGPAUSE or the LIO_WAIT_SIGACTIVE bits are bit.
496 * Otherwise there is not necessary a signal handler to trap
499 if ( sig && !(method & LIO_USE_SIGNAL) &&
500 ! (method & LIO_WAIT_SIGTYPES) ){
502 sig=0; /* ignore signal parameter */
506 * only setup signal hander if sig was specified and
507 * a sig wait method was specified.
508 * Doing this will change the handler for this signal. The
509 * old signal handler will not be restored.
510 *** restoring the signal handler could be added ***
514 * Determine the system call that will be called and produce
515 * the string of the system call and place it in Lio_SysCall.
516 * Execute the system call and check for system call failure.
517 * If sync i/o, return the number of bytes written/read.
520 if ( (method & LIO_IO_SYNC) || (method & LIO_IO_TYPES) == 0 ){
522 * write(2) is used if LIO_IO_SYNC bit is set or not none
523 * of the LIO_IO_TYPES bits are set (default).
527 "write(%d, buf, %d)", fd, size);
530 printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, Lio_SysCall);
533 if ((ret = write(fd, buffer, size)) == -1) {
534 sprintf(Errormsg, "%s/%d write(%d, buf, %d) ret:-1, errno=%d %s",
536 fd, size, errno, strerror(errno));
542 "%s/%d write(%d, buf, %d) returned=%d",
546 else if ( Debug_level > 1 )
547 printf("DEBUG %s/%d: write completed without error (ret %d)\n",
548 __FILE__, __LINE__, ret);
554 else if ( method & LIO_IO_SYNCV ) {
557 "writev(%d, &iov, 1) nbyte:%d", fd, size);
560 printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, Lio_SysCall);
562 if ((ret = writev(fd, &iov, 1)) == -1) {
563 sprintf(Errormsg, "%s/%d writev(%d, iov, 1) nbyte:%d ret:-1, errno=%d %s",
565 fd, size, errno, strerror(errno));
571 "%s/%d writev(%d, iov, 1) nbyte:%d returned=%d",
575 else if ( Debug_level > 1 )
576 printf("DEBUG %s/%d: writev completed without error (ret %d)\n",
577 __FILE__, __LINE__, ret);
583 printf("DEBUG %s/%d: No I/O method chosen\n", __FILE__, __LINE__ );
588 * If there was an error waiting for async i/o to complete,
589 * return the error value (errno) to the caller.
590 * Note: Errormsg should already have been updated.
597 * If i/o was not waited for (may not have been completed at this time),
598 * return the size that was requested.
604 * check that async io was successful.
605 * Note: if the there was an system call failure, -errno
606 * was returned and Errormsg should already have been updated.
607 * If amount i/o was different than size, Errormsg should already
608 * have been updated but the actual i/o size if returned.
612 } /* end of lio_write_buffer */
614 /***********************************************************************
615 * Generic read function
616 * This function can be used to do a read using read(2), reada(2),
617 * aio_read(3), readv(2), pread(2),
618 * or single stride listio(2)/lio_listio(3).
619 * By setting the desired bits in the method
620 * bitmask, the caller can control the type of read and the wait method
621 * that will be used. If no io type bits are set, read will be used.
623 * If async io was attempted and no wait method bits are set then the
624 * wait method is: recall(2) for reada(2) and listio(2); aio_suspend(3) for
625 * aio_read(3) and lio_listio(3).
627 * If multiple wait methods are specified,
628 * only one wait method will be used. The order is predetermined.
630 * If the call specifies a signal and one of the two signal wait methods,
631 * a signal handler for the signal is set. This will reset an already
632 * set handler for this signal.
634 * If the LIO_RANDOM method bit is set, this function will randomly
635 * choose a io type and wait method from bits in the method argument.
637 * If an error is encountered, an error message will be generated
638 * in a internal static buffer. If errmsg is not NULL, it will
639 * be updated to point to the static buffer, allowing the caller
640 * to print the error message.
643 * If a system call fails, -errno is returned.
644 * If LIO_WAIT_NONE bit is set, the return value is the return value
645 * of the system call.
646 * If the io did not fail, the amount of data written is returned.
647 * If the size the system call say was written is different
648 * then what was asked to be written, errmsg is updated for
649 * this error condition. The return value is still the amount
650 * the system call says was written.
653 ***********************************************************************/
655 lio_read_buffer(fd, method, buffer, size, sig, errmsg, wrd)
656 int fd; /* open file descriptor */
657 int method; /* contains io type and wait method bitmask */
658 char *buffer; /* pointer to buffer */
659 int size; /* the size of the io */
660 int sig; /* signal to use if async io */
661 char **errmsg; /* char pointer that will be updated to point to err message */
662 long wrd; /* to allow future features, use zero for now */
664 int ret = 0; /* syscall return or used to get random method */
666 int listio_cmd; /* Holds the listio/lio_listio cmd */
667 int omethod = method;
669 struct iovec iov; /* iovec for readv(2) */
672 * If LIO_RANDOM bit specified, get new method randomly.
674 if ( method & LIO_RANDOM ) {
675 if( Debug_level > 3 )
676 printf("DEBUG %s/%d: method mask to choose from: %#o\n", __FILE__, __LINE__, method );
677 method = lio_random_methods(method);
678 if ( Debug_level > 2 )
679 printf("DEBUG %s/%d: random chosen method %#o\n", __FILE__, __LINE__, method);
682 if ( errmsg != NULL )
685 Rec_signal=Received_signal; /* get the current number of signals received */
686 bzero(&iov, sizeof(struct iovec));
687 iov.iov_base = buffer;
691 * If the LIO_USE_SIGNAL bit is not set, only use the signal
692 * if the LIO_WAIT_SIGPAUSE or the LIO_WAIT_SIGACTIVE bits are set.
693 * Otherwise there is not necessarily a signal handler to trap
696 if ( sig && !(method & LIO_USE_SIGNAL) &&
697 ! (method & LIO_WAIT_SIGTYPES) ){
699 sig=0; /* ignore signal parameter */
703 * only setup signal hander if sig was specified and
704 * a sig wait method was specified.
705 * Doing this will change the handler for this signal. The
706 * old signal handler will not be restored.
707 *** restoring the signal handler could be added ***
711 * Determine the system call that will be called and produce
712 * the string of the system call and place it in Lio_SysCall.
713 * Execute the system call and check for system call failure.
714 * If sync i/o, return the number of bytes written/read.
717 if ( (method & LIO_IO_SYNC) || (method & LIO_IO_TYPES) == 0 ){
719 * read(2) is used if LIO_IO_SYNC bit is set or not none
720 * of the LIO_IO_TYPES bits are set (default).
724 "read(%d, buf, %d)", fd, size);
727 printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, Lio_SysCall);
730 if ((ret = read(fd, buffer, size)) == -1) {
731 sprintf(Errormsg, "%s/%d read(%d, buf, %d) ret:-1, errno=%d %s",
733 fd, size, errno, strerror(errno));
739 "%s/%d read(%d, buf, %d) returned=%d",
743 else if ( Debug_level > 1 )
744 printf("DEBUG %s/%d: read completed without error (ret %d)\n",
745 __FILE__, __LINE__, ret);
751 else if ( method & LIO_IO_SYNCV ) {
754 "readv(%d, &iov, 1) nbyte:%d", fd, size);
757 printf("DEBUG %s/%d: %s\n", __FILE__, __LINE__, Lio_SysCall);
759 if ((ret = readv(fd, &iov, 1)) == -1) {
760 sprintf(Errormsg, "%s/%d readv(%d, iov, 1) nbyte:%d ret:-1, errno=%d %s",
762 fd, size, errno, strerror(errno));
768 "%s/%d readv(%d, iov, 1) nbyte:%d returned=%d",
772 else if ( Debug_level > 1 )
773 printf("DEBUG %s/%d: readv completed without error (ret %d)\n",
774 __FILE__, __LINE__, ret);
780 printf("DEBUG %s/%d: No I/O method chosen\n", __FILE__, __LINE__ );
785 * If there was an error waiting for async i/o to complete,
786 * return the error value (errno) to the caller.
787 * Note: Errormsg should already have been updated.
794 * If i/o was not waited for (may not have been completed at this time),
795 * return the size that was requested.
801 * check that async io was successful.
802 * Note: if the there was an system call failure, -errno
803 * was returned and Errormsg should already have been updated.
804 * If amount i/o was different than size, Errormsg should already
805 * have been updated but the actual i/o size if returned.
809 } /* end of lio_read_buffer */
813 /***********************************************************************
814 * This function will check that async io was successful.
815 * It can also be used to check sync listio since it uses the
819 * If status.sw_error is set, -status.sw_error is returned.
820 * Otherwise sw_count's field value is returned.
823 ***********************************************************************/
825 lio_check_asyncio(char *io_type, int size, aiocb_t *aiocbp, int method)
830 /* The I/O may have been synchronous with signal completion. It doesn't
831 * make sense, but the combination could be generated. Release the
832 * completion signal here otherwise it'll hang around and bite us
835 if( aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL )
836 sigrelse( aiocbp->aio_sigevent.sigev_signo );
838 ret = aio_error( aiocbp );
840 while( ret == EINPROGRESS ){
841 ret = aio_error( aiocbp );
846 "%s/%d %s, aio_error had to loop on EINPROGRESS, cnt=%d; random method %#o; sigev_notify=%s",
847 __FILE__, __LINE__, io_type, cnt, method,
848 (aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL ? "signal" :
849 aiocbp->aio_sigevent.sigev_notify == SIGEV_NONE ? "none" :
850 aiocbp->aio_sigevent.sigev_notify == SIGEV_CALLBACK ? "callback" :
857 "%s/%d %s, aio_error = %d %s; random method %#o",
858 __FILE__, __LINE__, io_type,
863 ret = aio_return( aiocbp );
866 "%s/%d %s, aio_return not as expected(%d), but actual:%d",
867 __FILE__, __LINE__, io_type,
870 #ifdef BUG1_workaround
873 if( Debug_level > 1 ){
874 printf("WARN %s/%d: %s completed with bug1_workaround (aio_error == 0, aio_return now == %d)\n",
875 __FILE__, __LINE__, io_type, ret);
878 #endif /* BUG1_workaround */
881 else if( Debug_level > 1 ){
882 printf("DEBUG %s/%d: %s completed without error (aio_error == 0, aio_return == %d)\n",
883 __FILE__, __LINE__, io_type, ret);
888 } /* end of lio_check_asyncio */
891 /***********************************************************************
893 * This function will wait for async io to complete.
894 * If multiple wait methods are specified, the order is predetermined
895 * to LIO_WAIT_RECALL,
896 * LIO_WAIT_ACTIVE, LIO_WAIT_SIGPAUSE, LIO_WAIT_SIGACTIVE,
897 * then LIO_WAIT_NONE.
899 * If no wait method was specified the default wait method is: recall(2)
900 * or aio_suspend(3), as appropriate.
903 * <0: errno of failed recall
904 * 0 : async io was completed
905 * 1 : async was not waited for, io may not have completed.
908 ***********************************************************************/
910 lio_wait4asyncio(int method, int fd, aiocb_t *aiocbp)
914 if ( (method & LIO_WAIT_RECALL)
915 || ((method & LIO_WAIT_TYPES) == 0) ){
917 * If method has LIO_WAIT_RECALL bit set or method does
918 * not have any wait method bits set (default), use recall/aio_suspend.
920 if ( Debug_level > 2 )
921 printf("DEBUG %s/%d: wait method : aio_suspend, sigev_notify=%s\n", __FILE__, __LINE__,
922 (aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL ? "signal" :
923 aiocbp->aio_sigevent.sigev_notify == SIGEV_NONE ? "none" :
924 aiocbp->aio_sigevent.sigev_notify == SIGEV_CALLBACK ? "callback" :
928 ret = aio_suspend( aioary, 1, NULL );
929 if( (ret == -1) && (errno == EINTR) ){
930 if( aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL ){
931 if( Debug_level > 2 ){
932 printf("DEBUG %s/%d: aio_suspend received EINTR, sigev_notify=SIGEV_SIGNAL -- ok\n",
933 __FILE__, __LINE__ );
937 sprintf(Errormsg, "%s/%d aio_suspend received EINTR, sigev_notify=%s, not ok\n",
939 (aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL ? "signal" :
940 aiocbp->aio_sigevent.sigev_notify == SIGEV_NONE ? "none" :
941 aiocbp->aio_sigevent.sigev_notify == SIGEV_CALLBACK ? "callback" :
947 sprintf(Errormsg, "%s/%d aio_suspend(fildes=%d, aioary, 1, NULL) failed, errno:%d %s",
949 fd, errno, strerror(errno));
952 } else if ( method & LIO_WAIT_ACTIVE ) {
953 if ( Debug_level > 2 )
954 printf("DEBUG %s/%d: wait method : active\n", __FILE__, __LINE__);
956 /* loop while aio_error() returns EINPROGRESS */
959 ret = aio_error( aiocbp );
960 if( (ret == 0) || (ret != EINPROGRESS) ){
966 if ( Debug_level > 5 && cnt && (cnt % 50) == 0 )
967 printf("DEBUG %s/%d: wait active cnt = %d\n",
968 __FILE__, __LINE__, cnt);
970 } else if ( method & LIO_WAIT_SIGPAUSE ) {
971 if ( Debug_level > 2 )
972 printf("DEBUG %s/%d: wait method : sigpause\n", __FILE__, __LINE__);
975 } else if ( method & LIO_WAIT_SIGACTIVE ) {
976 if ( Debug_level > 2 )
977 printf("DEBUG %s/%d: wait method : sigactive\n", __FILE__, __LINE__);
978 if( aiocbp->aio_sigevent.sigev_notify == SIGEV_SIGNAL )
979 sigrelse( aiocbp->aio_sigevent.sigev_signo );
981 printf("DEBUG %s/%d: sigev_notify != SIGEV_SIGNAL\n", __FILE__, __LINE__ );
984 /* loop waiting for signal */
985 while ( Received_signal == Rec_signal ){
986 sigrelse( aiocbp->aio_sigevent.sigev_signo );
989 } else if ( method & LIO_WAIT_NONE ) {
990 if ( Debug_level > 2 )
991 printf("DEBUG %s/%d: wait method : none\n", __FILE__, __LINE__);
992 /* It's broken because the aiocb/iosw is an automatic variable in
993 * lio_{read,write}_buffer, so when the function returns and the
994 * I/O completes there will be nowhere to write the I/O status.
995 * It doesn't cause a problem on unicos--probably because of some
996 * compiler quirk, or an accident. It causes POSIX async I/O
997 * to core dump some threads. spr/pv 705909. 6/27/97 roehrich
999 sprintf(Errormsg, "%s/%d LIO_WAIT_NONE was selected (this is broken)\n",
1000 __FILE__, __LINE__ );
1004 if( Debug_level > 2 )
1005 printf("DEBUG %s/%d: no wait method was chosen\n", __FILE__, __LINE__ );
1011 } /* end of lio_wait4asyncio */
1013 #endif /* ifndef linux */
1016 /***********************************************************************
1017 * The following code is provided as unit test.
1018 * Just define add "-DUNIT_TEST=1" to the cc line.
1021 ***********************************************************************/
1022 struct unit_info_t {
1027 { LIO_IO_SYNC, 0, "sync io" },
1028 { LIO_IO_SYNCV, 0, "sync readv/writev" },
1029 { LIO_IO_SYNCP, 0, "sync pread/pwrite" },
1030 { LIO_IO_ASYNC, 0, "async io, def wait" },
1031 { LIO_IO_SLISTIO, 0, "sync listio" },
1032 { LIO_IO_ALISTIO, 0, "async listio, def wait" },
1033 { LIO_IO_ASYNC|LIO_WAIT_ACTIVE, 0, "async active" },
1034 { LIO_IO_ASYNC|LIO_WAIT_RECALL, 0, "async recall/suspend" },
1035 { LIO_IO_ASYNC|LIO_WAIT_SIGPAUSE, SIGUSR1, "async sigpause" },
1036 { LIO_IO_ASYNC|LIO_WAIT_SIGACTIVE, SIGUSR1, "async sigactive" },
1037 { LIO_IO_ALISTIO|LIO_WAIT_ACTIVE, 0, "async listio active" },
1038 { LIO_IO_ALISTIO|LIO_WAIT_RECALL, 0, "async listio recall" },
1039 { LIO_IO_ALISTIO|LIO_WAIT_SIGACTIVE, SIGUSR1, "async listio sigactive" },
1040 { LIO_IO_ALISTIO|LIO_WAIT_SIGPAUSE, SIGUSR1, "async listio sigpause" },
1041 { LIO_IO_ASYNC, SIGUSR2, "async io, def wait, sigusr2" },
1042 { LIO_IO_ALISTIO, SIGUSR2, "async listio, def wait, sigusr2" },
1050 extern char *optarg;
1061 int exit_status = 0;
1064 char *symbols = NULL;
1067 while( (c = getopt(argc,argv,"s:di:")) != -1 ){
1069 case 's': symbols = optarg; break;
1070 case 'd': ++die_on_err; break;
1071 case 'i': iter = atoi(optarg); break;
1075 if ((fd=open("unit_test_file", O_CREAT|O_RDWR|O_TRUNC, 0777)) == -1 ) {
1076 perror("open(unit_test_file, O_CREAT|O_RDWR|O_TRUNC, 0777) failed");
1082 if ( symbols != NULL ) {
1083 if ( (method=lio_parse_io_arg2(symbols, &err)) == -1 ){
1084 printf("lio_parse_io_arg2(%s, &err) failed, bad token starting at %s\n",
1090 printf("lio_parse_io_arg2(%s, &err) returned %#o\n", symbols, method);
1093 for(ind=0; ind < iter; ind++ ) {
1094 memset( buffer, 'A', 4096 );
1095 if( lseek(fd, 0, 0) == -1 ){
1096 printf("lseek(fd,0,0), %d, failed, errno %d\n",
1100 if ((ret=lio_write_buffer(fd, method, buffer,
1101 size, SIGUSR1, &err, 0)) != size ) {
1102 printf("lio_write_buffer returned -1, err = %s\n", err);
1104 printf("lio_write_buffer returned %d\n", ret);
1106 memset( buffer, 'B', 4096 );
1107 if( lseek(fd, 0, 0) == -1 ){
1108 printf("lseek(fd,0,0), %d, failed, errno %d\n",
1112 if ((ret=lio_read_buffer(fd, method, buffer,
1113 size, SIGUSR2, &err, 0)) != size ) {
1114 printf("lio_read_buffer returned -1, err = %s\n", err);
1116 printf("lio_read_buffer returned %d\n", ret);
1118 for( i = 0; i < 4096; ++i ){
1119 if( buffer[i] != 'A' ){
1120 printf(" buffer[%d] = %d\n", i, buffer[i] );
1131 unlink("unit_test_file");
1135 for(ind=0; ind < sizeof(Unit_info)/sizeof(struct unit_info_t); ind++ ) {
1137 printf("\n********* write %s ***************\n", Unit_info[ind].str);
1138 if( lseek(fd, 0, 0) == -1 ){
1139 printf("lseek(fd,0,0), %d, failed, errno %d\n",
1144 memset( buffer, 'A', 4096 );
1145 if ((ret=lio_write_buffer(fd, Unit_info[ind].method, buffer,
1146 size, Unit_info[ind].sig, &err, 0)) != size ) {
1147 printf(">>>>> lio_write_buffer(fd,0%x,buffer,%d,%d,err,0) returned -1,\n err = %s\n",
1148 Unit_info[ind].method, size, Unit_info[ind].sig, err);
1153 printf("lio_write_buffer returned %d\n", ret);
1156 printf("\n********* read %s ***************\n", Unit_info[ind].str);
1157 if( lseek(fd, 0, 0) == -1 ){
1158 printf("lseek(fd,0,0), %d, failed, errno %d\n",
1162 memset( buffer, 'B', 4096 );
1163 if ((ret=lio_read_buffer(fd, Unit_info[ind].method, buffer,
1164 size, Unit_info[ind].sig, &err, 0)) != size ) {
1165 printf(">>>>> lio_read_buffer(fd,0%x,buffer,%d,%d,err,0) returned -1,\n err = %s\n",
1166 Unit_info[ind].method, size, Unit_info[ind].sig, err);
1171 printf("lio_read_buffer returned %d\n", ret);
1174 for( i = 0; i < 4096; ++i ){
1175 if( buffer[i] != 'A' ){
1176 printf(" buffer[%d] = %d\n", i, buffer[i] );
1190 unlink("unit_test_file");