static int verifyarg(char *dev, int flag) { struct stat st; int fd; int ret = 0; if (dev == NULL) { warn(gettext("specify device(s)\n")); ret = 1; goto err_exit; } if (dev[0] != '/') { warn(gettext("device name must begin with a '/'\n")); ret = 1; goto err_exit; } if ((pathcmp(dev, SYSMSG) == 0) || (pathcmp(dev, WSCONS) == 0) || (pathcmp(dev, CONSOLE) == 0)) { /* they match */ warn(gettext("invalid device %s\n"), dev); ret = 1; goto err_exit; } if (stat(dev, &st) || ! S_ISCHR(st.st_mode)) { warn(gettext("invalid device %s\n"), dev); ret = 1; goto err_exit; } /* Delete operation doesn't require this checking */ if ((fd = safeopen(dev)) < 0) { if (flag) { warn(gettext("invalid device %s\n"), dev); ret = 1; } goto err_exit; } if (!modem_support(fd)) { warn(gettext("invalid device %s\n"), dev); (void) close(fd); ret = 1; goto err_exit; } /* Only verify carrier if it's an add operation */ if (flag) { if (!has_carrier(fd)) { warn(gettext("failure, no carrier on %s\n"), dev); ret = 1; goto err_exit; } } err_exit: return (ret); }
/* * Display configured consoles. */ static void getconsole(void) { int fd; int bufsize = 0; /* size of device cache */ char *infop, *ptr, *p; /* info structure for ioctl's */ if ((fd = safeopen(SYSMSG)) < 0) die(gettext("%s is missing or not a valid device\n"), SYSMSG); if ((bufsize = ioctl(fd, CIOCGETCONSOLE, NULL)) < 0) die(gettext("cannot get table entry\n")); if (bufsize == 0) return; if ((infop = calloc(bufsize, sizeof (char))) == NULL) die(gettext("cannot allocate buffer")); if (ioctl(fd, CIOCGETCONSOLE, infop) < 0) die(gettext("cannot get table entry\n")); ptr = infop; while (ptr != NULL) { p = strchr(ptr, ' '); if (p == NULL) { (void) printf("%s\n", ptr); break; } *p++ = '\0'; (void) printf("%s\n", ptr); ptr = p; } (void) close(fd); }
void write_fg_csv(const char * outfilename,FGrid_str *fg){ int i,j; CA_FLOAT * tp; FILE * ofile; int nr = fg->nr,nz=fg->nz; ofile = safeopen(outfilename,"w"); fprintf(ofile,"nz,nr,nnodes\n%i,%i,%i\n",fg->nz,fg->nr,fg->nnodes); fprintf(ofile,"r values,"); for (i=0;i<nr-1;i++){ fprintf(ofile,"%.10g,",fg->r[i]); } fprintf(ofile,"%.10g\n",fg->r[i]); fprintf(ofile,"z values,"); for (i=0;i<nz-1;i++){ fprintf(ofile,"%.10g,",fg->z[i]); } fprintf(ofile,"%.10g\n",fg->z[i]); fprintf(ofile,"T values\n"); tp = fg->Fidap_T; for (i=0;i<nr;i++){ fprintf(ofile,"%.10g,",fg->r[i]); for (j=0;j<nz;j++){ fprintf(ofile,"%.10g,",*tp++); } fprintf(ofile,"\n"); } fclose(ofile); }
/* 该函数用于从把文件fn的数据全部读取到dat中 2011-02-07 16:02 */ TYPE_STATUS fileio::saveall(TYPE_FILENAME fn, TYPE_BYTE* dat, TYPE_SIZE sz) { FILE *fp = NULL; fp = safeopen(fn.c_str(), "wb"); if(fp){ fwrite(dat, sizeof(TYPE_BYTE), sz, fp); fclose(fp); return 1; } return 0; }
/* 该函数用于读取文本文件到串组数组 2011-02-13 12:38 */ TYPE_STATUS fileio::loadtxtarray(TYPE_FILENAME fn, TYPE_STRING_ARRAY& sa) { char s[0x10000]; FILE *fp; fp = safeopen(fn.c_str(), "r+"); if(fp){ while(fgets(s, 0x10000, fp)){ sa.push_back(s); } return 1; } return 0; }
/* 该函数用于从把文件fn的数据全部读取到dat中 2011-02-07 15:57 */ TYPE_STATUS fileio::loadall(TYPE_FILENAME fn, TYPE_BYTE* dat) { FILE *fp; fp = safeopen(fn.c_str(), "rb"); if(fp){ while(!feof(fp)){ *dat = (unsigned char)fgetc(fp); ++dat; } *dat = '\0'; fclose(fp); } return 1; }
/* * Send ioctl to device specified and * Remove the entry from the list of auxiliary devices. */ static void unsetaux(char *dev) { int fd; if ((fd = safeopen(SYSMSG)) < 0) die(gettext("%s is missing or not a valid device\n"), SYSMSG); if (ioctl(fd, CIOCRMCONSOLE, dev) != 0) { if (errno == EBUSY) die(gettext("cannot unset the default console\n")); } else syslog(LOG_WARNING, "%s: Removed auxiliary device %s", CONSADM, dev); (void) close(fd); }
void write_fg_bin(const char * outfilename,FGrid_str *fg){ const char frev[] = FG_REV; int fsize,fgsize,revsize; long data_pos=0,info_data_pos; /* position of the data, pointer to the location for that information */ FILE * ofile; fsize = sizeof(CA_FLOAT); fgsize = sizeof(FGrid_str); revsize = strlen(frev); ofile = safeopen(outfilename,"w"); /* save the revision ID string */ fwrite(frev,sizeof(char),revsize,ofile); fwrite("\n",sizeof(char),1,ofile); /* save the compiled-in size of a CA_FLOAT variable */ fwrite(&fsize,sizeof(int),1,ofile); /* get the current location, to put the data_pos into*/ /* and make space for it */ info_data_pos = ftell(ofile); fwrite(&data_pos,sizeof(long),1,ofile); /* save the current size of a FGrid_str structure */ fwrite(&fgsize,sizeof(int),1,ofile); /* save the structure data */ fwrite(fg,fgsize,1,ofile); /* save the data arrays */ /* get the position of the begginning of the arrays */ data_pos=ftell(ofile); fwrite(fg->r,sizeof(CA_FLOAT),fg->nr,ofile); fwrite(fg->z,sizeof(CA_FLOAT),fg->nz,ofile); fwrite(fg->Fidap_T,sizeof(CA_FLOAT),fg->nnodes,ofile); /* store the beginning position in the file */ fseek(ofile,info_data_pos,SEEK_SET); fwrite(&data_pos,sizeof(long),1,ofile); fclose(ofile); }
/* 该函数用于从把文件fn的数据全部读取到bs中 2011-02-09 19:01 */ TYPE_STATUS fileio::loadall(TYPE_FILENAME fn, TYPE_BYTE_STRING& bs) { FILE *fp; vector<TYPE_BYTE> buf; TYPE_BYTE dt; fp = safeopen(fn.c_str(), "rb"); if(fp){ while(!feof(fp)){ dt = (unsigned char)fgetc(fp); buf.push_back(dt); } fclose(fp); bs.buf = new TYPE_BYTE[buf.size()+1]; for(TYPE_SIZE i = 0; i < buf.size(); ++i){ bs.buf[i] = buf[i]; } bs.size = buf.size(); if(bs.size)--bs.size; bs.capacity = bs.size+1; return 1; } return 0; }
/* * send ioctl to /dev/sysmsg to route msgs of the device specified. */ static void setaux(char *dev) { int fd; if ((fd = safeopen(SYSMSG)) < 0) die(gettext("%s is missing or not a valid device\n"), SYSMSG); if (ioctl(fd, CIOCSETCONSOLE, dev) != 0) { /* * Let setting duplicate device be warning, consadm * must proceed to set persistence if requested. */ if (errno == EBUSY) die(gettext("%s is already the default console\n"), dev); else if (errno != EEXIST) die(gettext("cannot get table entry")); } syslog(LOG_WARNING, "%s: Added auxiliary device %s", CONSADM, dev); (void) close(fd); }
/* * Initialize scheduler log with * prog = program name to tag error messages with * opt = log_options_t specifying max log levels for syslog, stderr, and file * fac = log facility for syslog (unused if syslog level == LOG_QUIET) * logfile = logfile name if logfile level > LOG_QUIET */ static int _sched_log_init(char *prog, log_options_t opt, log_facility_t fac, char *logfile) { int rc = 0; if (!sched_log) { sched_log = (log_t *)xmalloc(sizeof(log_t)); atfork_install_handlers(); } if (prog) { xfree(sched_log->argv0); sched_log->argv0 = xstrdup(xbasename(prog)); } else if (!sched_log->argv0) { const char *short_name; short_name = strrchr((const char *) default_name, '/'); if (short_name) short_name++; else short_name = default_name; sched_log->argv0 = xstrdup(short_name); } if (!sched_log->fpfx) sched_log->fpfx = xstrdup(""); sched_log->opt = opt; if (sched_log->buf) { cbuf_destroy(sched_log->buf); sched_log->buf = NULL; } if (sched_log->fbuf) { cbuf_destroy(sched_log->fbuf); sched_log->fbuf = NULL; } if (sched_log->opt.buffered) { sched_log->buf = cbuf_create(128, 8192); sched_log->fbuf = cbuf_create(128, 8192); } if (sched_log->opt.syslog_level > LOG_LEVEL_QUIET) sched_log->facility = fac; if (logfile) { FILE *fp; fp = safeopen(logfile, "a", SAFEOPEN_LINK_OK); if (!fp) { rc = errno; goto out; } if (sched_log->logfp) fclose(sched_log->logfp); /* Ignore errors */ sched_log->logfp = fp; } if (sched_log->logfp) { int fd; if ((fd = fileno(sched_log->logfp)) < 0) sched_log->logfp = NULL; else fd_set_close_on_exec(fd); } sched_log->initialized = 1; out: return rc; }
/* * Initialize log with * prog = program name to tag error messages with * opt = log_options_t specifying max log levels for syslog, stderr, and file * fac = log facility for syslog (unused if syslog level == LOG_QUIET) * logfile = * logfile name if logfile level > LOG_QUIET */ static int _log_init(char *prog, log_options_t opt, log_facility_t fac, char *logfile ) { int rc = 0; if (!log) { log = (log_t *)xmalloc(sizeof(log_t)); log->logfp = NULL; log->argv0 = NULL; log->buf = NULL; log->fbuf = NULL; log->fpfx = NULL; atfork_install_handlers(); } if (prog) { if (log->argv0) xfree(log->argv0); log->argv0 = xstrdup(xbasename(prog)); } else if (!log->argv0) { const char *short_name = strrchr(default_name, '/'); if (short_name) short_name++; else short_name = default_name; log->argv0 = xstrdup(short_name); } if (!log->fpfx) log->fpfx = xstrdup(""); log->opt = opt; if (log->buf) { cbuf_destroy(log->buf); log->buf = NULL; } if (log->fbuf) { cbuf_destroy(log->fbuf); log->fbuf = NULL; } if (log->opt.buffered) { log->buf = cbuf_create(128, 8192); log->fbuf = cbuf_create(128, 8192); } if (log->opt.syslog_level > LOG_LEVEL_QUIET) log->facility = fac; if (logfile && (log->opt.logfile_level > LOG_LEVEL_QUIET)) { FILE *fp; fp = safeopen(logfile, "a", SAFEOPEN_LINK_OK); if (!fp) { char *errmsg = NULL; xslurm_strerrorcat(errmsg); fprintf(stderr, "%s: log_init(): Unable to open logfile" "`%s': %s\n", prog, logfile, errmsg); xfree(errmsg); rc = errno; goto out; } if (log->logfp) fclose(log->logfp); /* Ignore errors */ log->logfp = fp; } if (log->logfp) { int fd; if ((fd = fileno(log->logfp)) < 0) log->logfp = NULL; else fd_set_close_on_exec(fd); } log->initialized = 1; out: return rc; }
/* * Assume we always fall back to /dev/console. * parameter passed in will always be the auxiliary device. * The daemon will not start after the last device has been removed. */ static void fallbackdaemon(void) { int fd, sysmfd, ret = 0; char **devpaths; pollfd_t *fds; nfds_t nfds = 0; int index; int pollagain; struct sigaction sa; int bufsize = 0; /* length of device cache paths */ int cachesize = 0; /* size of device cache */ char *infop, *ptr, *p; /* info structure for ioctl's */ /* * catch SIGTERM cause it might be coming from user via consadm */ sa.sa_handler = catch_term; sa.sa_flags = 0; (void) sigemptyset(&sa.sa_mask); (void) sigaction(SIGTERM, &sa, NULL); /* * catch SIGHUP cause it might be coming from a disconnect */ sa.sa_handler = catch_hup; sa.sa_flags = 0; (void) sigemptyset(&sa.sa_mask); (void) sigaction(SIGHUP, &sa, NULL); if ((sysmfd = safeopen(SYSMSG)) < 0) die(gettext("%s is missing or not a valid device\n"), SYSMSG); if ((bufsize = ioctl(sysmfd, CIOCGETCONSOLE, NULL)) < 0) die(gettext("cannot get table entry\n")); if (bufsize == 0) return; if ((infop = calloc(bufsize, sizeof (char))) == NULL) die(gettext("cannot allocate buffer")); if (ioctl(sysmfd, CIOCGETCONSOLE, infop) < 0) die(gettext("cannot get table entry\n")); ptr = infop; while (ptr != NULL) { p = strchr(ptr, ' '); if (p == NULL) { cachesize++; break; } p++; cachesize++; ptr = p; } if ((fds = calloc(cachesize, sizeof (struct pollfd))) == NULL) die(gettext("cannot allocate buffer")); if ((devpaths = calloc(cachesize, sizeof (char *))) == NULL) die(gettext("cannot allocate buffer")); ptr = infop; while (ptr != NULL) { p = strchr(ptr, ' '); if (p == NULL) { if ((fd = safeopen(ptr)) < 0) { warn(gettext("cannot open %s, continuing"), ptr); break; } if (!has_carrier(fd)) { (void) close(fd); warn(gettext( "no carrier on %s, device will not be monitored.\n"), ptr); break; } else { fds[nfds].fd = fd; fds[nfds].events = 0; if ((devpaths[nfds] = malloc(strlen(ptr) + 1)) == NULL) die(gettext("cannot allocate buffer")); (void) strcpy(devpaths[nfds], ptr); nfds++; if (nfds >= cachesize) break; } break; } *p++ = '\0'; if ((fd = safeopen(ptr)) < 0) { warn(gettext("cannot open %s, continuing"), ptr); ptr = p; continue; } if (!has_carrier(fd)) { (void) close(fd); warn(gettext( "no carrier on %s, device will not be monitored.\n"), ptr); ptr = p; continue; } else { fds[nfds].fd = fd; fds[nfds].events = 0; if ((devpaths[nfds] = malloc(strlen(ptr) + 1)) == NULL) die(gettext("cannot allocate buffer")); (void) strcpy(devpaths[nfds], ptr); nfds++; if (nfds >= cachesize) break; } ptr = p; } (void) close(sysmfd); /* no point polling if no devices with carrier */ if (nfds == 0) return; for (;;) { /* daemon sleeps waiting for a hangup on the console */ ret = poll(fds, nfds, INFTIM); if (ret == -1) { /* Check if ttymon is trying to get rid of us */ if (errno == EINTR) continue; warn(gettext("cannot poll device")); return; } else if (ret == 0) { warn(gettext("timeout (%d milleseconds) occured\n"), INFTIM); return; } else { /* Go through poll list looking for events. */ for (index = 0; index < nfds; index++) { /* expected result */ if ((fds[index].revents & POLLHUP) == POLLHUP) { /* * unsetaux console. Take out of list * of current auxiliary consoles. */ unsetaux((char *)devpaths[index]); warn(gettext( "lost carrier, unsetting console %s\n"), devpaths[index]); syslog(LOG_WARNING, "%s: lost carrier, unsetting auxiliary device %s", CONSADM, devpaths[index]); free(devpaths[index]); devpaths[index] = NULL; (void) close(fds[index].fd); fds[index].fd = -1; fds[index].revents = 0; continue; } if ((fds[index].revents & POLLERR) == POLLERR) { warn(gettext("poll error\n")); continue; } else if (fds[index].revents != 0) { warn(gettext( "unexpected poll result 0x%x\n"), fds[index].revents); continue; } } /* check whether any left to poll */ pollagain = B_FALSE; for (index = 0; index < nfds; index++) if (fds[index].fd != -1) pollagain = B_TRUE; if (pollagain == B_TRUE) continue; else return; } } }
void read_fg_bin(const char * infilename,FGrid_str *fg,int fg_flag){ int fsize,fgsize; int nread; FILE * ifile; const char frev[] = FG_REV; char line[LINELENGTH]; long data_pos=0,data_compare=0; fprintf(stderr,"read_fg_bin: Reading from %s\n",infilename); /* open the file */ ifile = safeopen(infilename,"r"); if (fgets(line,LINELENGTH,ifile) == NULL){ fprintf(stderr,"read_fg_bin:ERROR: Cannot read a line\n"); #ifdef ERROR_EXIT exit(1); #endif } line[strlen(line)-1]=(char)0; /*test the revision numbers*/ /* test the cvs revision number of fidap.h */ fprintf(stderr,"Revision of fidap.h ... \nThis program %s\nBin file %s\n",frev,line); if (strcasecmp(line,frev) != 0){ fprintf(stderr,"**************************************************\n"); fprintf(stderr,"* read_fg_bin: WARNING: structure revision numbers do not match.\n"); fprintf(stderr,"* In the file: %s, In this program: %s\n",line,frev); fprintf(stderr,"* Attempting to continue...\n"); fprintf(stderr,"* If there are more errors below, the structure revisions\n"); fprintf(stderr,"* are probably not compatible.\n"); fprintf(stderr,"* In this case you should checkout the correct\n"); fprintf(stderr,"* older version of the ca program and use that,\n"); fprintf(stderr,"* or use (or create) a filter version to convert the data.\n"); fprintf(stderr,"**************************************************\n"); } /* test the size of CA_FLOAT - compiled precision */ fread (&fsize,sizeof(int),1,ifile); fprintf(stderr,"CA_FLOAT size: file: %i program: %i\n",fsize,sizeof(CA_FLOAT)); if(fsize != sizeof(CA_FLOAT)){ fprintf(stderr,"ERROR:CA_FLOAT precision does not match."); fprintf(stderr,"ERROR: Cannot continue in this case."); exit(0); } /* locate the numerical data section */ fread(&data_pos,sizeof(long),1,ifile); fprintf(stderr,"Location of data: %i\n",data_pos); switch (fg_flag){ case FG_FIRST_READ: case FG_TRANS_READ: /* test the size of fg struct */ fread (&fgsize,sizeof(int),1,ifile); fprintf(stderr,"fg struct size: %i %i\n",fgsize,sizeof(FGrid_str)); if(fgsize != sizeof(FGrid_str)){ fprintf(stderr,"ERROR: FGrid_str size does not match."); fprintf(stderr,"ERROR: Cannot continue in this case."); exit(0); } /* if we made it this far, read in the FGrid_str data */ nread=fread(fg,fgsize,1,ifile); fprintf(stderr,"Read %i bytes for fg struct\n",nread*fgsize); fprintf(stderr,"nr = %i,nz = %i,nnodes = %i\n",fg->nr,fg->nz,fg->nnodes); /* allocate the arrays */ alloc_fg(fg); /* this routine exits on malloc failure */ break; case 500:/* this method may not work very well....*/ /* set the file pointer to the location of the data */ fseek(ifile, data_pos,SEEK_SET); break; default: fprintf(stderr,"ERROR:read_fg_bin: Flag not understood %i\n",fg_flag); exit(0); break; } /* read in the data values */ read_fg_data(fg,ifile); fclose (ifile); }/* end of read_fg_bin */