/* * Setup a "daemon" JCR for the various standalone * tools (e.g. bls, bextract, bscan, ...) */ JCR *setup_jcr(const char *name, char *dev_name, BSR *bsr, DIRRES *director, const char *VolumeName, int mode) { DCR *dcr; JCR *jcr = new_jcr(sizeof(JCR), my_free_jcr); jcr->bsr = bsr; jcr->director = director; jcr->VolSessionId = 1; jcr->VolSessionTime = (uint32_t)time(NULL); jcr->NumReadVolumes = 0; jcr->NumWriteVolumes = 0; jcr->JobId = 0; jcr->setJobType(JT_CONSOLE); jcr->setJobLevel(L_FULL); jcr->JobStatus = JS_Terminated; jcr->where = bstrdup(""); jcr->job_name = get_pool_memory(PM_FNAME); pm_strcpy(jcr->job_name, "Dummy.Job.Name"); jcr->client_name = get_pool_memory(PM_FNAME); pm_strcpy(jcr->client_name, "Dummy.Client.Name"); bstrncpy(jcr->Job, name, sizeof(jcr->Job)); jcr->fileset_name = get_pool_memory(PM_FNAME); pm_strcpy(jcr->fileset_name, "Dummy.fileset.name"); jcr->fileset_md5 = get_pool_memory(PM_FNAME); pm_strcpy(jcr->fileset_md5, "Dummy.fileset.md5"); new_plugins(jcr); /* instantiate plugins */ init_autochangers(); create_volume_lists(); dcr = setup_to_access_device(jcr, dev_name, VolumeName, mode); if (!dcr) { return NULL; } if (!bsr && VolumeName) { bstrncpy(dcr->VolumeName, VolumeName, sizeof(dcr->VolumeName)); } bstrncpy(dcr->pool_name, "Default", sizeof(dcr->pool_name)); bstrncpy(dcr->pool_type, "Backup", sizeof(dcr->pool_type)); return jcr; }
void *device_initialization(void *arg) { DEVRES *device; DCR *dcr; JCR *jcr; DEVICE *dev; int errstat; LockRes(); pthread_detach(pthread_self()); jcr = new_jcr(sizeof(JCR), stored_free_jcr); new_plugins(jcr); /* instantiate plugins */ jcr->setJobType(JT_SYSTEM); /* * Initialize job start condition variable */ errstat = pthread_cond_init(&jcr->job_start_wait, NULL); if (errstat != 0) { berrno be; Jmsg1(jcr, M_ABORT, 0, _("Unable to init job start cond variable: ERR=%s\n"), be.bstrerror(errstat)); } /* * Initialize job end condition variable */ errstat = pthread_cond_init(&jcr->job_end_wait, NULL); if (errstat != 0) { berrno be; Jmsg1(jcr, M_ABORT, 0, _("Unable to init job endstart cond variable: ERR=%s\n"), be.bstrerror(errstat)); } foreach_res(device, R_DEVICE) { Dmsg1(90, "calling init_dev %s\n", device->device_name); dev = init_dev(NULL, device); Dmsg1(10, "SD init done %s\n", device->device_name); if (!dev) { Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name); continue; } jcr->dcr = dcr = new_dcr(jcr, NULL, dev, NULL); generate_plugin_event(jcr, bsdEventDeviceInit, dcr); if (dev->is_autochanger()) { /* If autochanger set slot in dev structure */ get_autochanger_loaded_slot(dcr); } if (device->cap_bits & CAP_ALWAYSOPEN) { Dmsg1(20, "calling first_open_device %s\n", dev->print_name()); if (!first_open_device(dcr)) { Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), dev->print_name()); Dmsg1(20, "Could not open device %s\n", dev->print_name()); free_dcr(dcr); jcr->dcr = NULL; continue; } } if (device->cap_bits & CAP_AUTOMOUNT && dev->is_open()) { switch (read_dev_volume_label(dcr)) { case VOL_OK: memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo)); volume_unused(dcr); /* mark volume "released" */ break; default: Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), dev->print_name()); break; } } free_dcr(dcr); jcr->dcr = NULL; }
bool setup_job(JCR *jcr) { int errstat; jcr->lock(); Dsm_check(100); init_msg(jcr, jcr->res.messages, job_code_callback_director); /* * Initialize termination condition variable */ if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) { berrno be; Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat)); jcr->unlock(); goto bail_out; } jcr->term_wait_inited = true; /* * Initialize nextrun ready condition variable */ if ((errstat = pthread_cond_init(&jcr->nextrun_ready, NULL)) != 0) { berrno be; Jmsg1(jcr, M_FATAL, 0, _("Unable to init job nextrun cond variable: ERR=%s\n"), be.bstrerror(errstat)); jcr->unlock(); goto bail_out; } jcr->nextrun_ready_inited = true; create_unique_job_name(jcr, jcr->res.job->name()); jcr->setJobStatus(JS_Created); jcr->unlock(); /* * Open database */ Dmsg0(100, "Open database\n"); jcr->db = db_sql_get_pooled_connection(jcr, jcr->res.catalog->db_driver, jcr->res.catalog->db_name, jcr->res.catalog->db_user, jcr->res.catalog->db_password, jcr->res.catalog->db_address, jcr->res.catalog->db_port, jcr->res.catalog->db_socket, jcr->res.catalog->mult_db_connections, jcr->res.catalog->disable_batch_insert); if (jcr->db == NULL) { Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"), jcr->res.catalog->db_name); goto bail_out; } Dmsg0(150, "DB opened\n"); if (!jcr->fname) { jcr->fname = get_pool_memory(PM_FNAME); } if (!jcr->res.pool_source) { jcr->res.pool_source = get_pool_memory(PM_MESSAGE); pm_strcpy(jcr->res.pool_source, _("unknown source")); } if (!jcr->res.npool_source) { jcr->res.npool_source = get_pool_memory(PM_MESSAGE); pm_strcpy(jcr->res.npool_source, _("unknown source")); } if (jcr->JobReads()) { if (!jcr->res.rpool_source) { jcr->res.rpool_source = get_pool_memory(PM_MESSAGE); pm_strcpy(jcr->res.rpool_source, _("unknown source")); } } /* * Create Job record */ init_jcr_job_record(jcr); if (!get_or_create_client_record(jcr)) { goto bail_out; } if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) { Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db)); goto bail_out; } jcr->JobId = jcr->jr.JobId; Dmsg4(100, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", jcr->JobId, jcr->Job, jcr->jr.JobType, jcr->jr.JobLevel); new_plugins(jcr); /* instantiate plugins for this jcr */ generate_plugin_event(jcr, bDirEventJobStart); if (job_canceled(jcr)) { goto bail_out; } if (jcr->JobReads() && !jcr->rstorage) { if (jcr->res.job->storage) { copy_rwstorage(jcr, jcr->res.job->storage, _("Job resource")); } else { copy_rwstorage(jcr, jcr->res.job->pool->storage, _("Pool resource")); } } if (!jcr->JobReads()) { free_rstorage(jcr); } /* * Now, do pre-run stuff, like setting job level (Inc/diff, ...) * this allows us to setup a proper job start record for restarting * in case of later errors. */ switch (jcr->getJobType()) { case JT_BACKUP: switch (jcr->getJobProtocol()) { case PT_NDMP: if (!do_ndmp_backup_init(jcr)) { ndmp_backup_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; default: if (jcr->is_JobLevel(L_VIRTUAL_FULL)) { if (!do_native_vbackup_init(jcr)) { native_vbackup_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } } else { if (!do_native_backup_init(jcr)) { native_backup_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } } break; } break; case JT_VERIFY: if (!do_verify_init(jcr)) { verify_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; case JT_RESTORE: switch (jcr->getJobProtocol()) { case PT_NDMP: if (!do_ndmp_restore_init(jcr)) { ndmp_restore_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; default: /* * Any non NDMP restore is not interested at the items * that were selected for restore so drop them now. */ if (jcr->restore_tree_root) { free_tree(jcr->restore_tree_root); jcr->restore_tree_root = NULL; } if (!do_native_restore_init(jcr)) { native_restore_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; } break; case JT_ADMIN: if (!do_admin_init(jcr)) { admin_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; case JT_COPY: case JT_MIGRATE: if (!do_migration_init(jcr)) { migration_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; default: Pmsg1(0, _("Unimplemented job type: %d\n"), jcr->getJobType()); jcr->setJobStatus(JS_ErrorTerminated); goto bail_out; } generate_plugin_event(jcr, bDirEventJobInit); Dsm_check(100); return true; bail_out: return false; }
/* * Director requests us to start a job * Basic tasks done here: * - We pickup the JobId to be run from the Director. * - We pickup the device, media, and pool from the Director * - Wait for a connection from the File Daemon (FD) * - Accept commands from the FD (i.e. run the job) * - Return when the connection is terminated or * there is an error. */ bool job_cmd(JCR *jcr) { int JobId; char auth_key[100]; char spool_size[30]; char seed[100]; BSOCK *dir = jcr->dir_bsock; POOL_MEM job_name, client_name, job, fileset_name, fileset_md5; int JobType, level, spool_attributes, no_attributes, spool_data; int write_part_after_job, PreferMountedVols; int stat; JCR *ojcr; /* * Get JobId and permissions from Director */ Dmsg1(100, "<dird: %s", dir->msg); bstrncpy(spool_size, "0", sizeof(spool_size)); stat = sscanf(dir->msg, jobcmd, &JobId, job.c_str(), job_name.c_str(), client_name.c_str(), &JobType, &level, fileset_name.c_str(), &no_attributes, &spool_attributes, fileset_md5.c_str(), &spool_data, &write_part_after_job, &PreferMountedVols, spool_size, &jcr->rerunning, &jcr->VolSessionId, &jcr->VolSessionTime); if (stat != 17) { pm_strcpy(jcr->errmsg, dir->msg); dir->fsend(BAD_job, stat, jcr->errmsg); Dmsg1(100, ">dird: %s", dir->msg); jcr->setJobStatus(JS_ErrorTerminated); return false; } Dmsg3(100, "==== rerunning=%d VolSesId=%d VolSesTime=%d\n", jcr->rerunning, jcr->VolSessionId, jcr->VolSessionTime); /* * Since this job could be rescheduled, we * check to see if we have it already. If so * free the old jcr and use the new one. */ ojcr = get_jcr_by_full_name(job.c_str()); if (ojcr && !ojcr->authenticated) { Dmsg2(100, "Found ojcr=0x%x Job %s\n", (unsigned)(intptr_t)ojcr, job.c_str()); free_jcr(ojcr); } jcr->JobId = JobId; Dmsg2(800, "Start JobId=%d %p\n", JobId, jcr); /* * If job rescheduled because previous was incomplete, * the Resched flag is set and VolSessionId and VolSessionTime * are given to us (same as restarted job). */ if (!jcr->rerunning) { jcr->VolSessionId = newVolSessionId(); jcr->VolSessionTime = VolSessionTime; } bstrncpy(jcr->Job, job, sizeof(jcr->Job)); unbash_spaces(job_name); jcr->job_name = get_pool_memory(PM_NAME); pm_strcpy(jcr->job_name, job_name); unbash_spaces(client_name); jcr->client_name = get_pool_memory(PM_NAME); pm_strcpy(jcr->client_name, client_name); unbash_spaces(fileset_name); jcr->fileset_name = get_pool_memory(PM_NAME); pm_strcpy(jcr->fileset_name, fileset_name); jcr->setJobType(JobType); jcr->setJobLevel(level); jcr->no_attributes = no_attributes; jcr->spool_attributes = spool_attributes; jcr->spool_data = spool_data; jcr->spool_size = str_to_int64(spool_size); jcr->write_part_after_job = write_part_after_job; jcr->fileset_md5 = get_pool_memory(PM_NAME); pm_strcpy(jcr->fileset_md5, fileset_md5); jcr->PreferMountedVols = PreferMountedVols; jcr->authenticated = false; /* * Pass back an authorization key for the File daemon */ bsnprintf(seed, sizeof(seed), "%p%d", jcr, JobId); make_session_key(auth_key, seed, 1); dir->fsend(OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key); Dmsg2(50, ">dird jid=%u: %s", (uint32_t)jcr->JobId, dir->msg); jcr->sd_auth_key = bstrdup(auth_key); memset(auth_key, 0, sizeof(auth_key)); new_plugins(jcr); /* instantiate the plugins */ generate_daemon_event(jcr, "JobStart"); generate_plugin_event(jcr, bsdEventJobStart, (void *)"JobStart"); return true; }