int main (int argc, char *argv[]) { int ch; JCR *jcr; cat_op mode; bool no_signals = false; bool test_config = false; char *uid = NULL; char *gid = NULL; start_heap = sbrk(0); setlocale(LC_ALL, ""); bindtextdomain("bareos", LOCALEDIR); textdomain("bareos"); init_stack_dump(); my_name_is(argc, argv, "bareos-dir"); init_msg(NULL, NULL); /* initialize message handler */ init_reload(); daemon_start_time = time(NULL); console_command = run_console_command; while ((ch = getopt(argc, argv, "c:d:fg:mr:stu:v?")) != -1) { switch (ch) { case 'c': /* specify config file */ if (configfile != NULL) { free(configfile); } configfile = bstrdup(optarg); break; case 'd': /* set debug level */ if (*optarg == 't') { dbg_timestamp = true; } else { debug_level = atoi(optarg); if (debug_level <= 0) { debug_level = 1; } } Dmsg1(10, "Debug level = %d\n", debug_level); break; case 'f': /* run in foreground */ background = false; break; case 'g': /* set group id */ gid = optarg; break; case 'm': /* print kaboom output */ prt_kaboom = true; break; case 'r': /* run job */ if (runjob != NULL) { free(runjob); } if (optarg) { runjob = bstrdup(optarg); } break; case 's': /* turn off signals */ no_signals = true; break; case 't': /* test config */ test_config = true; break; case 'u': /* set uid */ uid = optarg; break; case 'v': /* verbose */ verbose++; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (!no_signals) { init_signals(terminate_dird); } if (argc) { if (configfile != NULL) { free(configfile); } configfile = bstrdup(*argv); argc--; argv++; } if (argc) { usage(); } if (configfile == NULL) { configfile = bstrdup(CONFIG_FILE); } /* * See if we want to drop privs. */ if (geteuid() == 0) { drop(uid, gid, false); /* reduce privileges if requested */ } my_config = new_config_parser(); parse_dir_config(my_config, configfile, M_ERROR_TERM); if (init_crypto() != 0) { Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n")); goto bail_out; } if (!check_resources()) { Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile); goto bail_out; } if (!test_config) { /* we don't need to do this block in test mode */ if (background) { daemon_start(); init_stack_dump(); /* grab new pid */ } /* Create pid must come after we are a daemon -- so we have our final pid */ create_pid_file(me->pid_directory, "bareos-dir", get_first_port_host_order(me->DIRaddrs)); read_state_file(me->working_directory, "bareos-dir", get_first_port_host_order(me->DIRaddrs)); } set_jcr_in_tsd(INVALID_JCR); set_thread_concurrency(me->MaxConcurrentJobs * 2 + 4 /* UA */ + 5 /* sched+watchdog+jobsvr+misc */); lmgr_init_thread(); /* initialize the lockmanager stack */ load_dir_plugins(me->plugin_directory, me->plugin_names); /* * If we are in testing mode, we don't try to fix the catalog */ mode = (test_config) ? CHECK_CONNECTION : UPDATE_AND_FIX; if (!check_catalog(mode)) { Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile); goto bail_out; } if (test_config) { terminate_dird(0); } if (!initialize_sql_pooling()) { Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile); goto bail_out; } my_name_is(0, NULL, me->name()); /* set user defined name */ cleanup_old_files(); p_db_log_insert = (db_log_insert_func)dir_db_log_insert; #if !defined(HAVE_WIN32) signal(SIGHUP, reload_config); #endif init_console_msg(working_directory); Dmsg0(200, "Start UA server\n"); start_UA_server(me->DIRaddrs); start_watchdog(); /* start network watchdog thread */ if (me->jcr_watchdog_time) { init_jcr_subsystem(me->jcr_watchdog_time); /* start JCR watchdogs etc. */ } init_job_server(me->MaxConcurrentJobs); dbg_jcr_add_hook(db_debug_print); /* used to debug B_DB connexion after fatal signal */ // init_device_resources(); Dmsg0(200, "wait for next job\n"); /* Main loop -- call scheduler to get next job to run */ while ( (jcr = wait_for_next_job(runjob)) ) { run_job(jcr); /* run job */ free_jcr(jcr); /* release jcr */ set_jcr_in_tsd(INVALID_JCR); if (runjob) { /* command line, run a single job? */ break; /* yes, terminate */ } } terminate_dird(0); bail_out: return 0; }
//-------------------------------------------------------------------- //----- CommandRecognizing() //-------- sd - socket descriptor //-------------------------------------------------------------------- void CommandRecognizing (int sd) { RD cmd; int res, links=0; int IsName = 0; int IsWorking = 1; // sign of break or end connection by client char ClientName[256]; struct sockaddr_in addr; int addr_size = sizeof (addr); c_tree con_tree; ClientName[0] = 0; int last_error; res = getpeername (sd, (struct sockaddr *) &addr, (socklen_t *) & addr_size); last_error = errno; if (res != 0) { fprintf (stderr,"(%d) connector.c getpeername() ERROR(%d)\n", sd, last_error);fflush(stdout); return; } ConnectorsCounter++; CreateList (&con_tree); while (IsWorking > 0) { if( WaitNetDataExt(sd,5000) > 0) { if (STOP_TASK) { cmd.command = 88; break; } cmd.command = 80; IsWorking = recv (sd, &cmd, sizeof (cmd), MSG_WAITALL); // gettimeofday(&tv1, NULL); // printf("(%d)Connector recv: command = %d, work = %d, IsWorking = %d\n", sd, cmd.command, cmd.work, IsWorking);fflush(stdout); if (IsWorking > 0) { switch (cmd.command) { case GET_UNIT_IDENT_BY_NAME: IsWorking = GetUnitIdentByName (sd, cmd.work); break; case ACCEPT_ITEM: IsWorking = AcceptItem (sd, (ident_t) cmd.work); break; case FREE_ITEM: IsWorking = FreeItem (sd, (ident_t) cmd.work); break; case FREE_EXT_ITEM: IsWorking = FreeExtItem (sd, (ident_t) cmd.work, &con_tree); break; case READ_BY_IDENT: IsWorking = ReadByIdent (sd, (ident_t) cmd.work); break; case WRITE_BY_IDENT: IsWorking = WriteByIdent (sd, (ident_t) cmd.work); break; case END_SESSION: IsWorking = EndSession (sd); break; case READ_ALL: IsWorking = ReadAll (sd, &con_tree); break; case READ_NEW: IsWorking = ReadNew (sd, &con_tree); break; case LINK_ITEM_BY_NAME: IsWorking = LinkItem (sd, cmd.work, &con_tree); if(IsWorking) links++; break; case BEGIN_SESSION: recv (sd, ClientName, cmd.work, MSG_WAITALL); IsName = 1; break; case INIT_DB: init_reload(); IsName = 1; fprintf (stdout,"<%s> init reload DB [sd=(%d) %s:%d]. Now are %2d clients.\n", ClientName[0] ? ClientName : "Unknown", sd, inet_ntoa (addr.sin_addr), ntohs (addr.sin_port), ConnectorsCounter); break; case WRITE_MULTIPLE: IsWorking = WriteMultiple(sd, cmd.work); break; default: fprintf (stderr,"Unrecognized command %d \n", cmd.command);fflush(stdout); break; } if (IsName < 2) { fprintf (stderr,"<%s> has been connected [sd=(%d) %s:%d]. Now are %2d clients.\n", ClientName[0] ? ClientName : "Unknown", sd, inet_ntoa (addr.sin_addr), ntohs (addr.sin_port), ConnectorsCounter);fflush(stdout); IsName = 2; } } } else { fprintf (stderr, "Connector() TIMEOUT\n");fflush(stdout); break; } } ClearList (&con_tree); res = ServerFreeAll (sd); ConnectorsCounter--; fprintf (stderr, "(cmd=%d)(links=%d)<%s> has been disconnected [sd=(%d) %s:%d]. Now are %2d clients.\n", cmd.command,links, ClientName[0] ? ClientName : "Unknown", sd, inet_ntoa (addr.sin_addr), ntohs (addr.sin_port), ConnectorsCounter);fflush(stdout); }