FILE * myfopen(char *path, char *mode) { /* opens a file pointer and then sets close-on-exec for the file */ FILE *fp; fp = fopen(path, mode); #ifdef SETMODE if (fp && ! strchr(mode, 'b')) { SETMODE(fileno(fp), O_TEXT); } #endif /* SETMODE */ #ifndef CLOSE_ON_EXEC /* HBB 20010312: DOS GCC doesn't have FD_CLOEXEC (yet), so it * always fails this call. Have to skip that step */ if(fp) #else if(fp && (fcntl(fileno(fp), F_SETFD, CLOSE_ON_EXEC) != -1)) #endif return(fp); else return(NULL); }
int main(int argc, char **argv) { char c; if (SETMODE(fileno(stdin), O_BINARY) == -1) { perror("ucase: setmode"); exit(1); } if (SETMODE(fileno(stdout), O_BINARY) == -1) { perror("ucase: setmode"); exit(1); } while ((c = getchar()) != EOF) { if (islower(c)) c = toupper(c); if (fwrite(&c, sizeof(char), 1L, stdout) != 1) { perror("ucase: fwrite"); exit(1); } } exit(0); }
void MicroSequencer::decode() { INT32 immed4 = readBits(4); INT32 nextInstruction = readBits(4, TRUE); switch (nextInstruction) { case 0x0: if (immed4 == 0) RTS(); else SETPAGE(immed4); break; case 0x8: SETMODE(immed4); break; case 0x4: LOAD_4(immed4); break; case 0xC: LOAD_C(immed4); break; case 0x2: LOAD_2(immed4); break; case 0xA: SETMSB_A(immed4); break; case 0x6: SETMSB_6(immed4); break; case 0xE: LOAD_E(immed4); break; case 0x1: LOADALL(immed4); break; case 0x9: DELTA_9(immed4); break; case 0x5: SETMSB_5(immed4); break; case 0xD: DELTA_D(immed4); break; case 0x3: SETMSB_3(immed4); break; case 0xB: JSR(immed4); break; case 0x7: JMP(immed4); break; case 0xF: PAUSE(immed4); break; /* case 0x0: if (immed4 == 0) RTS(); else SETPAGE(immed4); break; case 0x1: SETMODE(immed4); break; case 0x2: LOAD_4(immed4); break; case 0x3: LOAD_C(immed4); break; case 0x4: LOAD_2(immed4); break; case 0x5: SETMSB_A(immed4); break; case 0x6: SETMSB_6(immed4); break; case 0x7: LOAD_E(immed4); break; case 0x8: LOADALL(immed4); break; case 0x9: DELTA_9(immed4); break; case 0xA: SETMSB_5(immed4); break; case 0xB: DELTA_D(immed4); break; case 0xC: SETMSB_3(immed4); break; case 0xD: JSR(immed4); break; case 0xE: JMP(immed4); break; case 0xF: PAUSE(immed4); break; */ } }
/*-------------------------------------------------------------------*/ static int open_printer (DEVBLK *dev) { pid_t pid; /* Child process identifier */ int open_flags; /* File open flags */ #if !defined( _MSVC_ ) int pipefd[2]; /* Pipe descriptors */ int rc; /* Return code */ #endif /* Regular open if 1st char of filename is not vertical bar */ if (!dev->ispiped) { int fd; /* Socket printer? */ if (dev->bs) return (dev->fd < 0 ? -1 : 0); /* Normal printer */ open_flags = O_BINARY | O_WRONLY | O_CREAT /* | O_SYNC */; if (dev->notrunc != 1) { open_flags |= O_TRUNC; } fd = HOPEN (dev->filename, open_flags, S_IRUSR | S_IWUSR | S_IRGRP); if (fd < 0) { WRMSG (HHC01105, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "open()", strerror(errno)); return -1; } /* Save file descriptor in device block and return */ dev->fd = fd; return 0; } /* Filename is in format |xxx, set up pipe to program xxx */ #if defined( _MSVC_ ) /* "Poor man's" fork... */ pid = w32_poor_mans_fork ( dev->filename+1, &dev->fd ); if (pid < 0) { WRMSG (HHC01105, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "fork()", strerror(errno)); return -1; } /* Log start of child process */ WRMSG (HHC01106, "I", SSID_TO_LCSS(dev->ssid), dev->devnum, pid); dev->ptpcpid = pid; #else /* !defined( _MSVC_ ) */ /* Create a pipe */ rc = create_pipe (pipefd); if (rc < 0) { WRMSG (HHC01105, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "create_pipe()", strerror(errno)); return -1; } /* Fork a child process to receive the pipe data */ pid = fork(); if (pid < 0) { WRMSG (HHC01005, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "fork()", strerror(errno)); close_pipe ( pipefd[0] ); close_pipe ( pipefd[1] ); return -1; } /* The child process executes the pipe receiver program... */ if (pid == 0) { /* Log start of child process */ WRMSG (HHC01106, "I", SSID_TO_LCSS(dev->ssid), dev->devnum, getpid()); /* Close the write end of the pipe */ close_pipe ( pipefd[1] ); /* Duplicate the read end of the pipe onto STDIN */ if (pipefd[0] != STDIN_FILENO) { rc = dup2 (pipefd[0], STDIN_FILENO); if (rc != STDIN_FILENO) { WRMSG (HHC01105, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "dup2()", strerror(errno)); close_pipe ( pipefd[0] ); _exit(127); } } /* end if(pipefd[0] != STDIN_FILENO) */ /* Close the original descriptor now duplicated to STDIN */ close_pipe ( pipefd[0] ); /* Redirect stderr (screen) to hercules log task */ dup2(STDOUT_FILENO, STDERR_FILENO); /* Relinquish any ROOT authority before calling shell */ SETMODE(TERM); /* Execute the specified pipe receiver program */ rc = system (dev->filename+1); if (rc == 0) { /* Log end of child process */ WRMSG (HHC01107, "I", SSID_TO_LCSS(dev->ssid), dev->devnum, getpid()); } else { /* Log error */ WRMSG (HHC01108, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, dev->filename+1, strerror(errno)); } /* The child process terminates using _exit instead of exit to avoid invoking the panel atexit cleanup routine */ _exit(rc); } /* end if(pid==0) */ /* The parent process continues as the pipe sender */ /* Close the read end of the pipe */ close_pipe ( pipefd[0] ); /* Save pipe write descriptor in the device block */ dev->fd = pipefd[1]; dev->ptpcpid = pid; #endif /* defined( _MSVC_ ) */ return 0; } /* end function open_printer */
void *http_server (void *arg) { int rc; /* Return code */ int lsock; /* Socket for listening */ int csock; /* Socket for conversation */ struct sockaddr_in server; /* Server address structure */ fd_set selset; /* Read bit map for select */ int optval; /* Argument for setsockopt */ TID httptid; /* Negotiation thread id */ struct timeval timeout; /* timeout value */ UNREFERENCED(arg); http_serv.httpshutdown = TRUE; hdl_adsc("http_shutdown",http_shutdown, NULL); /* Set root mode in order to set priority */ SETMODE(ROOT); /* Set server thread priority; ignore any errors */ if(setpriority(PRIO_PROCESS, 0, sysblk.srvprio)) WRMSG(HHC00136, "W", "setpriority()", strerror(errno)); /* Back to user mode */ SETMODE(USER); /* Display thread started message on control panel */ WRMSG (HHC00100, "I", (u_long)thread_id(), getpriority(PRIO_PROCESS,0), "HTTP server"); /* make sure root path is built */ if ( http_root() == NULL ) goto http_server_stop; /* Obtain a socket */ lsock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (lsock < 0) { WRMSG(HHC01800,"E", "socket()", strerror(HSO_errno)); goto http_server_stop; } /* Allow previous instance of socket to be reused */ optval = 1; setsockopt (lsock, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(optval)); /* Prepare the sockaddr structure for the bind */ memset (&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = http_serv.httpport; server.sin_port = htons(server.sin_port); http_serv.httpbinddone = FALSE; /* Attempt to bind the socket to the port */ while (TRUE) { rc = bind (lsock, (struct sockaddr *)&server, sizeof(server)); if (rc == 0 || HSO_errno != HSO_EADDRINUSE) break; WRMSG(HHC01804, "W", http_serv.httpport); SLEEP(10); } /* end while */ if (rc != 0) { WRMSG(HHC01800,"E", "bind()", strerror(HSO_errno)); goto http_server_stop; } else http_serv.httpbinddone = TRUE; /* Put the socket into listening state */ rc = listen (lsock, 32); if (rc < 0) { WRMSG(HHC01800,"E", "listen()", strerror(HSO_errno)); http_serv.httpbinddone = FALSE; goto http_server_stop; } http_serv.httpshutdown = FALSE; WRMSG(HHC01803, "I", http_serv.httpport); /* Handle http requests */ while ( !http_serv.httpshutdown ) { /* Initialize the select parameters */ FD_ZERO (&selset); FD_SET (lsock, &selset); timeout.tv_sec = 0; timeout.tv_usec = 10000; /* until a better way to implement this use standard windows */ #undef select /* Wait for a file descriptor to become ready use NON-BLOCKING select()*/ rc = select ( lsock+1, &selset, NULL, NULL, &timeout ); if ( rc == 0 || http_serv.httpshutdown ) continue; if (rc < 0 ) { if (HSO_errno == HSO_EINTR) continue; WRMSG(HHC01800, "E", "select()", strerror(HSO_errno)); break; } /* If a http request has arrived then accept it */ if (FD_ISSET(lsock, &selset)) { /* Accept the connection and create conversation socket */ csock = accept (lsock, NULL, NULL); if (csock < 0) { WRMSG(HHC01800, "E", "accept()", strerror(HSO_errno)); continue; } /* Create a thread to execute the http request */ rc = create_thread (&httptid, DETACHED, http_request, (void *)(uintptr_t)csock, "http_request"); if(rc) { WRMSG(HHC00102, "E", strerror(rc)); close_socket (csock); } } /* end if(lsock) */ } /* end while */ /* Close the listening socket */ close_socket (lsock); http_server_stop: if ( !sysblk.shutdown ) hdl_rmsc(http_shutdown, NULL); /* Display thread started message on control panel */ WRMSG(HHC00101, "I", (u_long)thread_id(), getpriority(PRIO_PROCESS,0), "HTTP server"); sysblk.httptid = 0; http_serv.httpbinddone = FALSE; signal_condition(&http_serv.http_wait_shutdown); return NULL; } /* end function http_server */
static void* logger_thread(void *arg) { int bytes_read; UNREFERENCED(arg); /* Set root mode in order to set priority */ SETMODE(ROOT); /* Set device thread priority; ignore any errors */ if(set_thread_priority(0, sysblk.devprio)) WRMSG(HHC00136, "W", "set_thread_priority()", strerror(errno)); /* Back to user mode */ SETMODE(USER); #if !defined( _MSVC_ ) /* Redirect stdout to the logger */ if(dup2(logger_syslogfd[LOG_WRITE],STDOUT_FILENO) == -1) { if(logger_hrdcpy) fprintf(logger_hrdcpy, MSG(HHC02102, "E", "dup2()", strerror(errno))); exit(1); } #endif /* !defined( _MSVC_ ) */ setvbuf (stdout, NULL, _IONBF, 0); obtain_lock(&logger_lock); logger_active = 1; /* Signal initialization complete */ signal_condition(&logger_cond); release_lock(&logger_lock); /* ZZ FIXME: We must empty the read pipe before we terminate */ /* (Couldn't we just loop waiting for a 'select(,&readset,,,timeout)' to return zero?? Or use the 'poll' function similarly?? - Fish) */ while(logger_active) { bytes_read = read_pipe(logger_syslogfd[LOG_READ],logger_buffer + logger_currmsg, ((logger_bufsize - logger_currmsg) > LOG_DEFSIZE ? LOG_DEFSIZE : logger_bufsize - logger_currmsg)); if(bytes_read == -1) { int read_pipe_errno = HSO_errno; // (ignore any/all errors at shutdown) if (sysblk.shutdown) continue; if (HSO_EINTR == read_pipe_errno) continue; obtain_lock(&logger_lock); if(logger_hrdcpy) { fprintf(logger_hrdcpy, MSG(HHC02102, "E", "read_pipe()", strerror(read_pipe_errno))); } release_lock(&logger_lock); bytes_read = 0; } /* If Hercules is not running in daemon mode and panel initialization is not yet complete, write message to stderr so the user can see it on the terminal */ if (!sysblk.daemon_mode) { if (!sysblk.panel_init) { char* pLeft2 = logger_buffer + logger_currmsg; int nLeft2 = bytes_read; #if defined( OPTION_MSGCLR ) /* Remove "<pnl,..." color string if it exists */ if (1 && nLeft2 > 5 && strncasecmp( pLeft2, "<pnl", 4 ) == 0 && (pLeft2 = memchr( pLeft2+4, '>', nLeft2-4 )) != NULL ) { pLeft2++; nLeft2 -= (int)(pLeft2 - (logger_buffer + logger_currmsg)); } #endif // defined( OPTION_MSGCLR ) /* (ignore any errors; we did the best we could) */ if (nLeft2) { if ( fwrite( pLeft2, nLeft2, 1, stderr ) ) { perror(QLINE "fwrite failure/HHC02102 "); } } } } obtain_lock(&logger_lock); /* Write log data to hardcopy file */ if (logger_hrdcpy) { /* Need to prefix each line with a timestamp. */ static int needstamp = 1; char* pLeft = logger_buffer + logger_currmsg; int nLeft = bytes_read; char* pRight = NULL; int nRight = 0; char* pNL = NULL; /* (pointer to NEWLINE character) */ if (needstamp) { if (!sysblk.logoptnotime) logger_logfile_timestamp(); needstamp = 0; } while ( (pNL = memchr( pLeft, '\n', nLeft )) != NULL ) { pRight = pNL + 1; nRight = nLeft - (int)(pRight - pLeft); nLeft -= nRight; #if defined( OPTION_MSGCLR ) /* Remove "<pnl...>" color string if it exists */ { char* pLeft2 = pLeft; int nLeft2 = nLeft; if (1 && nLeft > 5 && strncasecmp( pLeft, "<pnl", 4 ) == 0 && (pLeft2 = memchr( pLeft+4, '>', nLeft-4 )) != NULL ) { pLeft2++; nLeft2 -= (int)(pLeft2 - pLeft); } else { pLeft2 = pLeft; nLeft2 = nLeft; } if (nLeft2) logger_logfile_write( pLeft2, nLeft2 ); } #else // !defined( OPTION_MSGCLR ) if (nLeft) logger_logfile_write( pLeft, nLeft ); #endif // defined( OPTION_MSGCLR ) pLeft = pRight; nLeft = nRight; if (!nLeft) { needstamp = 1; break; } if (!sysblk.logoptnotime) logger_logfile_timestamp(); } if (nLeft) logger_logfile_write( pLeft, nLeft ); } release_lock(&logger_lock); /* Increment buffer index to next available position */ logger_currmsg += bytes_read; if(logger_currmsg >= logger_bufsize) { logger_currmsg = 0; logger_wrapped = 1; } /* Notify all interested parties new log data is available */ obtain_lock(&logger_lock); broadcast_condition(&logger_cond); release_lock(&logger_lock); } logger_tid = 0; /* Logger is now terminating */ obtain_lock(&logger_lock); /* Write final message to hardcopy file */ if (logger_hrdcpy) { char* term_msg = MSG(HHC02103, "I"); size_t term_msg_len = strlen(term_msg); if (!sysblk.logoptnotime) logger_logfile_timestamp(); logger_logfile_write( term_msg, term_msg_len ); } /* Redirect all msgs to stderr */ logger_syslog[LOG_WRITE] = stderr; logger_syslogfd[LOG_WRITE] = STDERR_FILENO; fflush(stderr); /* Signal any waiting tasks */ broadcast_condition(&logger_cond); release_lock(&logger_lock); return NULL; }
void showLevel(int level) { int i; /***************************************************************** * * Step 1 -- Tile Images * * This data tells us what each different tile will look like * It will be copied into a character block * ****************************************************************/ /***************************************************************** * * Step 2 -- Screen Image (map) * * The numbers in this array tell us which tile we want in each * tile location on the screen. We will copy this data into a * screen block * ****************************************************************/ /***************************************************************** * * Step 3 -- Palette * ****************************************************************/ /***************************************************************** * * Step 4 -- Store Tile Images * ****************************************************************/ int nTiles = 256; int total = nTiles*64/2; switch(level) { case 1: for(i=0; i < total; i++) { // There are 10 tiles. Each tile is described by 64 chars // but we are storing them two to a short so we are // storing data in 64/2 shorts per tile //CHARBLOCKBASE[0].tileimg[i] = myTileImages[i*2] | (myTileImages[i*2+1]<<8); CHARBLOCKBASE[0].tileimg[i] = Level1Tiles[i*2] | (Level1Tiles[i*2+1]<<8); } break; } /***************************************************************** * * Step 5 -- Store map * ****************************************************************/ int totalMap; switch(level) { case 1: totalMap = 32*32; //1024 loadMapBlock(0, Level1Map); for(i=0; i < totalMap; i++) { //SCREENBLOCKBASE[31].tilemap[i] = Level1Map[i]; //SCREENBLOCKBASE[30].tilemap[i] = myScreenMap[i]; SCREENBLOCKBASE[28].tilemap[i] = myBackgroundMap[i]; } break; } /***************************************************************** * * Step 6 -- Store palette * ****************************************************************/ switch(level) { case 1: //loadPalette(myPalette); loadPalette(Level1Pal); break; } /***************************************************************** * * Step 7 -- Set image controls * ****************************************************************/ //REG_DISPCTL = MODE0 | BG0_ENABLE | BG1_ENABLE | BG2_ENABLE; //SETMODE(MODE0 | BG0_ENABLE | BG1_ENABLE | BG2_ENABLE | SPRITE_ENABLE); SETMODE(MODE0 | BG0_ENABLE | /*BG1_ENABLE |*/ BG2_ENABLE | SPRITE_ENABLE); REG_BG0HOFS = 0; REG_BG0VOFS = 0; // MyGUI: Texts,... Comment out this line to turn off BG0 //REG_BG0CNT = BG_SIZE0 | SBB(31) | COLOR256 | CBB(0); // Game REG_BG0CNT = BG_SIZE0 | SBB(30) | COLOR256 | CBB(0); // Background REG_BG2CNT = BG_SIZE0 | SBB(28) | COLOR256 | CBB(0); // DeuXieme part ;) // 30 //REG_BG1CNT = BG_SIZE0 | SBB(31) | COLOR256 | CBB(0); }
int main(int argc, char **argv) { long i; long count = -1; int verbose = 0; int fno = 1; int rno = 0; int eof = 0; long minlen, maxlen; TAPE tp; long bytes; char *device; REAL_BIG *buffer; #ifdef MSDOS long bufsiz = 10240; #else long bufsiz = 32768; #endif if (argc < 2) help(); device = argv[1]; #ifdef MSDOS if (strcmp(device, "0") == 0) { device = "/dev/rst00"; } else if (strcmp(device, "1") == 0) { device = "/dev/rst01"; } else if (strcmp(device, "2") == 0) { device = "/dev/rst02"; } else if (strcmp(device, "3") == 0) { device = "/dev/rst03"; } else if (strcmp(device, "4") == 0) { device = "/dev/rst04"; } else if (strcmp(device, "5") == 0) { device = "/dev/rst05"; } else if (strcmp(device, "6") == 0) { device = "/dev/rst06"; } else if (strcmp(device, "7") == 0) { device = "/dev/rst07"; } #endif /* MSDOS */ for (i = 2; i < argc; i++) { if (strncmp(argv[i], "bs=", strlen("bs=")) == 0) { bufsiz = util_atolk(argv[i] + strlen("bs=")); } else if (strncmp(argv[i], "count=", strlen("count=")) == 0) { count = util_atolk(argv[i] + strlen("count=")); if (count <= 0) { fprintf(stderr,"mtread: illegal count=\n", argv[i]); exit(1); } } else if (strcmp(argv[i], "+v") == 0) { verbose = 1; } else if (strcmp(argv[i], "-v") == 0) { verbose = 0; } else { help(); } } if ((buffer = (REAL_BIG *) malloc(bufsiz)) == NULL) { perror("mtread: malloc"); exit(1); } if (SETMODE(fileno(stdout), O_BINARY) == -1) { perror("mtread: setmode"); exit(1); } if ((tp = mtopen(device, "r")) == (TAPE) NULL) { fprintf(stderr,"mtread: mtopen"); mterror(device); exit(1); } i = minlen = maxlen = 0; while (1) { if (i == count) { if (verbose) fprintf(stderr,"\n"); mtclose(tp); exit(0); } bytes = mtread(tp, buffer, bufsiz); ++i; if (bytes < 0) { mterror("\nmtread: mtread"); #ifdef MSDOS if (bytes == SCSI_SMLREQ) { if (verbose) fprintf(stderr,"Program aborted.\n"); exit(1); } #endif fprintf(stderr,"Attempting to skip 1 record... "); if (mtfsr(tp, 1L) != 1) { mterror("failed"); fprintf(stderr,"Attempting to skip 1 file... "); if (mtfsf(tp, 1) != 1) { mterror("failed"); fprintf(stderr,"Aborting.\n"); exit(1); } else { fprintf(stderr,"OK\n"); ++fno; rno = 0; --i; minlen = 0; maxlen = 0; } } else { fprintf(stderr,"OK\n"); ++rno; } } else if (bytes == 0) { if (++eof < 2) { if (verbose) { fprintf(stderr,"\rFile %d has ", fno); fprintf(stderr,"%d records, ", rno); fprintf(stderr,"min/max length = "); fprintf(stderr,"%ld/%ld\n", minlen,maxlen); } ++fno; rno = 0; --i; minlen = 0; maxlen = 0; } else { if (count > 0 && verbose) { fprintf(stderr,"\nEnd-of-data detected.\n"); } exit(0); } } else { ++rno; eof = 0; if (rno == 1) { minlen = maxlen = bytes; } else { if (bytes < minlen) minlen = bytes; if (bytes > maxlen) maxlen = bytes; } if (verbose) { fprintf(stderr,"\rFile %d, ", fno); fprintf(stderr,"record %d ", rno); fprintf(stderr,"has %ld bytes.", bytes); } if (fwrite(buffer, sizeof(char), bytes, stdout) != bytes) { perror("mtread: fwrite"); exit(1); } } } }
int main(int argc, char **argv) { long i; FILE *ifp, *ofp; float sample; long count = 0; struct sac_header hdr; if (argc == 1) { ifp = stdin; ofp = stdout; } else if (argc == 2) { if ((ifp = fopen(argv[1], "r")) == NULL) { perror(argv[1]); exit(1); } ofp = stdout; } else if (argc == 3) { if ((ifp = fopen(argv[1], "r")) == NULL) { perror(argv[1]); exit(1); } if ((ofp = fopen(argv[2], "wb")) == NULL) { perror(argv[1]); exit(1); } } else { fprintf(stderr,"usage: saca2b: binary_name ascii_name\n"); exit(1); } if (SETMODE(fileno(ofp), O_BINARY) == -1) { perror("saca2b: setmode"); exit(1); } if (sacio_rah(ifp, &hdr) != 0) { perror("saca2b: sacio_rah"); exit(1); } if (sacio_wbh(ofp, &hdr) != 0) { perror("saca2b: sacio_wbh"); exit(1); } for (i = 0; i < hdr.npts; i++) { if (fscanf(ifp, "%f", &sample) != 1) { if (feof(ifp)) { fprintf(stderr,"saca2b: unexpected EOF encountered "); fprintf(stderr,"after %ld samples.\n", i); exit(1); } else { perror("saca2b: fscanf"); exit(1); } } if (fwrite(&sample, sizeof(float), 1, ofp) != 1) { perror("saca2b: fwrite"); exit(1); } } exit(0); }
int main(int argc, char **argv) { short sdata; long ldata, offset, nbytes; float fdata; int nionum, type; static char fname[] = "Xnnn.D"; FILE *fp; struct sac_header hdr; if (argc != 2) { fprintf(stderr,"usage: nio2sac NIO_number > output\n"); exit(1); } nionum = atoi(argv[1]); if (nionum < 0) { nionum *= -1; if (nionum < 100 || nionum > 999) { fprintf(stderr,"nio2sac: bad file number = -%d\n", nionum); exit(2); } else { sprintf(fname, "R%3.3d.D", nionum); } type = FLOAT; offset = 0L; } else if (nionum >= 1100 && nionum <= 1999) { nionum -= 1000; sprintf(fname, "L%3.3d.D", nionum); type = LONG; offset = 0L; } else if (nionum >= 4100 && nionum <= 4999) { nionum -= 4000; sprintf(fname, "H%3.3d.D", nionum); type = SHORT; offset = 3072L; } else if (nionum >= 100 && nionum <= 999) { sprintf(fname, "I%3.3d.D", nionum); type = SHORT; offset = 0L; } else { fprintf(stderr,"nio2sac: bad file number = %d\n", nionum); exit(2); } if ((fp = fopen(fname, "rb")) == NULL) { fprintf(stderr, "nio2sac: fopen: "); perror(fname); exit(3); } if (SETMODE(fileno(stdout), O_BINARY) == -1) { perror("nio2sac: setmode"); exit(3); } if (fseek(fp, 0L, SEEK_END) != 0) { fprintf(stderr, "nio2sac: fseek: "); perror(fname); exit(3); } nbytes = ftell(fp); rewind(fp); hdr = null_sac_header; nbytes -= offset; if (type == FLOAT) { hdr.npts = nbytes / sizeof(float); } else if (type == LONG) { hdr.npts = nbytes / sizeof(long); } else { hdr.npts = nbytes / sizeof(short); } hdr.delta = 1.0; hdr.b = 0.0; hdr.e = (hdr.npts - 1) * hdr.delta; hdr.iftype = ITIME; hdr.leven = 1; hdr.iztype = IB; strcpy(hdr.kinst, "NIOLIB"); if (sacio_wbh(stdout, &hdr) != 0) { perror("nio2sac: sacio_wbh"); exit(1); } if (offset && fseek(fp, offset, SEEK_SET) != 0) { fprintf(stderr, "nio2sac: fseek: "); perror(fname); exit(3); } switch (type) { case FLOAT: while (fread(&fdata, sizeof(float), 1, fp) == 1) { if (fwrite(&fdata, sizeof(float), 1, stdout) != 1) { perror("nio2sac: fwrite"); exit(1); } } break; case LONG: while (fread(&ldata, sizeof(long), 1, fp) == 1) { fdata = (float) ldata; if (fwrite(&fdata, sizeof(float), 1, stdout) != 1) { perror("nio2sac: fwrite"); exit(1); } } break; case SHORT: while (fread(&sdata, sizeof(short), 1, fp) == 1) { fdata = (float) sdata; if (fwrite(&fdata, sizeof(float), 1, stdout) != 1) { perror("nio2sac: fwrite"); exit(1); } } break; default: fprintf(stderr,"nio2sac: undefined type!!!\n"); exit(4); } if (ferror(fp)) { fprintf(stderr,"nio2sac fread: "); perror(fname); exit(5); } fclose(fp); exit(0); }
/*-------------------------------------------------------------------*/ DLL_EXPORT int impl(int argc, char *argv[]) { char *cfgfile; /* -> Configuration filename */ char pathname[MAX_PATH]; /* work area for filenames */ #if defined ( OPTION_LOCK_CONFIG_FILE ) int fd_cfg = -1; /* fd for config file */ #if !defined ( _MSVC_ ) struct flock fl_cfg; /* file lock for conf file */ #endif #endif int c; /* Work area for getopt */ int arg_error = 0; /* 1=Invalid arguments */ char *msgbuf; /* */ int msgnum; /* */ int msgcnt; /* */ TID rctid; /* RC file thread identifier */ TID logcbtid; /* RC file thread identifier */ int rc; #if defined(EXTERNALGUI) int e_gui = FALSE; /* EXTERNALGUI parm */ #endif #if defined(OPTION_DYNAMIC_LOAD) #define MAX_DLL_TO_LOAD 50 char *dll_load[MAX_DLL_TO_LOAD]; /* Pointers to modnames */ int dll_count; /* index into array */ #endif /* Seed the pseudo-random number generator */ srand( time(NULL) ); /* Clear the system configuration block */ memset( &sysblk, 0, sizeof( SYSBLK ) ); VERIFY( MLOCK( &sysblk, sizeof( SYSBLK )) == 0); #if defined (_MSVC_) _setmaxstdio(2048); #endif /* Initialize EYE-CATCHERS for SYSBLK */ memset(&sysblk.blknam,SPACE,sizeof(sysblk.blknam)); memset(&sysblk.blkver,SPACE,sizeof(sysblk.blkver)); memset(&sysblk.blkend,SPACE,sizeof(sysblk.blkend)); sysblk.blkloc = swap_byte_U64((U64)((uintptr_t)&sysblk)); memcpy(sysblk.blknam,HDL_NAME_SYSBLK,strlen(HDL_NAME_SYSBLK)); memcpy(sysblk.blkver,HDL_VERS_SYSBLK,strlen(HDL_VERS_SYSBLK)); sysblk.blksiz = swap_byte_U32((U32)sizeof(SYSBLK)); { char buf[32]; MSGBUF( buf, "END%13.13s", HDL_NAME_SYSBLK ); memcpy(sysblk.blkend, buf, sizeof(sysblk.blkend)); } /* Initialize SETMODE and set user authority */ SETMODE(INIT); #if defined(OPTION_DYNAMIC_LOAD) for ( dll_count = 0; dll_count < MAX_DLL_TO_LOAD; dll_count++ ) dll_load[dll_count] = NULL; dll_count = -1; #endif SET_THREAD_NAME("impl"); /* Initialize 'hostinfo' BEFORE display_version is called */ init_hostinfo( &hostinfo ); #ifdef _MSVC_ /* Initialize sockets package */ VERIFY( socket_init() == 0 ); #endif /* Ensure hdl_shut is called in case of shutdown hdl_shut will ensure entries are only called once */ atexit(hdl_shut); if ( argc > 0 ) { int i,len; for (len = 0, i = 0; i < argc; i++ ) len += (int)strlen( (char *)argv[i] ) + 1; sysblk.hercules_cmdline = (char *)malloc( len ); strlcpy( sysblk.hercules_cmdline, argv[0], len ); for ( i = 1; i < argc; i++ ) { strlcat( sysblk.hercules_cmdline, " ", len ); strlcat( sysblk.hercules_cmdline, argv[i], len ); } } /* Set program name */ if ( argc > 0 ) { if ( strlen(argv[0]) == 0 ) { sysblk.hercules_pgmname = strdup("hercules"); sysblk.hercules_pgmpath = strdup(""); } else { char path[MAX_PATH]; #if defined( _MSVC_ ) GetModuleFileName( NULL, path, MAX_PATH ); #else strncpy(path,argv[0],sizeof(path)-1); #endif sysblk.hercules_pgmname = strdup(basename(path)); #if !defined( _MSVC_ ) strncpy(path,argv[0],sizeof(path)-1); #endif sysblk.hercules_pgmpath = strdup(dirname(path)); } } else { sysblk.hercules_pgmname = strdup("hercules"); sysblk.hercules_pgmpath = strdup(""); } #if defined( OPTION_CONFIG_SYMBOLS ) /* These were moved from console.c to make them available sooner */ set_symbol( "VERSION", VERSION); set_symbol( "BDATE", __DATE__ ); set_symbol( "BTIME", __TIME__ ); { char num_procs[64]; if ( hostinfo.num_packages != 0 && hostinfo.num_physical_cpu != 0 && hostinfo.num_logical_cpu != 0 ) { MSGBUF( num_procs, "LP=%d, Cores=%d, CPUs=%d", hostinfo.num_logical_cpu, hostinfo.num_physical_cpu, hostinfo.num_packages ); } else { if ( hostinfo.num_procs > 1 ) MSGBUF( num_procs, "MP=%d", hostinfo.num_procs ); else if ( hostinfo.num_procs == 1 ) strlcpy( num_procs, "UP", sizeof(num_procs) ); else strlcpy( num_procs, "", sizeof(num_procs) ); } set_symbol( "HOSTNAME", hostinfo.nodename ); set_symbol( "HOSTOS", hostinfo.sysname ); set_symbol( "HOSTOSREL", hostinfo.release ); set_symbol( "HOSTOSVER", hostinfo.version ); set_symbol( "HOSTARCH", hostinfo.machine ); set_symbol( "HOSTNUMCPUS", num_procs ); } set_symbol( "MODNAME", sysblk.hercules_pgmname ); set_symbol( "MODPATH", sysblk.hercules_pgmpath ); #endif // defined( OPTION_CONFIG_SYMBOLS ) sysblk.sysgroup = DEFAULT_SYSGROUP; sysblk.msglvl = DEFAULT_MLVL; /* Defaults to TERSE and DEVICES */ /* set default console port address */ sysblk.cnslport = strdup("3270"); /* set default tape autoinit value to OFF */ sysblk.noautoinit = TRUE; /* default for system dasd cache is on */ sysblk.dasdcache = TRUE; #if defined(OPTION_MSGCLR) || defined(OPTION_MSGHLD) /* set default error message display (emsg) */ sysblk.emsg = EMSG_ON; #endif #if defined( OPTION_SHUTDOWN_CONFIRMATION ) /* set default quit timeout value (also ssd) */ sysblk.quitmout = QUITTIME_PERIOD; #endif /* Default command separator to off (NULL) */ sysblk.cmdsep = NULL; #if defined(_FEATURE_SYSTEM_CONSOLE) /* set default for scpecho to TRUE */ sysblk.scpecho = TRUE; /* set fault for scpimply to FALSE */ sysblk.scpimply = FALSE; #endif /* set default system state to reset */ sysblk.sys_reset = TRUE; /* set default SHCMDOPT enabled */ sysblk.shcmdopt = SHCMDOPT_ENABLE + SHCMDOPT_DIAG8; /* Save process ID */ sysblk.hercules_pid = getpid(); /* Save thread ID of main program */ sysblk.impltid = thread_id(); /* Save TOD of when we were first IMPL'ed */ time( &sysblk.impltime ); /* Set to LPAR mode with LPAR 1, LPAR ID of 01, and CPUIDFMT 0 */ sysblk.lparmode = 1; /* LPARNUM 1 # LPAR ID 01 */ sysblk.lparnum = 1; /* ... */ sysblk.cpuidfmt = 0; /* CPUIDFMT 0 */ sysblk.operation_mode = om_mif; /* Default to MIF operaitons */ /* set default CPU identifier */ sysblk.cpumodel = 0x0586; sysblk.cpuversion = 0xFD; sysblk.cpuserial = 0x000001; sysblk.cpuid = createCpuId(sysblk.cpumodel, sysblk.cpuversion, sysblk.cpuserial, 0); /* set default Program Interrupt Trace to NONE */ sysblk.pgminttr = OS_NONE; sysblk.timerint = DEF_TOD_UPDATE_USECS; /* set default thread priorities */ sysblk.hercprio = DEFAULT_HERCPRIO; sysblk.todprio = DEFAULT_TOD_PRIO; sysblk.cpuprio = DEFAULT_CPU_PRIO; sysblk.devprio = DEFAULT_DEV_PRIO; sysblk.srvprio = DEFAULT_SRV_PRIO; /* Cap the default priorities at zero if setuid not available */ #if !defined( _MSVC_ ) #if !defined(NO_SETUID) if (sysblk.suid) #endif { if (sysblk.hercprio < 0) sysblk.hercprio = 0; if (sysblk.todprio < 0) sysblk.todprio = 0; if (sysblk.cpuprio < 0) sysblk.cpuprio = 0; if (sysblk.devprio < 0) sysblk.devprio = 0; if (sysblk.srvprio < 0) sysblk.srvprio = 0; } #endif /* set default console keep alive values */ sysblk.kaidle = KEEPALIVE_IDLE_TIME; sysblk.kaintv = KEEPALIVE_PROBE_INTERVAL; sysblk.kacnt = KEEPALIVE_PROBE_COUNT; #if defined(_FEATURE_ECPSVM) sysblk.ecpsvm.available = 0; sysblk.ecpsvm.level = 20; #endif #ifdef PANEL_REFRESH_RATE sysblk.panrate = PANEL_REFRESH_RATE_SLOW; #endif #if defined( OPTION_SHUTDOWN_CONFIRMATION ) /* Set the quitmout value */ sysblk.quitmout = QUITTIME_PERIOD; /* quit timeout value */ #endif #if defined(OPTION_SHARED_DEVICES) sysblk.shrdport = 0; #endif #ifdef OPTION_MSGHLD /* Set the default timeout value */ sysblk.keep_timeout_secs = 120; #endif #if defined(OPTION_CONFIG_SYMBOLS) && defined(OPTION_BUILTIN_SYMBOLS) /* setup defaults for BUILTIN symbols */ { char buf[8]; set_symbol("LPARNAME", str_lparname()); set_symbol("LPARNUM", "1"); set_symbol("CPUIDFMT", "0"); MSGBUF( buf, "%06X", sysblk.cpuserial ); set_symbol( "CPUSERIAL", buf ); MSGBUF( buf, "%04X", sysblk.cpumodel ); set_symbol( "CPUMODEL", buf ); } #endif /* defined(OPTION_CONFIG_SYMBOLS) && defined(OPTION_BUILTIN_SYMBOLS */ #if defined(_FEATURE_CMPSC_ENHANCEMENT_FACILITY) sysblk.zpbits = DEF_CMPSC_ZP_BITS; #endif /* Initialize locks, conditions, and attributes */ initialize_lock (&sysblk.config); initialize_lock (&sysblk.todlock); initialize_lock (&sysblk.mainlock); sysblk.mainowner = LOCK_OWNER_NONE; initialize_lock (&sysblk.intlock); initialize_lock (&sysblk.iointqlk); sysblk.intowner = LOCK_OWNER_NONE; initialize_lock (&sysblk.sigplock); initialize_lock (&sysblk.mntlock); initialize_lock (&sysblk.scrlock); initialize_lock (&sysblk.crwlock); initialize_lock (&sysblk.ioqlock); initialize_condition (&sysblk.ioqcond); #if defined(OPTION_CMDSER) initialize_lock (&sysblk.cmdlock); initialize_condition (&sysblk.cmdcond); #endif /*defined(OPTION_CMDSER)*/ #ifdef FEATURE_MESSAGE_SECURITY_ASSIST_EXTENSION_3 /* Initialize the wrapping key registers lock */ initialize_rwlock(&sysblk.wklock); #endif /* Initialize thread creation attributes so all of hercules can use them at any time when they need to create_thread */ initialize_detach_attr (DETACHED); initialize_join_attr (JOINABLE); initialize_condition (&sysblk.cpucond); { int i; for (i = 0; i < MAX_CPU_ENGINES; i++) initialize_lock (&sysblk.cpulock[i]); } initialize_condition (&sysblk.sync_cond); initialize_condition (&sysblk.sync_bc_cond); /* Copy length for regs */ sysblk.regs_copy_len = (int)((uintptr_t)&sysblk.dummyregs.regs_copy_end - (uintptr_t)&sysblk.dummyregs); /* Set the daemon_mode flag indicating whether we running in background/daemon mode or not (meaning both stdout/stderr are redirected to a non-tty device). Note that this flag needs to be set before logger_init gets called since the logger_logfile_write function relies on its setting. */ sysblk.daemon_mode = !isatty(STDERR_FILENO) && !isatty(STDOUT_FILENO); /* Initialize the logmsg pipe and associated logger thread. This causes all subsequent logmsg's to be redirected to the logger facility for handling by virtue of stdout/stderr being redirected to the logger facility. */ logger_init(); /* Setup the initial codepage */ set_codepage(NULL); /* Now display the version information again after logger_init has been called so that either the panel display thread or the external gui can see the version which was previously possibly only displayed to the actual physical screen the first time we did it further above (depending on whether we're running in daemon_mode (external gui mode) or not). This it the call that the panel thread or the one the external gui actually "sees". The first call further above wasn't seen by either since it was issued before logger_init was called and thus got written directly to the physical screen whereas this one will be inter- cepted and handled by the logger facility thereby allowing the panel thread or external gui to "see" it and thus display it. */ display_version (stdout, "Hercules", TRUE); #ifdef EXTERNALGUI if (argc >= 1 && strncmp(argv[argc-1],"EXTERNALGUI",11) == 0) { e_gui = TRUE; argc--; } #endif #if !defined(WIN32) && !defined(HAVE_STRERROR_R) strerror_r_init(); #endif #if defined(OPTION_SCSI_TAPE) initialize_lock ( &sysblk.stape_lock ); initialize_condition ( &sysblk.stape_getstat_cond ); InitializeListHead ( &sysblk.stape_mount_link ); InitializeListHead ( &sysblk.stape_status_link ); #endif /* defined(OPTION_SCSI_TAPE) */ /* Get name of configuration file or default to hercules.cnf */ if(!(cfgfile = getenv("HERCULES_CNF"))) cfgfile = "hercules.cnf"; /* Process the command line options */ { #define HERCULES_BASE_OPTS "hf:r:db:v" #define HERCULES_SYM_OPTS "" #define HERCULES_HDL_OPTS "" #if defined(OPTION_CONFIG_SYMBOLS) #undef HERCULES_SYM_OPTS #define HERCULES_SYM_OPTS "s:" #endif #if defined(OPTION_DYNAMIC_LOAD) #undef HERCULES_HDL_OPTS #define HERCULES_HDL_OPTS "p:l:" #endif #define HERCULES_OPTS_STRING HERCULES_BASE_OPTS HERCULES_SYM_OPTS HERCULES_HDL_OPTS #if defined(HAVE_GETOPT_LONG) static struct option longopts[] = { { "help", no_argument, NULL, 'h' }, { "config", required_argument, NULL, 'f' }, { "rcfile", required_argument, NULL, 'r' }, { "daemon", no_argument, NULL, 'd' }, { "herclogo", required_argument, NULL, 'b' }, { "verbose", no_argument, NULL, 'v' }, #if defined(OPTION_CONFIG_SYMBOLS) { "defsym", required_argument, NULL, 's' }, #endif #if defined(OPTION_DYNAMIC_LOAD) { "modpath", required_argument, NULL, 'p' }, { "ldmod", required_argument, NULL, 'l' }, #endif { NULL, 0, NULL, 0 } }; while ((c = getopt_long( argc, argv, HERCULES_OPTS_STRING, longopts, NULL )) != EOF) #else while ((c = getopt( argc, argv, HERCULES_OPTS_STRING )) != EOF) #endif { switch (c) { case 'h': arg_error = 1; break; case 'f': cfgfile = optarg; break; case 'r': rcname = optarg; break; #if defined(OPTION_CONFIG_SYMBOLS) case 's': { char *sym = NULL; char *value = NULL; char *strtok_str = NULL; if ( strlen( optarg ) >= 3 ) { sym = strtok_r( optarg, "=", &strtok_str); value = strtok_r( NULL, "=", &strtok_str); if ( sym != NULL && value != NULL ) { int j; for( j = 0; j < (int)strlen( sym ); j++ ) if ( islower( sym[j] ) ) { sym[j] = toupper( sym[j] ); } set_symbol(sym, value); } else WRMSG(HHC01419, "E" ); } else WRMSG(HHC01419, "E"); } break; #endif /* defined(OPTION_CONFIG_SYMBOLS) */ #if defined(OPTION_DYNAMIC_LOAD) case 'p': if(optarg) hdl_setpath(strdup(optarg), FALSE); break; case 'l': { char *dllname, *strtok_str = NULL; for(dllname = strtok_r(optarg,", ",&strtok_str); dllname; dllname = strtok_r(NULL,", ",&strtok_str)) { if (dll_count < MAX_DLL_TO_LOAD - 1) dll_load[++dll_count] = strdup(dllname); else { WRMSG(HHC01406, "W", MAX_DLL_TO_LOAD); break; } } } break; #endif /* defined(OPTION_DYNAMIC_LOAD) */ case 'b': sysblk.logofile = optarg; break; case 'v': sysblk.msglvl |= MLVL_VERBOSE; break; case 'd': sysblk.daemon_mode = 1; break; default: arg_error = 1; } /* end switch(c) */ } /* end while */ } /* end Process the command line options */ /* Treat filename None as special */ if(!strcasecmp(cfgfile,"None")) cfgfile = NULL; if (optind < argc) arg_error = 1; /* Terminate if invalid arguments were detected */ if (arg_error) { char pgm[MAX_PATH]; char* strtok_str = NULL; strncpy(pgm, sysblk.hercules_pgmname, sizeof(pgm)); /* Show them all of our command-line arguments... */ WRMSG (HHC01414, "S", ""); // (blank line) WRMSG (HHC01414, "S", ""); // (blank line) #if defined(OPTION_DYNAMIC_LOAD) // "Usage: %s [-f config-filename] [-d] [-b logo-filename] [-s sym=val]%s [> logfile]" WRMSG (HHC01407, "S", strtok_r(pgm,".",&strtok_str), " [-p dyn-load-dir] [[-l dynmod-to-load]...]"); #else WRMSG (HHC01407, "S", strtok_r(pgm,".", &strtok_str), ""); #endif /* defined(OPTION_DYNAMIC_LOAD) */ WRMSG (HHC01414, "S", ""); // (blank line) WRMSG (HHC01414, "S", ""); // (blank line) fflush(stderr); fflush(stdout); usleep(100000); return(1); } /* Initialize runtime opcode tables */ init_opcode_tables(); #if defined(OPTION_DYNAMIC_LOAD) /* Initialize the hercules dynamic loader */ hdl_main(); /* Load modules requested at startup */ if (dll_count >= 0) { int hl_err = FALSE; for ( dll_count = 0; dll_count < MAX_DLL_TO_LOAD; dll_count++ ) { if (dll_load[dll_count] != NULL) { if (hdl_load(dll_load[dll_count], HDL_LOAD_DEFAULT) != 0) { hl_err = TRUE; } free(dll_load[dll_count]); } else break; } if (hl_err) { usleep(10000); // give logger time to issue error message WRMSG(HHC01408, "S"); delayed_exit(-1); return(1); } } #endif /* defined(OPTION_DYNAMIC_LOAD) */ #ifdef EXTERNALGUI /* Set GUI flag if specified as final argument */ if (e_gui) { #if defined(OPTION_DYNAMIC_LOAD) if (hdl_load("dyngui",HDL_LOAD_DEFAULT) != 0) { usleep(10000); /* (give logger thread time to issue preceding HHC01516E message) */ WRMSG(HHC01409, "S"); delayed_exit(-1); return(1); } #endif /* defined(OPTION_DYNAMIC_LOAD) */ } #endif /*EXTERNALGUI*/ /* Register the SIGINT handler */ if ( signal (SIGINT, sigint_handler) == SIG_ERR ) { WRMSG(HHC01410, "S", "SIGINT", strerror(errno)); delayed_exit(-1); return(1); } /* Register the SIGTERM handler */ if ( signal (SIGTERM, sigterm_handler) == SIG_ERR ) { WRMSG(HHC01410, "S", "SIGTERM", strerror(errno)); delayed_exit(-1); return(1); } #if defined( _MSVC_ ) /* Register the Window console ctrl handlers */ if (!IsDebuggerPresent()) { if (!SetConsoleCtrlHandler( console_ctrl_handler, TRUE )) { WRMSG( HHC01410, "S", "Console-ctrl", strerror( errno )); delayed_exit(-1); return(1); } } #endif #if defined(HAVE_DECL_SIGPIPE) && HAVE_DECL_SIGPIPE /* Ignore the SIGPIPE signal, otherwise Hercules may terminate with Broken Pipe error if the printer driver writes to a closed pipe */ if ( signal (SIGPIPE, SIG_IGN) == SIG_ERR ) { WRMSG(HHC01411, "E", strerror(errno)); } #endif { int fds[2]; initialize_lock(&sysblk.cnslpipe_lock); initialize_lock(&sysblk.sockpipe_lock); sysblk.cnslpipe_flag=0; sysblk.sockpipe_flag=0; VERIFY( create_pipe(fds) >= 0 ); sysblk.cnslwpipe=fds[1]; sysblk.cnslrpipe=fds[0]; VERIFY( create_pipe(fds) >= 0 ); sysblk.sockwpipe=fds[1]; sysblk.sockrpipe=fds[0]; } #if !defined(NO_SIGABEND_HANDLER) { struct sigaction sa; sa.sa_sigaction = (void*)&sigabend_handler; #ifdef SA_NODEFER sa.sa_flags = SA_NODEFER; #else sa.sa_flags = 0; #endif if( sigaction(SIGILL, &sa, NULL) || sigaction(SIGFPE, &sa, NULL) || sigaction(SIGSEGV, &sa, NULL) || sigaction(SIGBUS, &sa, NULL) || sigaction(SIGUSR1, &sa, NULL) || sigaction(SIGUSR2, &sa, NULL) ) { WRMSG(HHC01410, "S", "SIGILL/FPE/SEGV/BUS/USR", strerror(errno)); delayed_exit(-1); return(1); } } #endif /*!defined(NO_SIGABEND_HANDLER)*/ if(cfgfile) { /* attempt to get lock on config file */ hostpath(pathname, cfgfile, sizeof(pathname)); #if defined( OPTION_LOCK_CONFIG_FILE ) /* Test that we can get a read the file */ if ( ( fd_cfg = HOPEN( pathname, O_RDONLY, S_IRUSR | S_IRGRP ) ) < 0 ) { if ( errno == EACCES ) { WRMSG( HHC01453, "S", cfgfile, strerror( errno ) ); delayed_exit(-1); return(1); } } else { if ( lseek(fd_cfg, 0L, 2) < 0 ) { if ( errno == EACCES ) { WRMSG( HHC01453, "S", cfgfile, strerror( errno ) ); delayed_exit(-1); return(1); } } close( fd_cfg ); } /* File was not lock, therefore we can proceed */ #endif // OPTION_LOCK_CONFIG_FILE } /* System initialisation time */ sysblk.todstart = hw_clock() << 8; #if !defined(NO_SIGABEND_HANDLER) /* Start the watchdog */ rc = create_thread (&sysblk.wdtid, DETACHED, watchdog_thread, NULL, "watchdog_thread"); if (rc) { WRMSG(HHC00102, "E", strerror(rc)); delayed_exit(-1); return(1); } #endif /*!defined(NO_SIGABEND_HANDLER)*/ if(log_callback) { // 'herclin' called us. IT'S in charge. Create its requested // logmsg intercept callback function and return back to it. rc = create_thread(&logcbtid,DETACHED, log_do_callback,NULL,"log_do_callback"); if (rc) WRMSG(HHC00102, "E", strerror(rc)); return(0); } hdl_adsc("release_config", release_config, NULL); /* Build system configuration */ if ( build_config (cfgfile) ) { delayed_exit(-1); return(1); } /* Start up the RC file processing thread */ rc = create_thread(&rctid,DETACHED, process_rc_file,NULL,"process_rc_file"); if (rc) WRMSG(HHC00102, "E", strerror(rc)); #if defined( OPTION_LOCK_CONFIG_FILE ) if(cfgfile) { if ( ( fd_cfg = HOPEN( pathname, O_RDONLY, S_IRUSR | S_IRGRP ) ) < 0 ) { WRMSG( HHC01432, "S", pathname, "open()", strerror( errno ) ); delayed_exit(-1); return(1); } else { #if defined( _MSVC_ ) if( ( rc = _locking( fd_cfg, _LK_NBRLCK, 1L ) ) < 0 ) { int rc = errno; WRMSG( HHC01454, "S", pathname, "_locking()", strerror( errno ) ); delayed_exit(-1); return(1); } #else fl_cfg.l_type = F_RDLCK; fl_cfg.l_whence = SEEK_SET; fl_cfg.l_start = 0; fl_cfg.l_len = 1; if ( fcntl(fd_cfg, F_SETLK, &fl_cfg) == -1 ) { if (errno == EACCES || errno == EAGAIN) { WRMSG( HHC01432, "S", pathname, "fcntl()", strerror( errno ) ); delayed_exit(-1); return(1); } } #endif } } #endif // OPTION_LOCK_CONFIG_FILE //--------------------------------------------------------------- // The below functions will not return until Hercules is shutdown //--------------------------------------------------------------- /* Activate the control panel */ if(!sysblk.daemon_mode) panel_display (); else { #if defined(OPTION_DYNAMIC_LOAD) if(daemon_task) daemon_task (); else #endif /* defined(OPTION_DYNAMIC_LOAD) */ { /* Tell RC file and HAO threads they may now proceed */ sysblk.panel_init = 1; /* Retrieve messages from logger and write to stderr */ while (1) if((msgcnt = log_read(&msgbuf, &msgnum, LOG_BLOCK))) if(isatty(STDERR_FILENO)) fwrite(msgbuf,msgcnt,1,stderr); } } // ----------------------------------------------------- // *** Hercules has been shutdown (PAST tense) *** // ----------------------------------------------------- #if defined( OPTION_LOCK_CONFIG_FILE ) if(cfgfile) close( fd_cfg ); // release config file lock #endif // OPTION_LOCK_CONFIG_FILE ASSERT( sysblk.shutdown ); // (why else would we be here?!) #ifdef _MSVC_ SetConsoleCtrlHandler(console_ctrl_handler, FALSE); socket_deinit(); #endif #if defined(OPTION_MSGCLR) || defined(OPTION_MSGHLD) if ( sysblk.emsg & EMSG_TEXT ) fprintf(stdout, HHC01412 ); else #endif fprintf(stdout, MSG(HHC01412, "I")); fflush(stdout); usleep(10000); return 0; } /* end function impl */