int main(int argc, char *argv[]) { struct stat a, b; char path[] = "test_O_TRUC"; int fd = eopen(path, O_RDWR | O_CREAT | O_TRUNC); efstat(fd, &a); eclose(fd); fd = eopen(path, O_RDWR | O_CREAT | O_TRUNC); efstat(fd, &b); aver(a.st_ino == b.st_ino); eclose(fd); eunlink(path); return 0; }
void fb_die(struct framebuffer *fb) { cmap_die(fb->cmap); cmap_die(fb->cmap_org); free(fb->buf); emunmap(fb->fp, fb->screen_size); eclose(fb->fd); }
int main(int argc, char *argv[]) { char buf[10]; int fd = ecreat("zerofile"); ftruncate(fd, 497); lseek(fd, 5046, 0); ewrite(fd, buf, 0); eclose(fd); return 0; }
static void verify_sync(void) { int fd = eopen(Option.file, O_RDONLY); int rc = eread(fd, Data, sizeof(Data)); if (rc != sizeof(Data)) { fatal("Expeted %d bytes read but read %d", sizeof(Data), rc); } verify_data(); eclose(fd); }
//saves a segment to harddisk bool dump_seg_to_disk(ulong n) { segment_t *curseg; uchar *segdata; char *answer; FILE *file; curseg = getnseg(n); //show "save file" dialog answer = askfile_cv(1,get_segm_name(curseg),"Enter a filename for the segment:",0); if(answer == NULL) { return false; } //get copy of segment and save it to disk segdata = get_segment_data(curseg->startEA, curseg->endEA, getsegsize(curseg)); file = fopenWB(answer); ewrite(file, segdata, getsegsize(curseg)); eclose(file); free(segdata); return true; }
int main (int argc, char **argv) { char buf[4096]; char *pattern; int fd_in, bytes_read; int grep_pipe[2], less_pipe[2]; if (argc < 3) return -1; signal(SIGPIPE, int_handler); signal(SIGINT, int_handler); pattern = argv[1]; for (int i = 2; i < argc; i++) { if ((fd_in = open(argv[i], O_RDONLY)) < 0) { fprintf(stderr, "Error while opening '%s' for reading: %s\n", argv[i], strerror(errno)); continue; } if (pipe(grep_pipe) < 0) { fprintf(stderr, "Error while creating grep pipe: %s\n", strerror(errno)); return -1; } if (pipe(less_pipe) < 0) { fprintf(stderr, "Error while creating less pipe: %s\n", strerror(errno)); return -1; } if ((grep_pid = fork()) < 0) { eclose(fd_in); eclose(grep_pipe[0]); eclose(grep_pipe[1]); eclose(less_pipe[0]); eclose(less_pipe[1]); fprintf(stderr, "Error while forking: %s\n", strerror(errno)); return -1; } else if (grep_pid == 0) { dup2(grep_pipe[0], STDIN_FILENO); dup2(less_pipe[1], STDOUT_FILENO); eclose(fd_in); eclose(grep_pipe[0]); eclose(grep_pipe[1]); eclose(less_pipe[0]); eclose(less_pipe[1]); if (execl("/bin/grep", "grep", "-e", pattern, NULL) < 0) { fprintf(stderr, "Error while executing grep: %s\n", strerror(errno)); return -1; } } else { eclose(grep_pipe[0]); } if ((less_pid = fork()) < 0) { eclose(fd_in); eclose(grep_pipe[1]); eclose(less_pipe[0]); eclose(less_pipe[1]); fprintf(stderr, "Error while forking: %s\n", strerror(errno)); continue; } else if (less_pid == 0) { dup2(less_pipe[0], STDIN_FILENO); eclose(fd_in); eclose(grep_pipe[1]); eclose(less_pipe[0]); eclose(less_pipe[1]); if (execl("/bin/less", "less", NULL) < 0) { fprintf(stderr, "Error while executing less: %s\n", strerror(errno)); return -1; } } else { eclose(less_pipe[0]); eclose(less_pipe[1]); } if (sigsetjmp(int_jb, 1) != 0) { // SIGPIPE returns here dwFilesProcessed++; eclose(fd_in); eclose(grep_pipe[1]); continue; } dwFilesProcessed++; do { bytes_read = read(fd_in, buf, 4096); if (bytes_read < 0) { fprintf(stderr, "Error while reading '%s': %s\n", argv[i], strerror(errno)); return -1; } if (bytes_read > 0) { int res, size; char *buf_write; buf_write = buf; size = bytes_read; while (size > 0) { res = write(grep_pipe[1], buf_write, size); if (res < 0) { fprintf(stderr, "Error while writing to pipe: %s\n", strerror(errno)); return -1; } size -= res; buf_write += res; dwBytesProcessed += res; } } } while (bytes_read != 0); eclose(fd_in); eclose(grep_pipe[1]); // No deadlocks here because if a state already has changed, waitpid instantly returns waitpid(grep_pid, NULL, 0); waitpid(less_pid, NULL, 0); } return 0; }
main(int argc, char **argv) { FILE *ifp; /* file pointer for input */ FILE *hfp; /* file pointer for popen write */ int bfd; /* file descriptor for bfile */ string tape; /* name of raw tape device */ int clean; /* clean trace header */ int verbose; /* echo every 20th trace */ int over; /* check format */ int convert; /* convert ibm fpt to ieee fpt */ string hfile; /* name of ascii header file */ string bfile; /* name of binary header file */ int trmin; /* first trace to read */ int trmax; /* last trace to read */ int nt; /* number of data samples */ char cmdbuf[BUFSIZ]; /* dd command buffer */ char ebcbuf[EBCBYTES]; /* ebcdic data buffer */ int itr = 0; /* current trace number */ bool nsflag = FALSE; /* flag for error in tr.ns */ char hdr_buf[10]; /* 1st 10 bytes of header in ascii */ char tmp_buf[3600]; /* temp. buffer to read in header */ unsigned int nsamp; /* number of samples per trace */ int i; /* loop counter to zero trace samples */ int *ibstart,*ibyte,*itype; int *obstart,*obyte,*otype; int nmap=0, imap; int ibs,iby,ity,obs,oby,oty; short itmp2; int itmp4; float tmp; int ntg=0; int rmbadtrace, ibt, nbt; /* initialize */ initargs(argc, argv); askdoc(1); /* make sure stdout is a file or pipe */ switch(filestat(STDOUT)) { case TTY: err("stdout can't be tty"); break; case DIRECTORY: err("stdout must be a file, not a directory"); break; case BADFILETYPE: err("stdout is illegal filetype"); break; } /* set filenames */ if (!getparstring("tape", &tape)) { ifp = stdin; file2g(ifp); } else { /* open files - first the tape */ ifp = efopen(tape, "r"); } file2g(stdout); /* set parameters */ if (!getparint("clean", &clean)) clean = 1; if (!getparint("verbose", &verbose)) verbose = 0; if (!getparint("over", &over)) over = 0; if (!getparint("convert", &convert)) convert = 1; if (!getparint("trmin", &trmin)) trmin = 1; if (!getparint("trmax", &trmax)) trmax = LONG_MAX; if (!getparint("rmbadtrace",&rmbadtrace)) rmbadtrace=0; nmap = countparval("ibstart"); if(nmap>0) { ibstart = (int*) malloc(nmap*sizeof(int)); ibyte = (int*) malloc(nmap*sizeof(int)); itype = (int*) malloc(nmap*sizeof(int)); obstart = (int*) malloc(nmap*sizeof(int)); obyte = (int*) malloc(nmap*sizeof(int)); otype = (int*) malloc(nmap*sizeof(int)); if(getparint("ibstart",ibstart)!=nmap) err(" check ibstart"); if(getparint("ibyte",ibyte)!=nmap) err(" check ibyte"); if(getparint("itype",itype)!=nmap) err(" check itype"); if(getparint("obstart",obstart)!=nmap) err(" check obstart"); if(getparint("obyte",obyte)!=nmap) err(" check obyte"); if(getparint("otype",otype)!=nmap) err(" check otype"); } /* read ebcdic and binary headers */ efread(ebcbuf, 1, EBCBYTES, ifp); efread((char *)&bh, 1, BNYBYTES, ifp); if (bh.format != 1) (over) ? warn("ignore bh.format ... continue") : err("format not IBM floating point"); if (!convert) warn( "assuming data is IEEE floating point, no conversion will be done"); /* set nt parameter */ if (!getparint("nt", &nt)) { nt = bh.hns; ntg = 0; } else { ntg = 1; } /* if needed, save ebcbuf into hfile */ if (getparstring("hfile", &hfile)) { /* Open pipe to use dd to convert ebcdic to ascii */ sprintf(cmdbuf, "dd ibs=3200 of=%s conv=ascii cbs=80 count=1", hfile); hfp = epopen(cmdbuf, "w"); /* Write ascii stream from buffer into pipe */ efwrite(ebcbuf, EBCBYTES, 1, hfp); epclose(hfp); } /* save the binary file, if needed */ if (getparstring("bfile", &bfile)) { /* - the binary data file */ bfd = eopen(bfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); /* Write binary header from bhed structure to binary file */ ewrite(bfd, (char *)&bh, BNYBYTES); eclose(bfd); } /* convert ebcdic to ascii for output data */ tascii_((unsigned char*)ebcbuf, (unsigned char*)&ch, EBCBYTES, 0); if (strncmp((char*)&ch, "C 1 CLIENT",10) != 0 ) { memcpy((char *)&ch, "C 1 CLIENT", 10); } /* test if number of samples set in binary header */ if (!bh.hns) { warn("samples/trace not set in binary header \n"); if (nt == 0) warn("samples/trace in 1st trace used \n"); else warn("nt in input used for samples/trace \n"); } if ((nt != bh.hns) && (nt != 0)) { warn("samples/trace reset in binary header =%d \n",nt); bh.hns = nt; } /* output ascii and binary headers to stdout */ puthdr(&ch, &bh); nbt = 0; /* convert the traces */ while (efread((char *)&tr, 1, HDRBYTES, ifp) && (itr < trmax)) { /* check first 10 bytes to look for ebcdic header, if found, this probably indicates a tape switch */ /* tascii_((unsigned char*)&tr, &hdr_buf, 10, 0); if ((strncmp(hdr_buf, "C 1 CLIENT", 10) == 0) || (strncmp(hdr_buf, "C CLIENT ", 10) == 0) || (strncmp(hdr_buf, "C 1 ", 4) == 0)) { fprintf(stderr," % efread(tmp_buf, 1, 3600 - HDRBYTES, ifp); } else { */ /* read in the trace data */ if(tr.ns==0) tr.ns = nt; if(ntg==0) { nsamp = tr.ns * 4; } else { nsamp = nt * 4; } efread((char *)&tr + HDRBYTES, 1, nsamp, ifp); ibt = 0; /* Check bh.hns with tr.ns */ if (bh.hns != tr.ns) { nsflag = true; ibt = 1; nbt = nbt + 1; /* print warning message */ if(verbose==1 || nbt<1000) warn("discrepant tr.ns = %d with bh.hns = %d\n" "\t... noted on trace %d", tr.ns, bh.hns, itr + 1); /* if user wants to leave things the way they are (nt=0) */ /* otherwise, modify number of samples per trace */ if (nt != 0) { if (nt > tr.ns) { for (i = tr.ns; i < nt; i++) tr.data[i] = 0.0; } nsamp = nt * 4; tr.ns = nt; } } /* convert and write desired traces */ if (++itr >= trmin) { /* Convert IBM floats to native floats */ if (convert) conv_float((char *)tr.data, (char *)tr.data, tr.ns, 1); /* write the trace to disk */ if(nmap==0) { /* clean up trace header beyond 180 bytes */ if (clean == 1) bzero((char *)&tr + 180, 60); if (ibt==0 || rmbadtrace==0) efwrite((char *)&tr, 1, nsamp + HDRBYTES, stdout); } else { bcopy((char*)&tr,(char*)&tro,nsamp+HDRBYTES); for(imap=0;imap<nmap;imap++) { ibs = ibstart[imap]; iby = ibyte[imap]; ity = itype[imap]; obs = obstart[imap]; oby = obyte[imap]; oty = otype[imap]; /* fprintf(stderr,"ibs=%d iby=%d ity=%d obs=%d oby=%d oty=%d \n", ibs,iby,ity,obs,oby,oty); */ if(iby==oby && ity==oty && ity!=1 ) { bcopy((char*)&tr+ibs-1,(char*)&tro+obs-1,iby); } else { if(ity==1) { conv_float((char*)&tr+ibs-1,(char*)&tmp,1,1); } else { if(iby==2) { bcopy((char*)&tr+ibs-1,(char*)&itmp2,iby); tmp = itmp2; } else if(iby==4) { bcopy((char*)&tr+ibs-1,(char*)&itmp4,iby); tmp = itmp4; } } if(oty==1) { bcopy((char*)&tmp,(char*)&tro+obs-1,oby); } else { tmp = tmp + 0.5; if(oby==2) { itmp2 = (short) tmp; bcopy((char*)&itmp2,(char*)&tro+obs-1,oby); } else { itmp4 = (int) tmp; bcopy((char*)&itmp4,(char*)&tro+obs-1,oby); } } } } /* clean up trace header beyond 180 bytes */ if (clean == 1) bzero((char *)&tro + 180, 60); if (ibt==0 || rmbadtrace==0) efwrite((char *)&tro, 1, nsamp + HDRBYTES, stdout); } /* echo under verbose option */ if (verbose && itr % 20 == 0) warn(" %d traces from tape", itr); } /* } */ } /* while loop */ /* re-iterate error in case not seen during run */ if ((nsflag) && (nt != 0)) warn("discrepancy found in header and trace ns values\n" "\theader value (%d) was used to extract traces", bh.hns); /* clean up */ efclose(ifp); if(nmap>0) { free(ibstart); free(ibyte); free(itype); free(obstart); free(obyte); free(otype); } return EXIT_SUCCESS; }
char updateutmp_f (enum utmp_action options, struct utmp *new_entry) { int ufile; struct stat st; // strip the utmp_add action if we don't get a new entry to add along with it if ((options & utmp_add) && !new_entry) options ^= utmp_add; // if we don't have anything to do, bail out if (!options) return -1; if (coremode == einit_mode_sandbox) ufile = eopen ("var/run/utmp", O_RDWR); else ufile = eopen ("/var/run/utmp", O_RDWR); if (ufile) { if (!fstat (ufile, &st) && st.st_size) { struct utmp *utmpentries = mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, ufile, 0); if (utmpentries != MAP_FAILED) { uint32_t entries = st.st_size / sizeof(struct utmp), i = 0; eclose (ufile); ufile = 0; for (i = 0; i < entries; i++) { #ifdef LINUX switch (utmpentries[i].ut_type) { case DEAD_PROCESS: if (options & utmp_add) { memcpy (&(utmpentries[i]), new_entry, sizeof (struct utmp)); options ^= utmp_add; } break; case RUN_LVL: if (options & utmp_clean) { /* the higher 8 bits contain the old runlevel, the lower 8 bits the current one */ char *new_previous_runlevel = cfg_getstring ("configuration-compatibility-sysv-simulate-runlevel/before", NULL), *new_runlevel = cfg_getstring ("configuration-compatibility-sysv-simulate-runlevel/now", NULL); if (new_runlevel && new_runlevel[0]) { if (new_previous_runlevel) utmpentries[i].ut_pid = (new_previous_runlevel[0] << 8) | new_runlevel[0]; else utmpentries[i].ut_pid = (utmpentries[i].ut_pid << 8) | new_runlevel[0]; } } break; case UT_UNKNOWN: case BOOT_TIME: case NEW_TIME: case OLD_TIME: case INIT_PROCESS: case LOGIN_PROCESS: case USER_PROCESS: case ACCOUNTING: if (options & utmp_clean) { #ifdef LINUX struct stat xst; char path[BUFFERSIZE]; esprintf (path, BUFFERSIZE, "/proc/%i/", utmpentries[i].ut_pid); if (stat (path, &xst)) { // stat path under proc to see if process exists // if not... #endif // clean utmp record if (options & utmp_add) { memcpy (&(utmpentries[i]), new_entry, sizeof (struct utmp)); options ^= utmp_add; } else { utmpentries[i].ut_type = DEAD_PROCESS; memset (&(utmpentries[i].ut_user), 0, sizeof (utmpentries[i].ut_user)); memset (&(utmpentries[i].ut_host), 0, sizeof (utmpentries[i].ut_host)); memset (&(utmpentries[i].ut_time), 0, sizeof (utmpentries[i].ut_time)); } #ifdef LINUX } #endif } break; #ifdef DEBUG default: notice (6, "bad UTMP entry: [%c%c%c%c] %i (%s), %s@%s: %i.%i\n", utmpentries[i].ut_id[0], utmpentries[i].ut_id[1], utmpentries[i].ut_id[2], utmpentries[i].ut_id[3], utmpentries[i].ut_type, utmpentries[i].ut_line, utmpentries[i].ut_user, utmpentries[i].ut_host, (int)utmpentries[i].ut_tv.tv_sec, (int)utmpentries[i].ut_tv.tv_usec); break; #endif } if ((options & utmp_modify) && (utmpentries[i].ut_pid == new_entry->ut_pid)) { memcpy (&(utmpentries[i]), new_entry, sizeof (struct utmp)); options ^= utmp_modify; } #endif if (!options) break; } munmap (utmpentries, st.st_size); } else { bitch(bitch_stdio, 0, "mmap() failed"); } } if (ufile) eclose (ufile); } else { bitch(bitch_stdio, 0, "open() failed"); } if (options & utmp_add) { // still didn't get to add this.. try to append it to the file if (coremode == einit_mode_sandbox) ufile = open ("var/run/utmp", O_WRONLY | O_APPEND); else ufile = open ("/var/run/utmp", O_WRONLY | O_APPEND); if (ufile) { if (write(ufile, new_entry, sizeof (struct utmp)) != sizeof (struct utmp)) { bitch(bitch_stdio, 0, "short write to utmp file"); } eclose (ufile); } else { bitch(bitch_stdio, 0, "mmap() failed"); } options ^= utmp_add; } return 0; }