/*---------------------------------------------------------------------+ | writer () | | ==================================================================== | | | | Function: Fill the "scratch" shared memory segment up with data and | | compute the segment checksum. | | Release "write" lock after completing so that the readers | | are able to start. | | | | Updates: cksum[] - array containing checksums computed by writers.| | data shared memory segment filled up. | | | +---------------------------------------------------------------------*/ void *writer (void *parm) { int num_w = (int) (long)parm; unsigned long cksum_w = 0; /* Shared memory regions checksum */ unsigned char data = 0; /* Value written into shared memory segment */ unsigned char *ptr; /* Misc pointer */ /* * Fill the "scratch" shared memory segment up with data and * compute the segments checksum. Release "write" lock after * completing so that the reader threads may begin to read the * data. */ data = num_w; for (ptr=shmptr[num_w]; ptr < (shmptr[num_w]+buffer_size); ptr++) { *ptr = (data++) % (UCHAR_MAX + 1); cksum_w += *ptr; } if (pthread_mutex_lock (&cond_mutex[num_w])) sys_error ("mutex_lock(&cond_mutex) failed", __LINE__); thread_hold[num_w]=0; if (pthread_cond_broadcast (&cond_var[num_w])) sys_error ("cond_signal(&cond_var) failed", __LINE__); if (pthread_mutex_unlock (&cond_mutex[num_w])) sys_error ("mutex_unlock(&cond_mutex) failed", __LINE__); cksum[num_w] = cksum_w; printf ("\t\twriter (%03d): shared memory checksum %08lx\n", num_w, cksum_w); return NULL; }
/*---------------------------------------------------------------------+ | release () | | ==================================================================== | | | | Function: Release shared memory regions. | | | +---------------------------------------------------------------------*/ void release () { int i; int j; for (i=0; i<num_writers; i++) { if (pthread_mutex_destroy(&cond_mutex[i]) != 0) sys_error ("Can't destroy cond_mutex", __LINE__); if (pthread_mutex_destroy(&mutex_r[i]) != 0) sys_error ("Can't destroy mutex_r", __LINE__); } for (i=0; i<num_writers; i++) { /* * Release shared memory regions */ j=i*3; if (shmctl (shmid[j], IPC_RMID, 0) < 0) sys_error ("read_count shmctl failed", __LINE__); j++; if (shmctl (shmid[j], IPC_RMID, 0) < 0) sys_error ("checksum shmctl failed", __LINE__); j++; if (shmctl (shmid[j], IPC_RMID, 0) < 0) sys_error ("shmptr shmctl failed", __LINE__); } }
/*---------------------------------------------------------------------+ | read_file () | | ==================================================================== | | | | Function: ... | | | +---------------------------------------------------------------------*/ int read_file(int fd, char *filename) { int bytes_read; int loop_count; long total_bytes; off_t lseek(); off_t file_offset = 0; int whence = 0; char buf[BLOCK_SIZE]; /* read file for "TIMES" number of times */ total_bytes = 0; if (debug) printf("\n"); for (loop_count = 1; loop_count <= TIMES; loop_count++) { while ((bytes_read = read(fd, buf, BLOCK_SIZE)) > 0) { if (bytes_read == -1) { sys_error("read failed", __FILE__, __LINE__); } else total_bytes = total_bytes + bytes_read; } if (lseek(fd, file_offset, whence) < 0) sys_error("lseek failed", __FILE__, __LINE__); if (debug) { printf("\r\ttotal bytes read = %ld", total_bytes); fflush(stdout); } total_bytes = 0; } if (debug) printf("\n"); return 1; }
static bool readHeader(file_t fp, const unsigned id) { resource_header_t header; U16 u16Temp; if (sysfile_read(fp, &header, sizeof(header), 1) != 1) { sys_error("(resources) unable to read header from \"%s\"", resourceFiles[id]); return false; } if (memcmp(header.magic, resource_magic, sizeof(header.magic)) != 0) { sys_error("(resources) wrong header for \"%s\"", resourceFiles[id]); return false; } memcpy(&u16Temp, header.version, sizeof(u16Temp)); u16Temp = htole16(u16Temp); if (u16Temp != DATA_VERSION) { sys_error("(resources) incompatible version for \"%s\"", resourceFiles[id]); return false; } memcpy(&u16Temp, header.resourceId, sizeof(u16Temp)); u16Temp = htole16(u16Temp); if (u16Temp != id) { sys_error("(resources) mismatching ID for \"%s\"", resourceFiles[id]); return false; } return true; }
static int net_listen(int *fd, struct addrinfo *ai) { int res; int _fd; const int yes = 1; _fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (_fd < 0) { sys_error(); return -1; } /* disable IPv4-to-IPv6 mapping */ if (ai->ai_family == AF_INET6) { res = setsockopt(_fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)); if (res) goto error_and_close; } res = setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); if (res) goto error_and_close; res = bind(_fd, ai->ai_addr, ai->ai_addrlen); if (res) goto error_and_close; res = listen(_fd, 6); if (res) goto error_and_close; if (fd) *fd = _fd; return 0; error_and_close: sys_error(); close(_fd); return -1; }
static int cp_cmd(int argc, char *argv[]) { FileHandle *file; int fd; if ((file = file_open_pathname(fs, 0, argv[1])) == 0) return 0; if ((fd = creat(argv[2], 0666)) == -1) { sys_error("cp", "could not open '%s' for writing", argv[2]); return 0; } while (file_read(file) > 0) { if (write(fd, file->buffer, file->nread) == -1) { sys_error("cp", "could not write to '%s'", argv[2]); file_close(file); return 0; } } if (close(fd) == -1) { sys_error("cp", "could not write to '%s'", argv[2]); file_close(file); return 0; } return 1; }
static void wi_start(unsigned count) { unsigned i; struct listen *curr; int e; for (curr = listen_head; curr; curr = curr->next) { wi_grow(count); for (i = 0; i < count; i++) { struct work_info *wi; wi = calloc(1, sizeof(*wi)); wi->active = true; wi->li = curr; e = pthread_create(&wi->thr, NULL, worker_loop, wi); if (e) { sys_error(); free(wi); break; } e = pthread_detach(wi->thr); if (e) { sys_error(); /* TODO: handle this error */ } wi = NULL; work[work_cur++] = wi; } } printf("thread count at %d\n", work_cur); }
void POL::usefile (int source, const char *fn) { FILE *fp; ++currentf; if (currentf >= MAXFILE) { --currentf; sys_error (ERR_SEVERE, "files nested too deeply"); return; } while (! m_stackPushBackInput.empty()) m_stackPushBackInput.pop(); if (source == P_USE_STR) { filep[currentf] = NULL; } else if (source == P_USE_FILE) { if (fn == NULL || strlen(fn) == 0) { fp = stdin; } else if ((fp = fopen(fn, "r")) == NULL) { --currentf; sys_error (ERR_SEVERE, "can't open file"); return; } filep[currentf] = fp; fname[currentf] = strdup (fn); } }
/*---------------------------------------------------------------------+ | fork_realtime () | | ==================================================================== | | | | Function: ... | | | +---------------------------------------------------------------------*/ int fork_realtime(char **args) { int pid; char *results_file = args[2]; char *priority = args[3]; /* fork process then determine if process is parent or child */ pid = fork(); switch (pid) { /* fork failed */ case -1: sys_error("fork failed", __FILE__, __LINE__); /* child process */ case 0: if (execl(*args, *args, REAL_TIME, results_file, priority, NO_FORK, NULL) < 0) sys_error("execl failed", __FILE__, __LINE__); /* parent process */ default: #ifdef DEBUG printf("\tparent process id = %d\n", getpid()); printf("\tchild process id = %d\n\n", pid); #endif break; } return (pid); }
/*---------------------------------------------------------------------+ | process_file () | | ==================================================================== | | | | Function: Opens a file, reads from it until it encounters an | | end-of-file and then closes the file.. | | | +---------------------------------------------------------------------*/ void process_file(char *filename) { char record[100]; /* holds each record of the file read */ FILE *datafile; /* file pointer to the open file */ /* * Try and open the datafile */ if ((datafile = fopen(filename, "r")) == NULL) sys_error("fopen failed", __FILE__, __LINE__); /* * Read the first record of the datafile, then read until end-of-file */ while (fgets(record, 80, datafile)) { if (feof(datafile)) break; } /* * Close the datafile */ if (fclose(datafile)) sys_error("fclose failed", __FILE__, __LINE__); }
void POL::getescape ( /* reads up to delim */ char *s, int delim, int toksiz ) { int i, c; for (i = 0; (c = inchar()) != delim; i++) { if (c == NEWLINE) { sys_error (ERR_WARNING, "Missing closing delimiter."); break; } if (i >= toksiz) { sys_error (ERR_SEVERE, "string too long."); break; } if (c == EOF) { ungetch(c); sys_error (ERR_SEVERE, "end of file inside quotation"); break; } else if (c == BSLASH) { /* escape character */ s[i++] = c; c = inchar(); /* get escaped character */ } s[i] = c; } s[i] = EOS; }
rpcbuf* rpcbuf::nonblocking(boolean nonblocking) { if (!_opened) { error("rpcbuf::nonblocking: not using a file number yet"); return nil; } if (_nonblocking != nonblocking) { int flags = fcntl(_fd, F_GETFL, 0); if (flags < 0) { sys_error("rpcbuf::nonblocking: F_GETFL fcntl"); return nil; } if (nonblocking) { flags |= O_NDELAY; } else { flags &= ~O_NDELAY; } if (fcntl(_fd, F_SETFL, flags) < 0) { sys_error("rpcbuf::nonblocking: F_SETFL fcntl"); return nil; } } _nonblocking = nonblocking; return this; }
static void init_files(void) { close(0); if ((Acceptfd = open("/dev/null", O_RDWR)) != 0) { logmessage("Trouble opening /dev/null"); sys_error(E_SYS_ERROR, EXIT | NOCORE); } close(1); if ((Sacpipefd = open(SACPIPE, O_RDWR|O_NDELAY)) != 1) { logmessage(sacopenmsg); error(E_CREAT, EXIT | NOCORE | NO_MSG); } close(2); if ((Pmpipefd = open(PMPIPE, O_RDWR|O_NDELAY)) != 2) { logmessage(pmopenmsg); error(E_CREAT, EXIT | NOCORE | NO_MSG); } close(3); if ((Passfd = dup(Acceptfd)) != 3) { logmessage("Trouble duping /dev/null"); sys_error(E_SYS_ERROR, EXIT | NOCORE); } }
uint64_t blkio_read(DevInfo *dev, void *buf, uint64_t offset, uint64_t count) { int bytes; if (offset > dev->bytes) { error("blkio_read", "offset 0x%" PRIx64 " > disk size 0x%" PRIx64, offset, dev->bytes); return -1; } if (lseek(dev->fd, offset, SEEK_SET) == -1) { sys_error("blkio_read", "seek to 0x%" PRIx64 " failed", offset); return -1; } if ((bytes = read(dev->fd, buf, count)) == -1) { sys_error("blkio_read", "read failed"); return -1; } if (bytes < count) { error("blkio_read", "short read - wanted 0x%" PRIx64 " bytes, got 0x%" PRIx64 " bytes", count, bytes); return -1; } if (each_block_fn) each_block_fn(dev, buf, offset, count); return bytes; }
int get_conn_to_host(const char* host, int port, int socktype) { sockfd_t sock; struct addrinfo *rp, *result = getnetinfo(host, port, socktype); for (rp = result; rp != NULL; rp = rp->ai_next) { sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sock >= 0) break; } if (rp == NULL) { freeaddrinfo(result); return sys_error("socket"); } if (socktype == SOCK_STREAM) { if (connect(sock, rp->ai_addr, serv.socksize) == -1) { evt_close(sock); freeaddrinfo(result); return sys_error("connect"); } } freeaddrinfo(result); return sock; }
/*---------------------------------------------------------------------+ | main | | ==================================================================== | | | | Function: ... | | | +---------------------------------------------------------------------*/ int main (int argc, char **argv) { long start_time; /* time at start of testcase */ int i; /* * Setup signal handler & setup alarm so we do not loop forever... */ signal (SIGUSR1, signal_handler); signal (SIGALRM, signal_handler); alarm (600); /* wait 10 minutes before aborting */ /* * Process command line arguments... */ parse_args (argc, argv); if (verbose) printf ("%s: Scheduler TestSuite program\n\n", *argv); if (debug) { printf ("\tpriority: %s\n", priority); } /* * Adjust the priority of this process if the real time flag is set */ if (!strcmp (priority, "fixed")) { #ifndef __linux__ if (setpri (0, DEFAULT_PRIORITY) < 0) sys_error ("setpri failed", __FILE__, __LINE__); #else if (setpriority(PRIO_PROCESS, 0, 0) < 0) sys_error ("setpri failed", __FILE__, __LINE__); #endif } /* * Continuously multiply matrix as time permits... */ i = 0; start_time = time ((long *) 0); /* * Continuously read through file until interrupted... */ if (debug) printf ("\n"); while (!signaled) { if (debug) { printf ("\r\tmultiplying matrix [%d]", i++); fflush (stdout); } multiply_matrices (); } if (debug) printf ("\n"); /* * Exit with success! */ if (verbose) printf ("\nsuccessful!\n"); return (0); }
/*---------------------------------------------------------------------+ | main | | ==================================================================== | | | | Function: ... | | | +---------------------------------------------------------------------*/ int main (int argc, char **argv) { long start_time; /* time at start of testcase */ int i; /* * Process command line arguments... */ if (argc < 2) { fprintf (stderr, USAGE, *argv); exit (0); } parse_args (argc, argv); if (verbose) printf ("%s: Scheduler TestSuite program\n\n", *argv); if (debug) { printf ("\tpriority: %s\n", priority); printf ("\texecution_time: %ld (sec)\n", execution_time); } /* * Adjust the priority of this process if the real time flag is set */ if (!strcmp (priority, "fixed")) { #ifndef __linux__ if (setpri (0, DEFAULT_PRIORITY) < 0) sys_error ("setpri failed", __FILE__, __LINE__); #else if (setpriority(PRIO_PROCESS, 0, 0) < 0) sys_error ("setpri failed", __FILE__, __LINE__); #endif } /* * Continuously multiply matrix as time permits... */ i = 0; start_time = time ((long *) 0); if (debug) printf ("\n"); while ( (time ((long *)0) - start_time) < execution_time) { if (debug) { printf ("\r\tmultiplying matrix [%d], time left: %ld", i++, execution_time - (time ((long *)0)-start_time)); fflush (stdout); } multiply_matrices (); } if (debug) printf ("\n"); /* * Exit with success! */ if (verbose) printf ("\nsuccessful!\n"); return (0); }
/*---------------------------------------------------------------------+ | signal_init () | | ==================================================================== | | | | Function: Take default action for all signals except for SIGCHLD. | | Particularily interrested in the following: | | | | o SIGSTOP (17) - default action: stop | | o SIGCONT (19) - default action: stop | | o SIGKILL (09) - default action: abort process | | | | Process signal handler upon receiving the following: | | (Sent when child process stops or exits) | | | | o SIGCHLD (20) | | | | Returns: n/a | | | +---------------------------------------------------------------------*/ void signal_init() { struct sigaction action; char msg[100]; int i; for (i = 1; i <= SIGMAX; i++) { /* Do not ignore SIGCHILD signal */ #ifdef _IA64 /* SIGWAITING not supported, RESERVED */ if (i == SIGCHLD || i == SIGALRM || i == SIGUSR1 || i == SIGWAITING) continue; #else #ifdef _LINUX_ if ((i == SIGKILL) || (i == SIGSTOP) || ((i >= 32) && (i <= 34))) continue; #else if ((i == SIGKILL) || (i == SIGSTOP) || (i == SIGCONT)) continue; #endif #endif action.sa_handler = SIG_DFL; sigfillset(&action.sa_mask); action.sa_flags = SA_RESTART; if (sigaction(i, &action, NULL) < 0) { sprintf(msg, "sigaction failed on signal %d", i); error(msg, __LINE__); } } /* Setup signal handler for SIGCHLD & SIGUSR1 signals */ action.sa_handler = (void (*)(int))handler; sigfillset(&action.sa_mask); /* changing */ /*action.sa_flags = 0; */ action.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &action, NULL) < 0) sys_error("sigaction (SIGCHLD) failed", __LINE__); /* changing */ action.sa_flags = SA_RESTART; /* end of changing */ if (sigaction(SIGUSR1, &action, NULL) < 0) sys_error("sigaction (SIGUSR1) failed", __LINE__); /* Setup signal handler for SIGALRM */ action.sa_handler = (void (*)(int))handler; if (sigaction(SIGALRM, &action, NULL) < 0) sys_error("sigaction (SIGCHLD) failed", __LINE__); }
/*---------------------------------------------------------------------+ | init_sig_vec () | | ==================================================================== | | | | Function: Initialize the signal vector for ALL possible signals | | (as defined in /usr/include/sys/signal.h) except for | | the following signals which cannot be caught or ignored: | | | | o SIGKILL (9) | | o SIGSTOP (17) | | o SIGCONT (19) | | | | Returns: Nothing | | | +---------------------------------------------------------------------*/ void init_sig_vec() { char errmsg[256]; int i; #ifdef _LINUX_ static struct sigaction invec; for (i = 1; i <= SIGMAX; i++) { if ((i == SIGKILL) || (i == SIGSTOP) || ((i >= 32) && (i <= 34))) continue; invec.sa_handler = handler; //sigaction.sa_mask = 0; invec.sa_flags = 0; if (sigaction(i, &invec, NULL)) { sprintf(errmsg, "init_sig_vec: sigaction failed on signal (%d)", i); perror(errmsg); sys_error(errmsg, __LINE__); } } #else static struct sigvec invec; for (i = 1; i <= SIGMAX; i++) { /* Cannot catch or ignore the following signals */ #ifdef _IA64 /* SIGWAITING NOT supported, RESERVED */ if ((i == SIGKILL) || (i == SIGSTOP) || (i == SIGCONT) || (i == SIGWAITING)) continue; #else if ((i == SIGKILL) || (i == SIGSTOP) || (i == SIGCONT)) continue; #endif invec.sv_handler = handler; //invec.sv_mask = SA_NOMASK; #if defined _IA64 invec.sv_flags = 1; #else invec.sv_onstack = 1; #endif if (sigvec(i, &invec, NULL)) { sprintf(errmsg, "init_sig_vec: sigvec failed on signal (%d)", i); perror(errmsg); sys_error(errmsg, __LINE__); } } #endif //ifdef _LINUX_ }
/*---------------------------------------------------------------------+ | main | | ==================================================================== | | | | Function: ... | | | +---------------------------------------------------------------------*/ int main(int argc, char **argv) { char *filename = NULL; if ((filename = getenv("KERNEL")) == NULL) { errno = ENODATA; sys_error("environment variable KERNEL not set", __FILE__, __LINE__); } /* * Setup signal handler & setup alarm so we do not loop forever... */ signal(SIGUSR1, signal_handler); signal(SIGALRM, signal_handler); alarm(600); /* wait 10 minutes before aborting */ /* * Process command line arguments... */ parse_args(argc, argv); if (verbose) printf("%s: Scheduler TestSuite program\n\n", *argv); if (debug) { printf("\tpriority: %s\n", priority); } /* * Adjust the priority of this process if the real time flag is set */ if (!strcmp(priority, "fixed")) { #ifndef __linux__ if (setpri(0, DEFAULT_PRIORITY) < 0) sys_error("setpri failed", __FILE__, __LINE__); #else if (setpriority(PRIO_PROCESS, 0, 0) < 0) sys_error("setpri failed", __FILE__, __LINE__); #endif } /* * Continuously read through file until interrupted... */ while (!signaled) process_file(filename); /* * Exit with success! */ if (verbose) printf("\nsuccessful!\n"); return (0); }
/* ----------------------------------------------------------------- MAIN: This is started by the IBP server in the source depot, it receives as parameters name of target server, port, and readfd, stablishes a connection with the server and sends the data ----------------------------------------------------------------- */ main (int argc, char **argv) { int *connectfds, sourcefd, numtargets, i, nargs; int retval; unsigned long int filesize; double rtmp, result; double s; char **targets, **ports; float rate; fprintf(stderr,"\nTCP DATA MOVER:\n%s %s %s %s %s %s <%s>\n\n\n", argv[0],argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]); if(argc < 7) sys_error("Incorrect parameters: smclientTCP <target_hostname> <port_Number> <file_descriptor> <filesize> <numtargets> <IBP_cap>"); sourcefd = atoi(argv[3]); filesize = atol(argv[4]); numtargets = atoi(argv[5]); strcpy(glb.IBP_cap,argv[6]); targets = String2Array(argv[1],numtargets,MAX_HOST_LEN); ports = String2Array(argv[2],numtargets,MAX_WORD_LEN); connectfds = (int *) malloc(sizeof(int) * numtargets); for(i=0;i<numtargets;i++){ if((connectfds[i]= tcp_connect(targets[i],ports[i]))<0) sys_error("connectfd error: host %d failed"); } s = seconds(); retval = SendData2(sourcefd, connectfds, filesize, numtargets); if(retval<0){ fprintf(stderr,"DM mclientTCP problem sending data\n"); exit(TCP_FAIL); } s = seconds()-s; rate = (s !=0) ? filesize/(1024.0*1024*(s)) : 0; fprintf(stderr,"\nTransfer time = %6.2f secs ",s); fprintf(stderr," size = %d xfer rate = %.2f MB/s (%.2fMb/s)\n", filesize, rate, rate*8); for(i=0;i<numtargets;i++) close(connectfds[i]); free(connectfds); free(targets); free(ports); exit(TCP_OK); }
/*---------------------------------------------------------------------+ | thread () | | ==================================================================== | | | | Function: Recursively creates threads while num < num_threads | | | +---------------------------------------------------------------------*/ void *thread(void *parm) { intptr_t num = (intptr_t) parm; pthread_t th; pthread_attr_t attr; size_t stacksize = 1046528; int pcrterr; /* * Create threads while num < num_threads... */ if (test_limit || (num < num_threads)) { if (pthread_attr_init(&attr)) sys_error("pthread_attr_init failed", __LINE__); if (pthread_attr_setstacksize(&attr, stacksize)) sys_error("pthread_attr_setstacksize failed", __LINE__); if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) sys_error("pthread_attr_setdetachstate failed", __LINE__); /************************************************/ /* pthread_create does not touch errno. It RETURNS the error * if it fails. errno has no bearing on this test, so it was * removed and replaced with return value check(see man page * for pthread_create(); */ pcrterr = pthread_create(&th, &attr, thread, (void *)(num + 1)); if (pcrterr != 0) { if (test_limit) { tst_resm(TINFO, "Testing pthread limit, %d pthreads created.", (int)num); pthread_exit(0); } if (pcrterr == EAGAIN) { tst_resm(TINFO, "Thread [%d]: unable to create more threads!", (int)num); return NULL; } else sys_error("pthread_create failed", __LINE__); } pthread_join(th, (void *)NULL); } return 0; /* pthread_exit(0); */ }
void* server_worker(void* arg) { sockfd_t sock = 0; struct addrinfo *rp; for (rp = serv.ai; rp != NULL; rp = rp->ai_next) { sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sock < 0) continue; int ret = 1; int err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &ret, 4); if (err != 0) { sys_error("setsockopt: SO_REUSEADDR"); } if (bind(sock, rp->ai_addr, rp->ai_addrlen) == 0) { break; } } if (rp == NULL) { sys_error("bind"); goto server_worker_exit; } if (set_nonblocking(sock) == -1) { goto server_worker_exit; } if (serv.socktype == SOCK_STREAM) { if (listen(sock, 5) == -1) { sys_error("listen"); goto server_worker_exit; } } serv.serv_sock = sock; struct event evt; event_set(&evt, sock, EV_READ | EV_PERSIST, serv.handler, arg); event_add(&evt, NULL); event_dispatch(); server_worker_exit: evt_close(sock); #ifdef _WIN32 WSACleanup(); #endif return NULL; }
/*---------------------------------------------------------------------+ | setup_handler () | | ==================================================================== | | | | Function: Setup the signal handler for SIGPIPE. | | | +---------------------------------------------------------------------*/ void setup_signal_handlers () { struct sigaction invec; invec.sa_handler = (void (*)(int)) handler; sigemptyset (&invec.sa_mask); invec.sa_flags = 0; if (sigaction (SIGINT, &invec, (struct sigaction *) NULL) < 0) sys_error ("sigaction failed", __LINE__); if (sigaction (SIGPIPE, &invec, (struct sigaction *) NULL) < 0) sys_error ("sigaction failed", __LINE__); }
static int read_data(int num, unsigned long *cksum) { int fd; const int bufSize = 1024; char *buffer; int bytes_read = 0; int n; char *p; if (debug) printf("\tThread [%d]: read_data()\n", num); if ((fd = open(file, O_RDONLY, NULL)) < 0) sys_error("open failed /dev/cdrom", __LINE__); buffer = malloc(sizeof(char) * bufSize); assert(buffer); lseek(fd, 1024 * 36, SEEK_SET); while (bytes_read < num_bytes) { n = read(fd, buffer, bufSize); if (n < 0) sys_error("read failed", __LINE__); else if (n == 0) sys_error("End of file", __LINE__); bytes_read += n; for (p = buffer; p < buffer + n; p++) *cksum += *p; if (debug) printf("\tThread [%d] bytes read: %5d checksum: " "%-#12lx\n", num, bytes_read, *cksum); } free(buffer); if (debug) printf("\tThread [%d] bytes read: %5d checksum: %-#12lx\n", num, bytes_read, *cksum); if (close(fd) < 0) sys_error("close failed", __LINE__); if (debug) printf("\tThread [%d]: done\n", num); return (0); }
int mysocket::myconnect(){ int ret; serverlen = sizeof(sockaddr); if((ret = ::connect(listenfd, (sockaddr*)&serveraddr, serverlen)) < 0) sys_error("connect"); return ret; }
static int fill(struct channel *ch, struct buffer *bu) { ssize_t res; if (!ch->connected) return -1; if (bu->max == bu->cur) return -1; if (bu->cur) return 0; do { assert(ch->fd > 0); assert(bu->cur < bu->max); res = recv(ch->fd, bu->data + bu->cur, bu->max - bu->cur, 0); } while (res < 0 && errno == EINTR); if (res <= 0) { if (res < 0) sys_error(); ch_close(ch); return -1; } bu->cur += res; return 0; }
/*---------------------------------------------------------------------+ | create_semaphores () | | ==================================================================== | | | | Function: Creates two semaphores: | | | | READ_COUNT: shared read semaphore | | WRITE: exclusive write semaphore | | | | Updates: semid - system wide semaphore identifier | | | +---------------------------------------------------------------------*/ static void create_semaphores() { int nsems = 2; /* Number of semaphores to create */ /* * Create two system unique semaphores. */ if ((semid = semget(IPC_PRIVATE, nsems, IPC_CREAT | 0666)) < 0) sys_error("semget failed", __LINE__); arg.val = 1; if (semctl(semid, WRITE, SETVAL, arg) < 0) sys_error("semctl (SETVAL) failed", __LINE__); if (semctl(semid, READ_COUNT, SETVAL, arg) < 0) sys_error("semctl (SETVAL) failed", __LINE__); }
static struct module *mo_load(const char *name, const char *dll_path, const char *args) { struct module *mo; mo = mo_find(name); if (mo) { fprintf(stderr, "module '%s' is already loaded\n", name); return NULL; } mo = calloc(1, sizeof(*mo)); if (!mo) { sys_error(); return NULL; } if (dll_open(&mo->mh, dll_path, args)) goto error; mo->name = strdup(name); mo->next = module_head; module_head = mo; printf("MODULE:%s\n", name); return mo; error: free(mo); return NULL; }
int rd_right(t_system *sys) { t_cmd *tmp; int fd; tmp = sys->cmd->prev; fd = 0; delete_link(&(sys->cmd)); if (sys->cmd == NULL) return (0 * printf("error in command line \n")); sys->cmd->separator = 2; if (rdr_open(sys, &fd) == 0) return (0); delete_link(&(sys->cmd)); if (sys_error(close_fd(sys->pipe.pipefd[1])) != 1) return (0); sys->pipe.pipefd[0] = 0; sys->pipe.pipefd[1] = fd; if (sys->pipe.last_open == 0) sys->pipe.last_open = 1; while (sys->cmd != NULL && (sys->cmd->separator == 2 || sys->cmd->separator == 3)) if (rdr_checkout(sys) == 0) return (0); sys->cmd = tmp; sys->pipe.file = true; return (1); }