/****************************************************************************** * FunctionName : syslog_chk_wifi_stat * Description : check whether get ip addr or not * Parameters : none * Returns : none *******************************************************************************/ static void ICACHE_FLASH_ATTR syslog_chk_wifi_stat(void) { struct ip_info ipconfig; DBG("syslog_chk_wifi_stat: state: %d ", syslogState); //disarm timer first os_timer_disarm(&wifi_chk_timer); //try to get ip info of ESP8266 station wifi_get_ip_info(STATION_IF, &ipconfig); int wifi_status = wifi_station_get_connect_status(); if (wifi_status == STATION_GOT_IP && ipconfig.ip.addr != 0) { if (syslogState == SYSLOG_WAIT) { // waiting for initialization DBG("connected, initializing UDP socket\n"); syslog_init(flashConfig.syslog_host); } } else { if ((wifi_status == STATION_WRONG_PASSWORD || wifi_status == STATION_NO_AP_FOUND || wifi_status == STATION_CONNECT_FAIL)) { syslogState = SYSLOG_ERROR; os_printf("*** connect failure!!!\n"); } else { DBG("re-arming timer...\n"); os_timer_setfn(&wifi_chk_timer, (os_timer_func_t *)syslog_chk_wifi_stat, NULL); os_timer_arm(&wifi_chk_timer, WIFI_CHK_INTERVAL, 0); } } }
void log_init(int restart) { if (log_initialised) { if (!restart) return; if (strcmp(logfile_name, lp_log_file(module_id)) != 0) { if (logfile_fp) { fclose(logfile_fp); logfile_fp = NULL; } else closelog(); logfile_name = NULL; } else if (*logfile_name) return; /* unchanged, non-empty "log file" names */ else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id)) closelog(); else return; /* unchanged syslog settings */ } else log_initialised = 1; /* This looks pointless, but it is needed in order for the * C library on some systems to fetch the timezone info * before the chroot. */ timestring(time(NULL)); /* Optionally use a log file instead of syslog. (Non-daemon * rsyncs will have already set logfile_name, as needed.) */ if (am_daemon && !logfile_name) logfile_name = lp_log_file(module_id); if (logfile_name && *logfile_name) logfile_open(); else syslog_init(); }
static void logfile_open(void) { mode_t old_umask = umask(022 | orig_umask); logfile_fp = fopen(logfile_name, "a"); umask(old_umask); if (!logfile_fp) { int fopen_errno = errno; /* Rsync falls back to using syslog on failure. */ syslog_init(); rsyserr(FERROR, fopen_errno, "failed to open log-file %s", logfile_name); rprintf(FINFO, "Ignoring \"log file\" setting.\n"); } }
/* This is the C kernel entry point */ void kmain(struct multiboot *mboot_header, addr_t initial_stack) { /* Store passed values, and initiate some early things * We want serial log output as early as possible */ kernel_state_flags=0; mtboot = mboot_header; initial_boot_stack = initial_stack; loader_parse_kernel_elf(mboot_header, &kernel_sections); #if CONFIG_MODULES loader_init_kernel_symbols(); #endif serial_init(); cpu_early_init(); #if CONFIG_MODULES loader_init_modules(); #endif syscall_init(); fs_initrd_load(mtboot); cpu_timer_install(1000); cpu_processor_init_1(); /* Now get the management stuff going */ printk(1, "[kernel]: Starting system management\n"); mm_init(mtboot); syslog_init(); parse_kernel_command_line((char *)(addr_t)mtboot->cmdline); tm_init_multitasking(); dm_init(); fs_init(); net_init(); trace_init(); /* Load the rest... */ printk(KERN_MILE, "[kernel]: Kernel is setup (kv=%d, bpl=%d: ok)\n", CONFIG_VERSION_NUMBER, BITS_PER_LONG); printk(KERN_DEBUG, "[kernel]: structure sizes: process=%d bytes, thread=%d bytes, inode=%d bytes\n", sizeof(struct process), sizeof(struct thread), sizeof(struct inode)); cpu_interrupt_set(1); sys_setup(); cpu_processor_init_2(); timer_calibrate(); #if CONFIG_SMP if(boot_cpus) cpu_boot_all_aps(); #endif tm_clone(0, __init_entry, 0); sys_setsid(); kt_kernel_idle_task(); }
int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) { int f, t; if(!filename || !*filename || !strcmp(filename, "none")) filename = "/dev/null"; if(!strcmp(filename, "syslog")) { filename = "/dev/null"; syslog_init(); if(enabled_syslog) *enabled_syslog = 1; } else if(enabled_syslog) *enabled_syslog = 0; // don't do anything if the user is willing // to have the standard one if(!strcmp(filename, "system")) { if(fd != -1) return fd; filename = "stdout"; } if(!strcmp(filename, "stdout")) f = STDOUT_FILENO; else if(!strcmp(filename, "stderr")) f = STDERR_FILENO; else { f = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0664); if(f == -1) { error("Cannot open file '%s'. Leaving %d to its default.", filename, fd); return fd; } } // if there is a level-2 file pointer // flush it before switching the level-1 fds if(fp && *fp) fflush(*fp); if(fd != f && fd != -1) { // it automatically closes t = dup2(f, fd); if (t == -1) { error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename); close(f); return fd; } // info("dup2() new fd %d to old fd %d for '%s'", f, fd, filename); close(f); } else fd = f; if(fp && !*fp) { *fp = fdopen(fd, "a"); if (!*fp) error("Cannot fdopen() fd %d ('%s')", fd, filename); else { if (setvbuf(*fp, NULL, _IOLBF, 0) != 0) error("Cannot set line buffering on fd %d ('%s')", fd, filename); } } return fd; }
int main() { /* using NAME_MAX prevents EINVAL on read() (twice for UTF-16 on NTFS) */ char buf[sizeof(struct inotify_event) + NAME_MAX*2 + 1]; char *crtpath, *qpath, *rqpath, *looppath, *lsthost, *lstport; int sz, offset, rereg, evqok, retryid; struct inotify_event *iev; double retrytmout, lastclock; /* init logging */ syslog_init(); /* extract environment */ crtpath = alloc_env(CABLE_CERTS, "/" CERTS_NAME); qpath = alloc_env(CABLE_QUEUES, "/" QUEUE_NAME); rqpath = alloc_env(CABLE_QUEUES, "/" RQUEUE_NAME); looppath = alloc_env(CABLE_HOME, "/" LOOP_NAME); lsthost = alloc_env(CABLE_HOST, ""); lstport = alloc_env(CABLE_PORT, ""); /* initialize rng */ if (!rand_init()) warning("failed to initialize RNG"); /* initialize process accounting */ if (!init_process_acc()) warning("failed to initialize process accounting"); /* initialize webserver */ if (!init_server(crtpath, qpath, rqpath, lsthost, lstport)) { flog(LOG_ERR, "failed to initialize webserver"); return EXIT_FAILURE; } /* try to reregister watches as long as no signal caught */ for (lastclock = getmontime(), retryid = 0; !stop_requested(); ) { /* support empty CABLE_NOLOOP when testing, to act as pure server */ #ifdef TESTING if (getenv("CABLE_NOLOOP")) { sleepsec(RETRY_TMOUT); continue; } #endif wait_reg_watches(qpath, rqpath); /* read events as long as no signal caught and no unmount / move_self / etc. events read */ for (rereg = evqok = 0; !stop_requested() && !rereg; ) { /* wait for an event, or timeout (later blocking read() results in error) */ retrytmout = RETRY_TMOUT + RETRY_TMOUT * (rand_shift() / 2); if (wait_read(inotfd, retrytmout - (getmontime() - lastclock))) { /* read events (non-blocking), taking care to handle interrupts due to signals */ if ((sz = read(inotfd, buf, sizeof(buf))) == -1 && errno != EINTR) { /* happens buffer is too small (e.g., NTFS + 255 unicode chars) */ warning("error while reading from inotify queue"); rereg = 1; } /* process all events in buffer, sz = -1 and 0 are automatically ignored */ for (offset = 0; offset < sz && !stop_requested() && !rereg; evqok = 1) { /* get handler to next event in read buffer, and update offset */ iev = (struct inotify_event*) (buf + offset); offset += sizeof(struct inotify_event) + iev->len; /* IN_IGNORED is triggered by watched directory removal / fs unmount IN_MOVE_SELF is only triggered by move of actual watched directory (i.e., not its parent) */ if ((iev->mask & (IN_IGNORED | IN_UNMOUNT | IN_Q_OVERFLOW | IN_MOVE_SELF))) rereg = 1; /* ignore non-subdirectory events, and events with incorrect name */ else if (iev->len > 0 && (iev->mask & IN_ISDIR) && is_msgdir(iev->name)) { assert(iev->wd == inotqwd || iev->wd == inotrqwd); if (iev->wd == inotqwd || iev->wd == inotrqwd) { /* stop can be indicated here (while waiting for less processes) */ const char *qtype = (iev->wd == inotqwd) ? QUEUE_NAME : RQUEUE_NAME; run_loop(qtype, iev->name, looppath); } else flog(LOG_WARNING, "unknown watch descriptor"); } } } /* if sufficient time passed since last retries, retry again */ if (!stop_requested() && getmontime() - lastclock >= retrytmout) { /* alternate between queue dirs to prevent lock starvation on self-send */ if ((retryid ^= 1)) retry_dir(QUEUE_NAME, qpath, looppath); else retry_dir(RQUEUE_NAME, rqpath, looppath); lastclock = getmontime(); /* inotify is apparently unreliable on fuse, so reregister when no events */ if (!evqok) rereg = 1; evqok = 0; } } } unreg_watches(); if (!shutdown_server()) flog(LOG_WARNING, "failed to shutdown webserver"); dealloc_env(lstport); dealloc_env(lsthost); dealloc_env(looppath); dealloc_env(rqpath); dealloc_env(qpath); dealloc_env(crtpath); flog(LOG_INFO, "exiting"); closelog(); return EXIT_SUCCESS; }
/* locks are automatically released on program exit, so don't bother with unlocking on exceptions */ int main(int argc, char *argv[]) { const char *mhdir; struct dirent *de = NULL; DIR *dir; int dfd, i, spret; num_t maxidx = 0, curidx; char numname[NUM_LEN+1]; if (argc < 3) { fprintf(stderr, "%s <mh dir> <message on same fs> ...\n", argv[0]); return 1; } mhdir = argv[1]; /* init logging */ syslog_init(); /* signals */ set_signals(); /* open directory */ if ((dir = opendir(mhdir)) == NULL) error(); /* get corresponding file descriptor for lock / access */ if ((dfd = dirfd(dir)) == -1) error(); /* lock directory with timeout */ alarm(LOCK_TMOUT); if (flock(dfd, LOCK_EX) == -1) { if (errno == EINTR && alrm) flogexit(LOG_ERR, "timed out while locking %s", mhdir); else error(); } alarm(0); /* find max entry, don't bother with file types */ for (errno = 0; (de = readdir(dir)) != NULL; ) if ((curidx = getidx(de->d_name)) != NOT_NUM && curidx > maxidx) maxidx = curidx; if (de == NULL && errno) error(); /* deliver messages */ for (i = 2, ++maxidx; i < argc; ++i, ++maxidx) { if (maxidx == NOT_NUM || maxidx == 0) flogexit(LOG_ERR, "indexes exhausted, %llu not legal", maxidx); /* convert to string, but also check back due to possible locale-related problems */ spret = snprintf(numname, sizeof(numname), "%llu", maxidx); if (spret < 0 || spret >= sizeof(numname) || getidx(numname) != maxidx) flogexit(LOG_ERR, "could not convert %llu to file name", maxidx); /* rename() may replace an externally created file, so use link/unlink */ if (linkat(AT_FDCWD, argv[i], dfd, numname, 0) == -1) error(); if (unlink(argv[i]) == -1) error(); flog(LOG_INFO, "delivered %s/%s", mhdir, numname); } /* unlock (explicitly) */ if (flock(dfd, LOCK_UN) == -1) error(); /* close directory */ if (closedir(dir) == -1) error(); closelog(); return 0; }
void log_file_init(logd_file * plf) { char fullfilename[256]; char mylink[256]; const char *symfilename; char date_str[256]; char gmt_str[256]; char cmd_str[256]; int ret; int round_off = 0; syslog_init ( plf ); aln_interval *pal = NULL; time_t et; struct tm etime; pal =(aln_interval *)malloc(sizeof(aln_interval)); if(pal == NULL){ complain_error_msg(1, "Memory allocation failed!"); exit(EXIT_FAILURE); } memset(pal ,0,sizeof(aln_interval)); plf->pai = pal; /* Get current time */ time_t cur_time = time(NULL); struct tm loctime ; struct tm gtime; localtime_r(&cur_time, &loctime); gmtime_r(&cur_time, >ime); plf->st_time = (uint64_t)cur_time; strftime(plf->hour, 3, "%H", &loctime); // modify the date_str to include the file_start_min if(pal->use_file_time == 1){ loctime.tm_min = pal->file_start_min; pal->use_file_time = 0; } if((loctime.tm_min )&&(plf->rt_interval)){ round_off = loctime.tm_min%plf->rt_interval; if(round_off){ loctime.tm_min = abs(loctime.tm_min - round_off); loctime.tm_sec = 0; } } strftime(plf->file_start_time,SIZE_OF_TIMEBUFFER, "%G%m%d%H%M", &loctime); if(plf->rt_interval){ et = mktime(&loctime); et = et + (60 * plf->rt_interval); localtime_r(&et,&etime); strftime(plf->file_end_time, SIZE_OF_TIMEBUFFER, "%G%m%d%H%M", &etime); } else { memset(plf->file_end_time,'\0',sizeof(plf->file_end_time)); } /* adjust the fileid */ plf->cur_fileid ++; if(plf->log_rotation){//if log-rotation is enabled, then we need to increament the file-id if(plf->cur_fileid >= plf->max_fileid) plf->cur_fileid = 0; log_save_fileid(); } if (plf->type != TYPE_SYSLOG) {//no log file is created for syslog if(plf->log_rotation){ snprintf(cmd_str, sizeof(cmd_str), "%s %s/%s.%d.%s", "rm -f", log_folderpath, plf->filename, plf->cur_fileid, "*"); //printf("----cmd_str= %s\n", cmd_str); system(cmd_str); } strftime(date_str, 256, "%Y%m%d_%H:%M:%S", &loctime); snprintf(fullfilename, sizeof(fullfilename), "%s/%s.%d.%s", log_folderpath, plf->filename, plf->cur_fileid, date_str); if(plf->fullfilename) { free(plf->fullfilename); } plf->fullfilename = strdup(fullfilename); plf->fp = fopen(fullfilename, "w"); if(!plf->fp) { complain_error_errno(1, "Opening %s", fullfilename); } if (plf->type == TYPE_ACCESSLOG) { plf->io_buf = (char *) calloc(IOBUF_SIZE, 1); setbuffer(plf->fp, plf->io_buf, IOBUF_SIZE); } plf->cur_filesize = 0; } symfilename = plf->filename; // default: "access.log" if((plf->fp) && (plf->type != TYPE_SYSLOG)){ ret = fprintf(plf->fp, "#Version: 1.0\n"); plf->cur_filesize += ret; ret = fprintf(plf->fp, "#Software: Media Flow Controller v(%s)\n", NKN_BUILD_PROD_RELEASE); plf->cur_filesize += ret; strftime(gmt_str, 256, "%Y-%m-%d %T", >ime); ret = fprintf(plf->fp, "#Date: %s\n", gmt_str); plf->cur_filesize += ret; ret = fprintf(plf->fp, "#Remarks: -\n"); plf->cur_filesize += ret; } switch(plf->type) { case TYPE_ACCESSLOG: symfilename = plf->filename; // default: "access.log" if(plf->show_format){ ret = fprintf(plf->fp, "#Version: 1.0\n"); plf->cur_filesize += ret; strftime(date_str, 256, "%m-%d-%Y %H:%M:%S", &loctime); ret = fprintf(plf->fp, "#Date: %s\n", date_str); plf->cur_filesize += ret; ret = fprintf(plf->fp, "#Fields: %s\n", plf->format); plf->cur_filesize += ret; fflush(plf->fp); } break; case TYPE_TRACELOG: symfilename = plf->filename; break; //default: trace.log case TYPE_CACHELOG: symfilename = plf->filename; break; // default : "cache.log" case TYPE_FUSELOG: symfilename = plf->filename; break; // default : "fuse.log" case TYPE_STREAMLOG: symfilename = plf->filename; // default : "stream.log" if(plf->show_format){ /* ret = fprintf(plf->fp, "#Version: 1.0\n"); plf->cur_filesize += ret; strftime(date_str, 256, "%m-%d-%Y %H:%M:%S", loctime); ret = fprintf(plf->fp, "#Date: %s\n", date_str); plf->cur_filesize += ret;*/ ret = fprintf(plf->fp, "#Fields: %s\n", plf->format); plf->cur_filesize += ret; fflush(plf->fp); } break; case TYPE_SYSLOG: break; //We don't create a file for syslog case TYPE_ERRORLOG: symfilename = plf->filename; break; // default : "error.log" case TYPE_MFPLOG: symfilename = plf->filename; break; // default : "mfp.log" case TYPE_CRAWLLOG: symfilename = plf->filename; crawl_write_version(plf); break; // default : "crawl.log" case TYPE_CBLOG: symfilename = plf->filename; break; // default : "cb.log" default: symfilename = "other.log"; break; } /* Create a symbol link for web GUI */ if (plf->type != TYPE_SYSLOG) { snprintf(mylink, sizeof(mylink), "ln -fs %s %s/%s", fullfilename, log_folderpath, symfilename); system(mylink); } return ; }
void netmain_init(void) { int e; int i; char * msg; #ifdef IP_V6 ip6_addr host; #endif printf("\n%s\n", name); printf("Copyright 1997-2006 by InterNiche Technologies. All rights reserved. \n"); #ifndef SUPERLOOP /* call this to do pre-task setup including intialization of port_prep */ msg = pre_task_setup(); if (msg) panic(msg); #endif #ifdef INCLUDE_NVPARMS /* system uses InterNiche NV system */ e = get_nv_params(); /* get flash parameters into data structs */ if (e) { printf("fatal error (%d) reading NV parameters.\n", e); panic("nv"); } /* set static iface IP info up from stored parameters. These may be overwritten from command line parms or DHCP later. */ for (i = 0; i < STATIC_NETS; i++) { netstatic[i].n_ipaddr = inet_nvparms.ifs[i].ipaddr; netstatic[i].snmask = inet_nvparms.ifs[i].subnet; netstatic[i].n_defgw = inet_nvparms.ifs[i].gateway; #ifdef IP_MULTICAST /* Create a dummy entry for the Ethernet interface mcastlist */ /* If this entry is set to NULL, multicast is not supported */ /* on this interface */ netstatic[i].n_mcastlist = mcastlist; #endif /* IP_MULTICAST */ #ifdef IP_V6 IP6CPY(&host, &inet_nvparms.ifs[i].ipv6addr); if ( (host.addr[0] == 0xFE) && (host.addr[1] == 0xC0)) { netstatic[i].v6addrs[IPA_SITE] = ip6_mkaddr(&netstatic[i], IPA_SITE, &host); } else if ( (host.addr[0] == 0xFE) && (host.addr[1] == 0x80) ) { printf ("[IPV6 init]error : bad IPV6 address\n"); } else if (host.addr[0] != 0) { netstatic[i].v6addrs[IPA_GLOBAL] = ip6_mkaddr(&netstatic[i], IPA_GLOBAL, &host ); } #endif } #ifdef DNS_CLIENT /* set DNS client's server list from nvparms information */ MEMCPY(dns_servers, inet_nvparms.dns_servers, sizeof(dns_servers)); #ifdef DNS_CLIENT_UPDT MEMCPY(soa_mname, inet_nvparms.dns_zone_name, sizeof(soa_mname)); #endif /* DNS_CLIENT_UPDT */ #endif /* DNS_CLIENT */ #ifdef USE_COMPORT comportcfg.comport = comport_nvparms.comport; comportcfg.LineProtocol = comport_nvparms.LineProtocol; #endif /* USE_COMPORT */ #endif /* INCLUDE_NVPARMS */ #ifndef INCLUDE_NVPARMS #ifdef USE_COMPORT comportcfg.comport = 0x01; comportcfg.LineProtocol = PPP; /* Default to PPP */ #endif /* USE_COMPORT */ #endif /* INCLUDE_NVPARMS */ msg = ip_startup(); if (msg) { printf("inet startup error: %s\n", msg); panic("IP"); } #if defined(MEMDEV_SIZE) && defined(VFS_FILES) init_memdev(); /* init the mem and null test devices */ #endif #ifdef IP_MULTICAST #ifdef INCLUDE_TCP /* call the IP multicast test program */ u_mctest_init(); #endif #endif /* clear debugging flags. Port can optionally turn them * back on in post_task_setup(); * NDEBUG = UPCTRACE | IPTRACE | TPTRACE ; */ NDEBUG = 0; /* print IP address of the first interface - for user's benefit */ printf("IP address of %s : %s\n" , ((NET)(netlist.q_head))->name, print_ipad(((NET)(netlist.q_head))->n_ipaddr)); #ifndef SUPERLOOP /* call this per-target routine after basic tasks & net are up */ msg = post_task_setup(); if (msg) panic(msg); #endif #ifdef PING_APP ping_init(); #endif /* PING_APP */ #ifdef RAWIPTEST raw_test_init(); #endif /* RAWIPTEST */ #if defined(TFTP_CLIENT) || defined(TFTP_SERVER) tftp_init(); #endif /* TFTP */ #ifdef TESTMENU install_menu(testmenu); #endif /* TESTMENU */ #ifdef USE_AUTOIP Upnp_init(); /* start Auto IP before DHCP client */ #endif /* USE_AUTOIP */ #ifdef DHCP_CLIENT if( POWERUP_CONFIG_DHCP_ENABLED ) dhc_setup(); /* kick off any DHCP clients */ #endif /* DHCP_CLIENT */ #ifdef DHCP_SERVER #ifdef INCLUDE_NVPARMS if(dhserve_nvparms.ServeDHCP) #endif { e = dhcp_init(); if(e) { dprintf("Error %d starting DHCP server.\n",e); } else { exit_hook(dhcpsrv_cleanup); dprintf("Started DHCP server\n"); } } #endif /* DHCP_SERVER */ #ifdef IN_MENUS printf(prompt); #endif #ifdef UDPSTEST e=udp_echo_init(); if ( e == SUCCESS ) { exit_hook(udp_echo_cleanup); } else dprintf("Error %d starting UDP Echo server.\n",e); #endif #ifdef RIP_SUPPORT e=rip_init(); if ( e == SUCCESS ) { exit_hook(rip_cleanup); } else dprintf("Error %d starting RIP server.\n",e); #endif #ifdef INICHE_SYSLOG e =syslog_init(); if (e == SUCCESS) exit_hook(closelog); else dprintf("Error %d initializing syslog client.\n",e); #endif #ifdef FTP_CLIENT fc_callback=ftpc_callback; #endif /* The following initializations take place when SUPERLOOP is enabled. * Otherwise they would be done in the respective task. */ #ifdef SUPERLOOP #ifdef INCLUDE_SNMP e = snmp_init(); if (e == SUCCESS) exit_hook(snmp_cleanup); else dprintf("Error %d initializing SNMP agent.\n",e); #endif /* INCLUDE_SNMP */ #ifdef WEBPORT e = http_init(); /* start up http server */ if (e) dprintf("Error %d starting HTTP server.\n",e); #endif /* WEBPORT */ #ifdef FTP_SERVER e = ftps_init(); if ( e == SUCCESS ) { exit_hook(ftps_cleanup); } else dprintf("Error %d starting FTP server.\n",e); #endif /* FTP_SERVER */ #ifdef TELNET_SVR e=tel_init(); if ( e == SUCCESS ) { exit_hook(tel_cleanup); } else dprintf("Error %d starting TELNET server.\n",e); #endif #ifdef TCP_ECHOTEST e=tcp_echo_init(); if ( e == SUCCESS ) { exit_hook(tcp_echo_cleanup); } else dprintf("Error %d starting TCP Echo server.\n",e); #endif #ifdef TCP_CIPHERTEST e=tcp_cipher_init(); if ( e == SUCCESS ) { exit_hook(tcp_cipher_cleanup); } else dprintf("Error %d starting TCP cipher server.\n",e); #endif #ifdef USE_CRYPTOENG e = ce_init(); if(e != 0) { dprintf("ce_init() failed\n"); panic("prep_modules"); } #endif #ifdef SMTP_ALERTS smtp_init (); #endif #endif /* SUPERLOOP */ USE_ARG(e); /* Avoid compiler warnings */ USE_ARG(i); } /* end of netmain_init() */
/****************************************************************************** * FunctionName : syslog_chk_status * Description : check whether get ip addr or not * Parameters : none * Returns : none *******************************************************************************/ static void ICACHE_FLASH_ATTR syslog_chk_status(void) { struct ip_info ipconfig; DBG("[%uµs] %s: id=%lu ", WDEV_NOW(), __FUNCTION__, syslogQueue ? syslogQueue->msgid : 0); //disarm timer first syslog_timer_armed = false; //try to get ip info of ESP8266 station wifi_get_ip_info(STATION_IF, &ipconfig); int wifi_status = wifi_station_get_connect_status(); if (wifi_status == STATION_GOT_IP && ipconfig.ip.addr != 0) { // it seems we have to add an additional delay after the Wifi is up and running. // so we simply add an intermediate state with 25ms delay switch (syslogState) { case SYSLOG_WAIT: DBG("%s: Wifi connected\n", syslog_get_status()); syslog_set_status(SYSLOG_INIT); syslog_timer_arm(100); break; case SYSLOG_INIT: DBG("%s: init syslog\n", syslog_get_status()); syslog_set_status(SYSLOG_INITDONE); syslog_init(flashConfig.syslog_host); syslog_timer_arm(10); break; case SYSLOG_DNSWAIT: DBG("%s: wait for DNS resolver\n", syslog_get_status()); syslog_timer_arm(100); break; case SYSLOG_READY: DBG("%s: enforce sending\n", syslog_get_status()); syslog_send_udp(); break; case SYSLOG_SENDING: DBG("%s: delay\n", syslog_get_status()); syslog_set_status(SYSLOG_SEND); syslog_timer_arm(2); break; case SYSLOG_SEND: DBG("%s: start sending\n", syslog_get_status()); syslog_send_udp(); break; default: DBG("%s: %d\n", syslog_get_status(), syslogState); break; } } else { if ((wifi_status == STATION_WRONG_PASSWORD || wifi_status == STATION_NO_AP_FOUND || wifi_status == STATION_CONNECT_FAIL)) { syslog_set_status(SYSLOG_ERROR); os_printf("*** connect failure %d!!!\n", wifi_status); } else { DBG("re-arming timer...\n"); syslog_timer_arm(WIFI_CHK_INTERVAL); } } }
/* * static void * setup(int argc, char **argv) * Setup monitor environment. */ static void setup(int argc, char **argv) { pfc_cmdopt_t *parser; pfc_refptr_t *defconf, *dname, *defmconf, *defpfile; const char *conffile; const char *moncf; const char *pidpath; monitor_ctx_t *mp = &monitor_ctx; pfc_log_level_t lvl = PFC_LOGLVL_NONE; int argidx, do_kill = 0; uint32_t ktout = KILL_TIMEOUT; char c; /* Use C locale. */ (void)setlocale(LC_ALL, "C"); /* Set timezone. */ tzset(); if (PFC_EXPECT_TRUE(argc > 0)) { /* Initialize program name. */ dname = progname_init(*argv, &progname); if (PFC_EXPECT_FALSE(dname == NULL)) { fatal("Failed to create daemon name."); /* NOTREACHED */ } daemon_name = pfc_refptr_string_value(dname); daemon_rname = dname; } else { dname = NULL; } /* Construct default file paths. */ defconf = pfc_refptr_sprintf(PFC_SYSCONFDIR "/%s.conf", daemon_name); if (PFC_EXPECT_FALSE(defconf == NULL)) { fatal("Failed to create default configuration file path."); /* NOTREACHED */ } defmconf = pfc_refptr_sprintf(PFC_SYSCONFDIR "/%s.conf", progname); if (PFC_EXPECT_FALSE(defmconf == NULL)) { fatal("Failed to create default monitor configuration file " "path."); /* NOTREACHED */ } defpfile = pfc_refptr_sprintf(PFC_RUNDIR_PATH "/%s.pid", progname); if (PFC_EXPECT_FALSE(defpfile == NULL)) { fatal("Failed to create default PID file path."); /* NOTREACHED */ } conffile = pfc_refptr_string_value(defconf); moncf = pfc_refptr_string_value(defmconf); pidpath = pfc_refptr_string_value(defpfile); copt_desc_update(conffile); mcopt_desc_update(moncf); popt_desc_update(pidpath); /* Create command line option parser. */ parser = pfc_cmdopt_init(progname, argc, argv, option_spec, NULL, 0); if (PFC_EXPECT_FALSE(parser == NULL)) { fatal("Failed to create option parser."); /* NOTREACHED */ } mp->mc_syslog = PFC_FALSE; while ((c = pfc_cmdopt_next(parser)) != PFC_CMDOPT_EOF) { switch (c) { case 'C': conffile = pfc_cmdopt_arg_string(parser); if (PFC_EXPECT_FALSE(*conffile == '\0')) { fatal("Configuration file path is empty."); /* NOTREACHED */ } break; case 'c': moncf = pfc_cmdopt_arg_string(parser); if (PFC_EXPECT_FALSE(*moncf == '\0')) { fatal("Monitor configuration file path is " "empty."); /* NOTREACHED */ } break; case 'P': pidpath = pfc_cmdopt_arg_string(parser); if (PFC_EXPECT_FALSE(*pidpath == '\0')) { fatal("Monitor PID file path is empty."); /* NOTREACHED */ } break; case 's': mp->mc_syslog = PFC_TRUE; break; case 'd': /* Run as debug mode. */ monitor_debug++; lvl = (lvl == PFC_LOGLVL_NONE) ? PFC_LOGLVL_DEBUG : PFC_LOGLVL_VERBOSE; break; case 'K': ktout = pfc_cmdopt_arg_uint32(parser); if (PFC_EXPECT_FALSE(ktout < KILL_TIMEOUT_MIN)) { fatal("kill-timeout must be greater than or " "equal %u.", KILL_TIMEOUT_MIN); /* NOTREACHED */ } else if (PFC_EXPECT_FALSE(ktout > KILL_TIMEOUT_MAX)) { fatal("kill-timeout must be less than or " "equal %u.", KILL_TIMEOUT_MAX); /* NOTREACHED */ } /* FALLTHROUGH */ case 'k': do_kill = 1; break; case '\v': dump_version(); /* NOTREACHED */ case PFC_CMDOPT_USAGE: pfc_cmdopt_usage(parser, stdout); exit(MON_EX_OK); /* NOTREACHED */ case PFC_CMDOPT_HELP: pfc_cmdopt_help_list(parser, stdout, HELP_MESSAGE_1, daemon_name, HELP_MESSAGE_2, daemon_name, HELP_MESSAGE_3, NULL); exit(MON_EX_OK); /* NOTREACHED */ case PFC_CMDOPT_ERROR: exit(MON_EX_FATAL); /* NOTREACHED */ default: fatal("Failed to parse command line options."); /* NOTREACHED */ } } if ((argidx = pfc_cmdopt_validate(parser)) == -1) { fatal("Invalid command line options."); /* NOTREACHED */ } argc -= argidx; argv += argidx; if (PFC_EXPECT_FALSE(argc != 0)) { pfc_cmdopt_usage(parser, stderr); exit(MON_EX_FATAL); /* NOTREACHED */ } pfc_cmdopt_destroy(parser); /* Close all inherited file descriptors. */ pfc_closefrom(3); /* * Initialize the logging system to dump early logs to the standard * error output. */ pfc_log_init(progname, stderr, lvl, fatal_die); if (do_kill) { /* Kill the monitor process. */ stop_monitor(pidpath, ktout); exit(MON_EX_OK); /* NOTREACHED */ } mp->mc_conf = PFC_CONF_INVALID; mp->mc_cfpath = pfc_refptr_string_create(moncf); pfc_refptr_put(defmconf); if (PFC_EXPECT_FALSE(mp->mc_cfpath == NULL)) { fatal("Failed to create monitor configuration file path."); /* NOTREACHED */ } mp->mc_mon_pidfile = pfc_refptr_string_create(pidpath); pfc_refptr_put(defpfile); if (PFC_EXPECT_FALSE(mp->mc_mon_pidfile == NULL)) { fatal("Failed to create PID file path."); /* NOTREACHED */ } /* Load system configuration file. */ sysconf_init(mp, conffile); pfc_refptr_put(defconf); /* Initialize syslog if non-debug mode. */ if (monitor_debug) { mp->mc_syslog = PFC_FALSE; } else if (mp->mc_syslog) { syslog_init(); } }