int main(int argc, char *argv[]) { struct sockaddr_un addr; signal(SIGINT, handle_sigint); signal(SIGTERM, handle_sigterm); signal(SIGQUIT, handle_sigquit); if (options(argc, argv) < 0) { usage(); goto failure; } if (flags & HELP) { usage(); goto success; } if (flags & DAEMON) if (daemon(1, 1) < 0) goto failure; if (db_start() < 0) goto failure; if ((control_sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) goto failure; addr.sun_family = AF_UNIX; strncpy(addr.sun_path, CONTROL_SOCKET_PATH, UNIX_PATH_MAX); if (bind(control_sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) goto failure; if (handle_messages() < 0) goto failure; close(control_sock); unlink(CONTROL_SOCKET_PATH); if (db_end() < 0) goto failure; return 0; failure: if (control_sock != -1) { close(control_sock); unlink(CONTROL_SOCKET_PATH); } return 1; success: return 0; }
int main (int argc, const char * argv[]) { // send_image_file(0,"/home/tube/Downloads/c1130-rcvk9w8-tar.124-25e.JAP.tar"); cw_log_name="AC-Tube"; read_config("ac.conf"); cw_log_debug_level=conf_debug_level; cw_log(LOG_INFO,"Starting AC-Tube, Name=%s, ID=%s",conf_acname,conf_acid); // cw_dbg_opt_level= DBG_CW_MSGELEM_DMP | // DBG_CW_MSGELEM | DBG_CW_PKT| DBG_CW_RFC | DBG_ERR | DBG_CW_MSG | DBG_DTLS ; //| DBG_ALL; cw_dbg_opt_detail=DBG_DETAIL_ASC_DMP; // cw_log_dbg(DBG_CW_MSG,"Hello %s","World"); db_init(); db_start(); db_ping(); pthread_t alth; pthread_create (&alth, NULL, alive_thread, (void *)0); #ifdef WITH_DTLS dtls_init(); #endif if (!socklist_init()) goto errX; if (!wtplist_init()) goto errX; int rc = ac_run(); errX: wtplist_destroy(); socklist_destroy(); return rc; }
int main(int argc, char*argv[]) { int i2c_device; MYSQL *inst; MYSQL_STMT *stmt; station_state_t station_state[256]; reading_t *sensor_state = NULL; struct sigaction sigact; int i; openlog("sensord", 0, LOG_LOCAL1); if ((i2c_device = open(I2C_DEVICE, O_RDWR)) < 0) { fprintf(stderr, "Failed to open %s: %s\n", I2C_DEVICE, strerror(errno)); return 1; } if (ioctl(i2c_device, I2C_SLAVE, (long)I2C_SLAVE_ADDRESS) < 0) { fprintf(stderr, "ioctl(I2C_SLAVE) failed: %s\n", strerror(errno)); close(i2c_device); return 1; } if (db_start(&inst, &stmt) != 0) { fprintf(stderr, "Database initialisation failed\n"); return 1; } /* * Set up signal handling: * ignore HUP * terminate on INT, TERM */ sigemptyset(&sigact.sa_mask); sigact.sa_flags = 0; sigact.sa_handler = SIG_IGN; sigaction(SIGHUP, &sigact, NULL); sigact.sa_handler = set_shutdown_flag; sigaction(SIGINT, &sigact, NULL); sigaction(SIGTERM, &sigact, NULL); memset(station_state, 0, sizeof(station_state)); syslog(LOG_INFO, "started; entering event loop"); /* * Detach ourselves from the controlling tty */ daemon(0, 0); /* * Main event loop */ while (!Shutdown) { char i2c_message[256]; int n; /* * Read current state from the sensor receiver */ if ((n = read(i2c_device, i2c_message, sizeof(i2c_message))) < 0) { fprintf(stderr, "message read failed: %s\n", strerror(errno)); return 1; } if (!process_message(i2c_message, n, station_state, &sensor_state, stmt)) { fprintf(stderr, "message process failed\n"); return 1; } /* * Sensors send messages every 64 seconds, so this will ensure we don't miss * any updates. */ for (i = 0; !Shutdown && i < 45; ++i) sleep(1); } db_end(inst, stmt); close(i2c_device); syslog(LOG_INFO, "terminating"); closelog(); return 0; }