?? fifo_app.c
字號:
if ((write_size = write(filedes, outbuf, size + 1)) > size) { fprintf(stderr, "wrote %d bytes to %d size FIFO!\n", write_size, size); free(outbuf); close(filedes); return (-1); } free(outbuf); if ((close(filedes)) != 0) { fprintf(stderr, "close (%d): %s\n", filedes, strerror(errno)); return (errno); } return (0);}int write_test(const char *filename, const char *outbuf, int buf_size, int size){ int filedes; int write_size; int sofar; struct pollfd fifo_poll; write_size = 1; sofar = 0; if ((filedes = open(filename, O_WRONLY | O_NONBLOCK)) < 0) { fprintf(stderr, "open (%s, O_RDONLY): %s\n", filename, strerror(errno)); return (errno); } /* attempt to fill the FIFO buf_size chunks at a time */ fifo_poll.fd = filedes; fifo_poll.events = POLLOUT; while (sofar < size) { if ((poll(&fifo_poll, 1, 5000)) <= 0) { fprintf(stderr, "poll (&fifo_poll, 1, 5000): %s\n", strerror(errno)); close(filedes); return (errno); } if ((write_size = write(filedes, outbuf + sofar, buf_size)) < 0) { fprintf(stderr, "write (%d, outbuf, %d): %s\n", filedes, buf_size, strerror(errno)); close(filedes); return (errno); } sofar += write_size; } if ((close(filedes)) != 0) { fprintf(stderr, "close (%d): %s\n", filedes, strerror(errno)); return (errno); } return (0);}int write_read_test(const char *filename, int buf_size){ int filedes; char *outbuf; char inbuf[buf_size]; struct pollfd fifo_poll; if ((outbuf = get_random_str(buf_size)) == NULL) { return (-1); } if ((filedes = open(filename, O_RDWR | O_NONBLOCK)) < 0) { fprintf(stderr, "open (%s, O_RDWR): %s\n", filename, strerror(errno)); free(outbuf); return (errno); } /* write some stuff to the FIFO */ fifo_poll.fd = filedes; fifo_poll.events = POLLOUT; if ((poll(&fifo_poll, 1, 5000)) <= 0) { fprintf(stderr, "poll (&fifo_poll, 1, 5000): %s\n", strerror(errno)); free(outbuf); close(filedes); return (errno); } if ((write(filedes, outbuf, buf_size)) < 0) { fprintf(stderr, "write (%d, %s, %d): %s\n", filedes, outbuf, buf_size, strerror(errno)); free(outbuf); close(filedes); return (errno); } /* wait for data to arrive in FIFO */ fifo_poll.events = POLLIN; if ((poll(&fifo_poll, 1, 5000)) <= 0) { fprintf(stderr, "poll (&fifo_poll, 1, 5000): %s\n", strerror(errno)); free(outbuf); close(filedes); return (errno); } if ((read(filedes, inbuf, buf_size)) < 0) { fprintf(stderr, "read (%d, inbuf, %d): %s\n", filedes, buf_size, strerror(errno)); free(outbuf); close(filedes); return (errno); } if ((close(filedes)) != 0) { fprintf(stderr, "close (%d): %s\n", filedes, strerror(errno)); free(outbuf); return (errno); } /* make sure what we read is the same as what was sent */ if ((memcmp(outbuf, inbuf, buf_size)) != 0) { fprintf(stderr, "inbuf is not the same as outbuf\n"); free(outbuf); return (-1); } free(outbuf); return (0);}/* get random bytes from /dev/urandom and put them in a string *//* NOTE: this does NOT return NULL ("\0") terminated strings, so you * cannot use strlen() on strings that this function returns without * adding your own terminating NULL or changing this function */char *get_random_str(int size){ char *randstr; int randfile; if ((randstr = (char *) calloc(size, sizeof(char))) == NULL) { fprintf(stderr, "calloc (%d, %d): %s\n", size, sizeof(char), strerror(errno)); return (NULL); } if ((randfile = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) < 0) { fprintf(stderr, "open (\"/dev/urandom\", O_RDONLY | O_NONBLOCK): %s\n", strerror(errno)); free(randstr); return (NULL); } if ((read(randfile, randstr, size)) < size) { fprintf(stderr, "read (randfile, randstr, %d): %s\n", size, strerror(errno)); free(randstr); close(randfile); return (NULL); } if ((close(randfile)) != 0) { fprintf(stderr, "close (%d): %s\n", randfile, strerror(errno)); free(randstr); return (NULL); } return (randstr);}int sig_handler_setup(void){ struct sigaction *my_action; struct sigaction *old_action; if ( (my_action = (struct sigaction *) calloc(1, sizeof(struct sigaction))) == NULL) { fprintf(stderr, "calloc (1, %d): %s\n", sizeof(struct sigaction), strerror(errno)); return (errno); } my_action->sa_handler = &my_sahandler; if ( (old_action = (struct sigaction *) calloc(1, sizeof(struct sigaction))) == NULL) { fprintf(stderr, "calloc (1, %d): %s\n", sizeof(struct sigaction), strerror(errno)); return (errno); } sigaction(SIGHUP, my_action, old_action); sigaction(SIGINT, my_action, old_action); sigaction(SIGQUIT, my_action, old_action); return (0);}void my_sahandler(int whatever){ fprintf(stderr, "my_sahandler: received signal %d\n", whatever); unload_module(); exit(whatever);}int big_fifo_test(int test_nr){ char *filename; int fifo_size = 0, buf_size = 0, retval = 0; char *teststr, *resstr; if ((filename = construct_filename(test_nr)) == NULL) return (-1); fprintf(stderr, "Testing %s ", filename); if ((stat_test(filename)) != 0) { free(filename); return (errno); } fprintf(stderr, ". "); /* attempt to open while the module is not loaded; this should * fail internally */ if ((open_test_fail(filename)) != 0) { free(filename); return (errno); } fprintf(stderr, ". "); /* inner loop to test different FIFO sizes */ for (fifo_size = MIN_SIZE; fifo_size <= MAX_SIZE; fifo_size *= MUL_SIZE) { /* now we try to load the module */ if ((load_module(fifo_size, test_nr)) != 0) { free(filename); return (-1); } /* one last inner loop to test different read/write * sizes to/from FIFO */ for (buf_size = 1; buf_size <= fifo_size; buf_size *= MUL_SIZE) { /* write more than the size of the FIFO; should * fail internally */ if ((over_write_test(filename, fifo_size)) != 0) { unload_module(); free(filename); return (-1); } /* read more than the size of the FIFO; should * fail internally */ if ((over_read_test(filename, fifo_size)) != 0) { unload_module(); free(filename); return (-1); } /* get a random string for writing to the * FIFO */ if ((teststr = get_random_str(fifo_size)) == NULL) { unload_module(); free(filename); return (errno); } /* write to the FIFO in buf_size chunks */ if ( (write_test (filename, teststr, buf_size, fifo_size)) != 0) { unload_module(); free(filename); free(teststr); return (errno); } /* and read from the FIFO in buf_size chunks */ if ( (resstr = read_test(filename, buf_size, fifo_size)) == NULL) { unload_module(); free(filename); free(teststr); return (errno); } /* compare string written to string read to * make sure they are the same */ if ((retval = memcmp(resstr, teststr, fifo_size)) != 0) { fprintf(stderr, "strings not the same: %s\t%s!\n", resstr, teststr); unload_module(); free(filename); free(teststr); free(resstr); return (-1); } free(teststr); free(resstr); /* and do one last write/read test which * does its own internal string generation * and comparison */ if ((write_read_test(filename, buf_size)) != 0) { unload_module(); free(filename); return (errno); } } /* END buf_size loop */ /* now we try to unload the module */ if ((unload_module()) != 0) { free(filename); return (-1); } fprintf(stderr, "%d ", fifo_size); } /* END fifo_size loop */ fprintf(stderr, "passed.\n"); free(filename); return 0;} /* END all FIFOs loop */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -