// main int main(int argc, char *argv[]) { // arg analysis invocation_name = argv[0]; bool bdeamon = false; const Arg_parser::Option options[] = { { 'v', "version", Arg_parser::no }, { 'h', "help", Arg_parser::no }, { 256, "deamon", Arg_parser::no }}; const Arg_parser parser( argc, argv, options ); if( parser.error().size() ) { show_error( parser.error().c_str(), 0, true ); return BK_ERROR; } for( int argind = 0; argind < parser.arguments(); ++argind ) { const int code = parser.code( argind ); switch( code ) { case 'v': print_version(); return 0; case 'h': show_help(); return 0; case 256: bdeamon = true; break; default : internal_error( "uncaught option" ); } } if(bdeamon) { setdeamon(); string db, ip, user, psw; get_dbconfig(db, ip, user, psw); if (backup_init(db.c_str(), ip.c_str(), user.c_str(), psw.c_str()) != 0) //if (backup_init("backup", "127.0.0.1", "root", "root") != 0) { cout << "init backupd error "<< endl; return 0; } backup_start(); while(_running) sleep(10); } else { Gtk::Main kit(argc, argv); BackupMain window; //Shows the window and returns when it is closed. Gtk::Main::run(window); backup_stop(); } return 0; }
int vote_ready(Conn *conn, char *buf, int datalen) { short retcode; uint8_t cmd; int count = sizeof(int); char ip[INET_ADDRSTRLEN]; uint16_t port; uint64_t voteid; memcpy(&cmd, buf+count, sizeof(cmd)); count += sizeof(cmd); if (g_cf->sync_mode == MODE_MASTER_SLAVE) { conn_send_buffer_reply(conn, MEMLINK_ERR_SLAVE, NULL, 0); return 0; } switch (cmd) { case CMD_VOTE: memcpy(&retcode, buf+count, sizeof(retcode)); count += sizeof(retcode); switch (retcode) { case CMD_VOTE_WAIT: DINFO("*** VOTE WAITING\n"); int timeout = 10*g_cf->timeout; if (timeout < 60) { timeout = 60; } else if (timeout > 300) { timeout = 300; } //*((int *)conn->thread) = VOTE_WAITING; conn->vote_status = VOTE_WAITING; event_del(&conn->evt); change_event(conn, EV_READ|EV_PERSIST, timeout, 1); break; case CMD_VOTE_MASTER: DINFO("*** VOTE MASTER\n"); /* if (conn->thread != g_runtime->wthread) *((int *)conn->thread) = VOTE_OK; */ conn->vote_status = VOTE_OK; count += unpack_voteid(buf+count, &voteid); if (g_cf->role == ROLE_NONE) { DINFO("*** master init\n"); master_init(voteid, buf+count, datalen-count); g_cf->role = ROLE_MASTER; g_runtime->voteid = voteid; } else { DINFO("*** switch to master\n"); if (switch_master(voteid, buf+count, datalen-count) < 0) { DERROR("*** FATAL ERROR : switch to master error\n"); g_cf->role = ROLE_NONE; g_runtime->voteid = 0; } else { g_cf->role = ROLE_MASTER; g_runtime->voteid = voteid; } } DINFO("*** voteid: %llu\n", (unsigned long long)g_runtime->voteid); DINFO("*** VOTE: I am master\n"); while (count < datalen) { count += unpack_votehost(buf+count, ip, &port); DINFO("*** VOTE backup: %s(%d)\n", ip, port); } /* Host **hostp = &g_runtime->hosts; while (count < datalen) { count += unpack_votehost(buf+count, ip, &port); DINFO("*** VOTE backup: %s(%d)\n", ip, port); if (*hostp == NULL) { *hostp = (Host *)zz_malloc(sizeof(Host)); strcpy((*hostp)->ip, ip); (*hostp)->port = port; (*hostp)->next = NULL; hostp = &(*hostp)->next; } else { strcpy((*hostp)->ip, ip); (*hostp)->port = port; hostp = &(*hostp)->next; } } if (*hostp != NULL) { release_hosts(*hostp); *hostp = NULL; } */ conn_send_buffer_reply(conn, MEMLINK_OK, NULL, 0); break; case CMD_VOTE_BACKUP: DINFO("*** VOTE BACKUP\n"); /* if (conn->thread != g_runtime->wthread) *((int *)conn->thread) = VOTE_OK; */ conn->vote_status = VOTE_OK; count += unpack_voteid(buf+count, &voteid); unpack_votehost(buf+count, ip, &port); if (g_cf->role == ROLE_NONE) { DINFO("*** backup init\n"); backup_init(voteid, buf+count, datalen-count); g_cf->role = ROLE_BACKUP; g_runtime->voteid = voteid; } else { DINFO("*** switch to backup\n"); if (switch_backup(voteid, buf+count, datalen-count) < 0) { DERROR("*** FATAL ERROR : switch to backup error\n"); g_cf->role = ROLE_NONE; g_runtime->voteid = 0; } else { g_cf->role = ROLE_BACKUP; g_runtime->voteid = voteid; } } DINFO("*** VOTE: I am backup, master is %s(%d)\n", ip, port); DINFO("*** voteid: %llu\n", (unsigned long long)g_runtime->voteid); /* Host *host; if (g_runtime->hosts == NULL) { host = (Host *)zz_malloc(sizeof(Host)); if (host == NULL) { DERROR("*** VOTE: zz_malloc error\n"); MEMLINK_EXIT; } host->next = NULL; strcpy(host->ip, ip); host->port = port; g_runtime->hosts = host; } else { host = g_runtime->hosts; strcpy(host->ip, ip); host->port = port; release_hosts(host->next); host->next = NULL; } */ conn_send_buffer_reply(conn, MEMLINK_OK, NULL, 0); break; case CMD_VOTE_NONEED: DINFO("*** VOTE NONEED\n"); if (g_cf->role == ROLE_NONE) { //*((int *)conn->thread) = VOTE_CONTINUE; conn->vote_status = VOTE_CONTINUE; break; } //*((int *)conn->thread) = VOTE_OK; conn->vote_status = VOTE_OK; backup_create_heartbeat(g_runtime->wthread); break; case MEMLINK_ERR_VOTE_PARAM: DERROR("*** VOTE PARAM ERROR\n"); //*((int *)conn->thread) = VOTE_ERR_PARAM; conn->vote_status = VOTE_ERR_PARAM; break; default: DERROR("*** VOTE UNKNOWN ERROR\n"); //if (conn->thread != g_runtime->wthread) // *((int *)conn->thread) = VOTE_OK; conn->vote_status = VOTE_OK; break; } break; case CMD_VOTE_UPDATE: DINFO("*** VOTE UPDATING\n"); if (g_cf->role != ROLE_MASTER) { conn_send_buffer_reply(conn, MEMLINK_ERR_NOT_MASTER, NULL, 0); break; } //DINFO("*** VOTE MASTER\n"); /* if (conn->thread != g_runtime->wthread) *((int *)conn->thread) = VOTE_OK; */ //conn->vote_status = VOTE_OK; count += unpack_voteid(buf+count, &voteid); DINFO("*** voteid: %llu\n", (unsigned long long)voteid); DINFO("*** VOTE: I am master\n"); g_cf->role = ROLE_MASTER; g_runtime->voteid = voteid; mb_binfo_update(voteid, buf+count, datalen-count); while (count < datalen) { count += unpack_votehost(buf+count, ip, &port); DINFO("*** VOTE backup: %s(%d)\n", ip, port); } /* Host **hostp = &g_runtime->hosts; while (count < datalen) { count += unpack_votehost(buf+count, ip, &port); DINFO("*** backup: %s(%d)\n", ip, port); if (*hostp == NULL) { *hostp = (Host *)zz_malloc(sizeof(Host)); strcpy((*hostp)->ip, ip); (*hostp)->port = port; (*hostp)->next = NULL; hostp = &(*hostp)->next; } else { strcpy((*hostp)->ip, ip); (*hostp)->port = port; hostp = &(*hostp)->next; } } if (*hostp != NULL) { release_hosts(*hostp); *hostp = NULL; } */ conn_send_buffer_reply(conn, MEMLINK_OK, NULL, 0); break; case CMD_VOTE_DETECT: DINFO("*** VOTE DETECT\n"); if (g_cf->role == ROLE_MASTER) { retcode = CMD_VOTE_MASTER; } else if (g_cf->role == ROLE_BACKUP) { retcode = CMD_VOTE_BACKUP; } else { retcode = MEMLINK_ERR_NO_ROLE; } conn_send_buffer_reply(conn, retcode, (char *)&g_runtime->voteid, sizeof(g_runtime->voteid)); break; default: break; } return 0; }
int main (int argc, char **argv) { int c; int start_event = 0; bool foreground = false; while (1) { c = getopt_long(argc, argv, "fhbgv", long_opts, NULL); if (c == EOF) break; switch (c) { case 'b': start_event |= START_BOOT; break; case 'f': foreground = true; break; case 'g': start_event |= START_GET_RPC_METHOD; break; case 'h': print_help(); exit(EXIT_SUCCESS); case 'v': print_version(); exit(EXIT_SUCCESS); default: print_help(); exit(EXIT_FAILURE); } } log_message(NAME, L_DEBUG, "daemon started\n"); setlocale(LC_CTYPE, ""); umask(0037); if (getuid() != 0) { D("run %s as root\n", NAME); exit(EXIT_FAILURE); } /* run early cwmp initialization */ cwmp = calloc(1, sizeof(struct cwmp_internal)); if (!cwmp) return -1; INIT_LIST_HEAD(&cwmp->events); INIT_LIST_HEAD(&cwmp->notifications); INIT_LIST_HEAD(&cwmp->downloads); INIT_LIST_HEAD(&cwmp->scheduled_informs); uloop_init(); backup_init(); config_load(); cwmp_init_deviceid(); if (start_event & START_BOOT) { cwmp_add_event(EVENT_BOOT, NULL, 0, EVENT_BACKUP); cwmp_add_inform_timer(); } if (start_event & START_GET_RPC_METHOD) { cwmp->get_rpc_methods = true; cwmp_add_event(EVENT_PERIODIC, NULL, 0, EVENT_BACKUP); cwmp_add_inform_timer(); } if (netlink_init()) { D("netlink initialization failed\n"); exit(EXIT_FAILURE); } if (ubus_init()) D("ubus initialization failed\n"); http_server_init(); pid_t pid, sid; if (!foreground) { pid = fork(); if (pid < 0) exit(EXIT_FAILURE); if (pid > 0) exit(EXIT_SUCCESS); sid = setsid(); if (sid < 0) { D("setsid() returned error\n"); exit(EXIT_FAILURE); } char *directory = "/"; if ((chdir(directory)) < 0) { D("chdir() returned error\n"); exit(EXIT_FAILURE); } } log_message(NAME, L_DEBUG, "entering main loop\n"); uloop_run(); ubus_exit(); uloop_done(); http_client_exit(); xml_exit(); config_exit(); cwmp_free_deviceid(); free(cwmp); closelog(); log_message(NAME, L_DEBUG, "exiting\n"); return 0; }
static void backup_new_dir (void *msg) { fs_msg_s *m = msg; FN; backup_init(m->dir_id); }