int client_process(Configuration *config) { DataPack standartMessage; if (convert_message_to_datapack(messageString,sizeof(messageString),&standartMessage)) { fputs("Client: Failed to create standart message\n", stderr); config->work = 0; kill(0, SIGTERM); return -1; } DEBUGMSG("Starting server wait"); if (wait(config->outputDevice)) { config->work = 0; puts("Test FAILED"); kill(0, SIGTERM); return -1; } DEBUGMSG("Starting transfere"); if(read_and_compare(config->outputDevice, &standartMessage)) { puts("Test COM - FAILED"); config->work = 0; kill(0, SIGTERM); return -1; } config->work = 0; puts("client process - OK"); return 0; }
int main (int argc, char **argv) { unsigned long i, j; int fd; char *buf, *read_buf, *mmap_buf, *filename = "/dev/asgn1"; int nproc = 12345; srandom (getpid ()); if (argc > 1) filename = argv[1]; if ((fd = open (filename, O_RDWR)) < 0) { fprintf (stderr, "open of %s failed: %s\n", filename, strerror (errno)); exit (1); } assert(buf = malloc(SIZE)); for (i = 0; i < SIZE; i++) { buf[i] = random() % 256; } if (SIZE != my_fwrite(fd, buf, SIZE)) { fprintf (stderr, "write problem: %s\n", strerror (errno)); exit(1); } #if 0 /* have the driver tell us the buffer size */ if (ioctl (fd, MMAP_DEV_CMD_GET_BUFSIZE, &len) < 0) { fprintf (stderr, "ioctl failed: %s\n", strerror (errno)); exit (1); } printf ("driver's ioctl says buffer size is %ld\n", len); #endif assert(read_buf = malloc(SIZE)); mmap_buf = mmap (NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mmap_buf == (char *)MAP_FAILED) { fprintf (stderr, "mmap of %s failed: %s\n", filename, strerror (errno)); exit (1); } printf ("mmap succeeded: %p\n", mmap_buf); (void)lseek (fd, 0, SEEK_SET); read_and_compare (fd, buf, mmap_buf, SIZE); printf ("comparison of same data via read() and mmap() successful\n"); /* Change one randomly chosen byte in the mmap region */ j = random () % SIZE; *(mmap_buf + j) = random () % j; printf ("modified mmap_buf\n"); /* repeat the read-back comparison. */ (void)lseek (fd, 0, SEEK_SET); read_and_compare (fd, read_buf, mmap_buf, SIZE); printf ("comparison of modified data via read() and mmap() successful\n"); (void)lseek (fd, 0, SEEK_SET); if (ioctl (fd, ASGN1_SET_NPROC, &nproc) < 0) { fprintf (stderr, "ioctl failed: %s\n", strerror (errno)); exit (1); } printf("nproc set to %d\n", nproc); return 0; }
static void fork_and_wait (int argc, const char **argv, unsigned int timeout_usec, int do_events, int expect_ok, int kill_child, const char * output, const char * error) { pid_t pid; int stdout_fd[2]; int stderr_fd[2]; int ret = 0; /* open a socketpair for stdout */ ret = socketpair (AF_UNIX, SOCK_STREAM, 0, stdout_fd); MY_ASSERT (ret == 0); /* open a socketpair for stderr */ ret = socketpair (AF_UNIX, SOCK_STREAM, 0, stderr_fd); MY_ASSERT (ret == 0); pid = fork (); MY_ASSERT (pid >= 0); if (pid == 0) { /* child */ int ret = 0; /* close stdin */ ret = close (0); MY_ASSERT (ret == 0); /* close then duplicate stdout and stderr */ ret = close (stdout_fd[1]); MY_ASSERT ( ret == 0); ret = close (stderr_fd[1]); MY_ASSERT ( ret == 0); ret = dup2 (stdout_fd[0], 1); MY_ASSERT ( ret != -1); ret = dup2 (stderr_fd[0], 2); MY_ASSERT ( ret != -1); ret = lwes_event_testing_emitter_main (argc, (char **) argv); exit (ret); } else { /* parent */ int status; pid_t child; int ret = 0; ret = close (stdout_fd[0]); MY_ASSERT (ret == 0); ret = close (stderr_fd[0]); MY_ASSERT (ret == 0); if (do_events) { usleep (timeout_usec); generate_events (); usleep (10 * timeout_usec); if (kill_child) { kill (pid, SIGINT); } } read_and_compare (output, stdout_fd[1]); read_and_compare (error, stderr_fd[1]); /* wait for the child to end */ child = wait (&status); MY_ASSERT (child == pid); ret = (WEXITSTATUS (status) == 0); MY_ASSERT (ret == expect_ok); (void) expect_ok; ret = close (stdout_fd[1]); MY_ASSERT (ret == 0); ret = close (stderr_fd[1]); MY_ASSERT (ret == 0); } }