void onkill(char *source, char *target, char *comment) { char buffer[200]; if (!strcasecmp(target, mynick)) { #if 0 /* ignore kill for nick collisions because we * already check in onnick() if we're collided. * This kill is prolly a lost kill resulting of * another nick collision.. */ if (strstr(comment, "older nick overruled") || strstr(comment, "collided yourself")) { log("ERROR: Nick collision on me?"); return; } #endif sprintf(buffer, ":%s SQUIT %s 0 :killed by %s\n", SERVERNAME, SERVERNAME, source); sendtoserv(buffer); dumpbuff(); close(Irc.fd); Irc.fd = -1; if (reconnect(server)) { try_later(server); } #ifdef BACKUP } else if (!strcasecmp(target, MAIN_NICK)) { quit(MAIN_NICK " is back", 0); #endif #ifdef FAKE_UWORLD } else if (!strcasecmp(target, UFAKE_NICK)) { char buffer[200]; sprintf(buffer, "%s is KILLED by %s", UFAKE_NICK, source); log(buffer); Uworld_status = 0; KillUworld("Killed"); #endif } else onquit(target); }
int main (int argc, char *argv[]) { fd_set readset; fd_set writeset; int n, sel; int c, errflg=0; struct sigaction sigact; sigset_t sigmask; char *scratch, *next_scratch; char nmea_buf[1024]; char send_buf[1024]; int gps_buf[15]; int nmea_index = 0; int send_len = 0; /* Set default options */ outfile = stdout; int sAnt = 0; memset(gps_buf, 0, 15); memset(send_buf, 0, 1024); send_len = 0; /* Process options */ while ((c = getopt(argc, argv, "Vlqvs:o:p:t:m:u:g:/:")) != EOF) switch (c) { case 'q': quiet=1; break; case 'v': verbose = 1; break; case 'l': linebuff = 1; break; case 'p': opt_ptyname = optarg; break; case 't': opt_ttyname = optarg; break; case 'm': /* mode for pty: [user,[group,]]mode */ scratch = strdup(optarg); if ((next_scratch = strchr(scratch,',')) != NULL) { /* Username */ *next_scratch = '\0'; next_scratch++; frontend_owner = find_uid(scratch); scratch = next_scratch; if ((next_scratch = strchr(scratch,',')) != NULL) { /* Group */ *next_scratch = '\0'; next_scratch++; frontend_group = find_gid(scratch); scratch = next_scratch; } } frontend_mode = strtol(scratch,NULL,8); break; case 'u': switch_uid = find_uid(optarg); break; case 'g': switch_gid = find_gid(optarg); break; case '/': switch_root = strdup(optarg); break; case 's': settings = optarg; break; case 'o': outfilename=optarg; if ( (outfile = fopen(outfilename,"w")) == NULL) errorf("Couldn't open output file '%s' for write: %s\n",outfilename,strerror(errno)); break; case 'V': puts(VERSION); exit(0); case '?': default: errflg++; break; } if (errflg || ((argc-optind) < 1) || ((argc-optind) > 2)) { usage(argv[0]); exit (2); } /* Process the two non-flag options */ backend = argv[optind]; if ((argc-optind) == 2) frontend = argv[optind+1]; if (strcmp(frontend,"-") == 0) frontend = NULL; if (linebuff) setlinebuf(outfile); atexit (closedown); /* Do some initialization */ stty_initstore(); sendudp_open(); /* Setup backend */ if (setup_backend(backfd) < 0) errorf ("select failed. errno = %d\n", errno); /* Setup frontend */ if ((setup_frontend(frontfd)) < 0) errorf("setup_frontend failed: %s\n",strerror(errno)); /* Drop privileges if we've been asked to */ if (switch_root) { if (chroot(switch_root) != 0) errorf("chroot(%s) failed: %s\n",switch_root,strerror(errno)); } if (switch_gid != -1) { if (setgroups(1,&switch_gid) == -1) errorf("setgroups(1,[%d]) failed: %s\n",switch_gid, strerror(errno)); if (setregid(switch_gid, switch_gid) == -1) errorf("setregid(%d,%d) failed: %s\n",switch_gid,switch_gid); if (getgid() != switch_gid) errorf("setregid succeeded, but we're the wrong gid!"); if (getegid() != switch_gid) errorf("setregid succeeded, but we're the wrong effective gid!"); } if (switch_uid != -1) { if (setreuid(switch_uid, switch_uid) == -1) errorf("setreuid(%d,%d) failed: %s\n",switch_uid,switch_uid,strerror(errno)); if (getuid() != switch_uid) errorf("setreuid succeeded, but we're the wrong uid!"); if (geteuid() != switch_uid) errorf("setregid succeeded, but we're the wrong effective uid!"); } /* calc (initial) max file descriptor to use in select() */ fdmax = max(backfd[0], frontfd[0]); /* Set up signal handlers and such */ sigemptyset(&sigmask); memset(&sigact,0,sizeof sigact); sigact.sa_handler = sigdeath; sigact.sa_mask = sigmask; sigaction(SIGHUP,&sigact,NULL); sigaction(SIGINT,&sigact,NULL); sigaction(SIGQUIT,&sigact,NULL); sigaction(SIGPIPE,&sigact,NULL); sigaction(SIGTERM,&sigact,NULL); sigact.sa_handler = sigchld; sigaction(SIGCHLD,&sigact,NULL); pthread_t udp_recv; pthread_create(&udp_recv, NULL, udp_recv_gnssmode_thread, NULL); pthread_detach(udp_recv); struct tm p; time_t timep; while (!please_die_now) { do { FD_ZERO (&readset); FD_SET (backfd[0], &readset); FD_SET (frontfd[0], &readset); FD_ZERO (&writeset); FD_SET (backfd[1], &writeset); FD_SET (frontfd[1], &writeset); sel = select(fdmax + 1, &readset, &writeset, NULL, NULL); } while (sel == -1 && errno == EINTR && !please_die_now); if (sel == -1 && errno != EINTR) errorf ("select failed. errno = %d\n", errno); else if (please_die_now) break; if (FD_ISSET(backfd[0], &readset)) { if ((n = read(backfd[0], buff, BUFF_SIZE)) == 0) { /* Serial port has closed. This doesn't really make sense for * a real serial port, but for sockets and programs, probably * we should just exit. */ if (!quiet) errorf("Backend device was closed.\n"); break; } else if (n < 0) { if ( (errno != EAGAIN) && (errno != EINTR) ) { errorf("Error reading from backend device: %s\n",strerror(errno)); } break; } else { /* We should handle this better. FIX */ if(FD_ISSET(frontfd[1], &writeset)) { if (write (frontfd[1], buff, n) != n) errorf("Error writing to frontend device: %s\n",strerror(errno)); } // if (!quiet) // dumpbuff(1,buff,n); //nmea decode { tNmea_Msg nmeamsg ; int i=0; int ret = 0; for(i=0; i<n; i++) { if(buff[i] == '$') nmea_index = 0; nmea_buf[nmea_index] = buff[i]; if(buff[i] == '\r') nmea_buf[nmea_index] = 0; if(buff[i] == '\n') { nmea_buf[nmea_index] = 0; #if 0 if (!quiet) fprintf(outfile,"%s\n",nmea_buf); #endif #if 1 ret = NmeaDecode(nmea_buf,&nmeamsg); if(nmeamsg.nMsgMask == GGA_DECODE) { memset(send_buf, 0, 1024); send_len = 0; } memcpy(send_buf+send_len, nmea_buf, nmea_index); send_len += strlen(nmea_buf); send_buf[send_len] = 0x0D; send_len ++; if(nmeamsg.nMsgMask & ANT_DECODE) { sAnt = nmeamsg.nAnttenaStatus; //sendudp(1011,&sAnt, 4); //gui sAnt |= (nmeamsg.version<<8); } if(nmeamsg.nMsgMask & RMC_DECODE) { if (nmea_buf[3] == 'R' && nmea_buf[4] == 'M' && nmea_buf[5] == 'C' ) { if (!quiet) fprintf(outfile,"%s=len=%d\n",nmea_buf, strlen(nmea_buf)); sendudp(1014,nmea_buf, strlen(nmea_buf)); //busdemaon } } if(nmeamsg.nMsgMask >= 0x8F) { sendudp(1012, send_buf, send_len); sendudp(1011,&nmeamsg.aSvInfo, 2*sizeof(tSVData)); //gui satelite display if (!quiet) { //fprintf(outfile,"\r\n\r\n%s---------send_len=%d\n\n",send_buf,send_len); fprintf(outfile,"nAnttenaStatus=%d,position=%c mode=%d fy=%f fx=%f (%d-%d-%d)(%d:%d:%f)\n", sAnt, nmeamsg.cFixFlag, NmeaGetRxRunMode(), nmeamsg.fLatInDegree, nmeamsg.fLonInDegree, nmeamsg.usYear, nmeamsg.ucMonth, nmeamsg.ucDay, nmeamsg.usHours, nmeamsg.usMinutes, nmeamsg.fSeconds); } #if 1 localtime_r(&timep, &p); p.tm_sec = (int)nmeamsg.fSeconds; p.tm_min = nmeamsg.usMinutes; p.tm_hour = nmeamsg.usHours+8; p.tm_mday = nmeamsg.ucDay; p.tm_mon = nmeamsg.ucMonth-1; p.tm_year = nmeamsg.usYear + 100; timep = mktime(&p); if(nmeamsg.cFixFlag == 'A') gps_buf[0] = 1; else gps_buf[0] = 0; gps_buf[1] = nmeamsg.fLonInDegree*1000000; gps_buf[2] = nmeamsg.fLatInDegree*1000000; gps_buf[3] = (int)timep; gps_buf[4] = (int)timep; gps_buf[5] = nmeamsg.fHeading; gps_buf[6] = nmeamsg.fGroundVel*1.85; if(nmeamsg.cLatIdx == 'S') gps_buf[7] = 1; else gps_buf[7] = 0; if(nmeamsg.cLonIdx == 'W') gps_buf[8] = 1; else gps_buf[8] = 0; gps_buf[9] = nmeamsg.fAltitude; gps_buf[10] = NmeaGetRxRunMode(); gps_buf[11] = nmeamsg.aSvInfo[1].nSvsUsed; gps_buf[12] = nmeamsg.aSvInfo[0].nSvsUsed; gps_buf[13] = sAnt; #if 0 printf("position=%d, flon=%d, flat=%d, %d, %d, iOrientation=%d, vel=%d, s=%d, w=%d, alt=%d, mode=%d, gps=%d, bd=%d\n", gps_buf[0], gps_buf[1],gps_buf[2],gps_buf[3], gps_buf[4],gps_buf[5], gps_buf[6],gps_buf[7], gps_buf[8],gps_buf[9], gps_buf[10], gps_buf[11], gps_buf[12]); #endif sendudp(1013, gps_buf, 4*14); sendudp(1015, gps_buf, 4*14); //busdemaon memset(gps_buf, 0 , 15); #endif memset(send_buf, 0, 1024); send_len = 0; } #endif } nmea_index++; // fprintf(outfile,"nmea_index = %d\n",nmea_index); if(nmea_index >= 1024) nmea_index = 0; } } } } if (please_die_now) break; if (FD_ISSET(frontfd[0], &readset)) { if ((n = read(frontfd[0], buff, BUFF_SIZE)) == 0) { if (listenfd) { if (close(frontfd[0]) < 0) errorf("Couldn't close old frontfd: %s\n",strerror(errno)); if ((frontfd[0]=frontfd[1]=accept(listenfd,NULL,NULL)) < 0) errorf("Couldn't accept new socket connection: %s\n",strerror(errno)); } } else if (n <= 0) { if ( (errno == EAGAIN) || (errno == EINTR) ) { /* No real error */ } else { errorf("Error reading from frontend device: %s\n",strerror(errno)); } } else { if (write (backfd[1], buff, n) != n) errorf("Error writing to backend device: %s\n",strerror(errno)); if (!quiet) dumpbuff(0,buff,n); } } } stty_orig(); exit(0); }