]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
f9d258db5090605576f4f6aad23c92c114151901
[bacula/bacula] / bacula / src / dird / job.c
1 /*
2  *
3  *   Bacula Director Job processing routines
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "dird.h"
31
32 /* Forward referenced subroutines */
33 static void *job_thread(void *arg);
34 static char *edit_run_codes(JCR *jcr, char *omsg, char *imsg);
35 static void release_resource_locks(JCR *jcr);
36 static int acquire_resource_locks(JCR *jcr);
37 #ifdef USE_SEMAPHORE
38 static void backoff_resource_locks(JCR *jcr, int count);
39 #endif
40
41 /* Exported subroutines */
42 void run_job(JCR *jcr);
43
44
45 /* Imported subroutines */
46 extern void term_scheduler();
47 extern void term_ua_server();
48 extern int do_backup(JCR *jcr);
49 extern int do_admin(JCR *jcr);
50 extern int do_restore(JCR *jcr);
51 extern int do_verify(JCR *jcr);
52
53 #ifdef USE_SEMAPHORE
54 static semlock_t job_lock;
55 static pthread_mutex_t mutex;
56 static pthread_cond_t  resource_wait;
57 static int waiting = 0;               /* count of waiting threads */
58 #else
59 /* Queue of jobs to be run */
60 workq_t job_wq;                   /* our job work queue */
61 #endif
62
63 void init_job_server(int max_workers)
64 {
65    int stat;
66 #ifdef USE_SEMAPHORE
67    if ((stat = sem_init(&job_lock, max_workers)) != 0) {
68       Emsg1(M_ABORT, 0, _("Could not init job lock: ERR=%s\n"), strerror(stat));
69    }
70    if ((stat = pthread_mutex_init(&mutex, NULL)) != 0) {
71       Emsg1(M_ABORT, 0, _("Could not init resource mutex: ERR=%s\n"), strerror(stat));
72    }
73    if ((stat = pthread_cond_init(&resource_wait, NULL)) != 0) {
74       Emsg1(M_ABORT, 0, _("Could not init resource wait: ERR=%s\n"), strerror(stat));
75    }
76
77 #else
78    if ((stat = workq_init(&job_wq, max_workers, job_thread)) != 0) {
79       Emsg1(M_ABORT, 0, _("Could not init job work queue: ERR=%s\n"), strerror(stat));
80    }
81 #endif
82    return;
83 }
84
85 /*
86  * Run a job -- typically called by the scheduler, but may also
87  *              be called by the UA (Console program).
88  *
89  */
90 void run_job(JCR *jcr)
91 {
92    int stat, errstat;
93 #ifdef USE_SEMAPHORE
94    pthread_t tid;
95 #else
96    workq_ele_t *work_item;
97 #endif
98
99    sm_check(__FILE__, __LINE__, True);
100    init_msg(jcr, jcr->messages);
101    create_unique_job_name(jcr, jcr->job->hdr.name);
102    set_jcr_job_status(jcr, JS_Created);
103    jcr->jr.SchedTime = jcr->sched_time;
104    jcr->jr.StartTime = jcr->start_time;
105    jcr->jr.Type = jcr->JobType;
106    jcr->jr.Level = jcr->JobLevel;
107    jcr->jr.JobStatus = jcr->JobStatus;
108    bstrncpy(jcr->jr.Name, jcr->job->hdr.name, sizeof(jcr->jr.Name));
109    bstrncpy(jcr->jr.Job, jcr->Job, sizeof(jcr->jr.Job));
110
111    /* Initialize termination condition variable */
112    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
113       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
114       set_jcr_job_status(jcr, JS_ErrorTerminated);
115       free_jcr(jcr);
116       return;
117    }
118
119    /*
120     * Open database
121     */
122    Dmsg0(50, "Open database\n");
123    jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
124                             jcr->catalog->db_password, jcr->catalog->db_address,
125                             jcr->catalog->db_port, jcr->catalog->db_socket);
126    if (!db_open_database(jcr, jcr->db)) {
127       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
128       set_jcr_job_status(jcr, JS_ErrorTerminated);
129       free_jcr(jcr);
130       return;
131    }
132    Dmsg0(50, "DB opened\n");
133
134    /*
135     * Create Job record  
136     */
137    jcr->jr.JobStatus = jcr->JobStatus;
138    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
139       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
140       set_jcr_job_status(jcr, JS_ErrorTerminated);
141       free_jcr(jcr);
142       return;
143    }
144    jcr->JobId = jcr->jr.JobId;
145    ASSERT(jcr->jr.JobId > 0);
146
147    Dmsg4(30, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
148        jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
149    Dmsg0(200, "Add jrc to work queue\n");
150
151 #ifdef USE_SEMAPHORE
152   if ((stat = pthread_create(&tid, NULL, job_thread, (void *)jcr)) != 0) {
153       Emsg1(M_ABORT, 0, _("Unable to create job thread: ERR=%s\n"), strerror(stat));
154    }
155 #else
156    /* Queue the job to be run */
157    if ((stat = workq_add(&job_wq, (void *)jcr, &work_item, 0)) != 0) {
158       Emsg1(M_ABORT, 0, _("Could not add job to work queue: ERR=%s\n"), strerror(stat));
159    }
160    jcr->work_item = work_item;
161 #endif
162    Dmsg0(200, "Done run_job()\n");
163 }
164
165 /* 
166  * This is the engine called by workq_add() when we were pulled                
167  *  from the work queue.
168  *  At this point, we are running in our own thread 
169  */
170 static void *job_thread(void *arg)
171 {
172    JCR *jcr = (JCR *)arg;
173
174    pthread_detach(pthread_self());
175    sm_check(__FILE__, __LINE__, True);
176
177    if (!acquire_resource_locks(jcr)) {
178       set_jcr_job_status(jcr, JS_Canceled);
179    }
180
181    Dmsg0(200, "=====Start Job=========\n");
182    jcr->start_time = time(NULL);      /* set the real start time */
183    set_jcr_job_status(jcr, JS_Running);
184
185    if (job_canceled(jcr)) {
186       update_job_end_record(jcr);
187    } else if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
188        (utime_t)(jcr->start_time - jcr->sched_time)) {
189       Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
190       set_jcr_job_status(jcr, JS_Canceled);
191       update_job_end_record(jcr);
192    } else {
193
194       /* Run Job */
195       if (jcr->job->RunBeforeJob) {
196          POOLMEM *before = get_pool_memory(PM_FNAME);
197          int status;
198          BPIPE *bpipe;
199          char line[MAXSTRING];
200          
201          before = edit_run_codes(jcr, before, jcr->job->RunBeforeJob);
202          bpipe = open_bpipe(before, 0, "r");
203          while (fgets(line, sizeof(line), bpipe->rfd)) {
204             Jmsg(jcr, M_INFO, 0, _("RunBefore: %s"), line);
205          }
206          status = close_bpipe(bpipe);
207          if (status != 0) {
208             Jmsg(jcr, M_FATAL, 0, _("RunBeforeJob returned non-zero status=%d\n"),
209                status);
210             set_jcr_job_status(jcr, JS_FatalError);
211             update_job_end_record(jcr);
212             free_pool_memory(before);
213             goto bail_out;
214          }
215          free_pool_memory(before);
216       }
217       switch (jcr->JobType) {
218          case JT_BACKUP:
219             do_backup(jcr);
220             if (jcr->JobStatus == JS_Terminated) {
221                do_autoprune(jcr);
222             }
223             break;
224          case JT_VERIFY:
225             do_verify(jcr);
226             if (jcr->JobStatus == JS_Terminated) {
227                do_autoprune(jcr);
228             }
229             break;
230          case JT_RESTORE:
231             do_restore(jcr);
232             if (jcr->JobStatus == JS_Terminated) {
233                do_autoprune(jcr);
234             }
235             break;
236          case JT_ADMIN:
237             do_admin(jcr);
238             if (jcr->JobStatus == JS_Terminated) {
239                do_autoprune(jcr);
240             }
241             break;
242          default:
243             Pmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
244             break;
245          }
246       if (jcr->job->RunAfterJob) {
247          POOLMEM *after = get_pool_memory(PM_FNAME);
248          int status;
249          BPIPE *bpipe;
250          char line[MAXSTRING];
251          
252          after = edit_run_codes(jcr, after, jcr->job->RunAfterJob);
253          bpipe = open_bpipe(after, 0, "r");
254          while (fgets(line, sizeof(line), bpipe->rfd)) {
255             Jmsg(jcr, M_INFO, 0, _("RunAfter: %s"), line);
256          }
257          status = close_bpipe(bpipe);
258          if (status != 0) {
259             Jmsg(jcr, M_FATAL, 0, _("RunAfterJob returned non-zero status=%d\n"),
260                status);
261             set_jcr_job_status(jcr, JS_FatalError);
262             update_job_end_record(jcr);
263          }
264          free_pool_memory(after);
265       }
266    }
267 bail_out:
268    release_resource_locks(jcr);
269    Dmsg0(50, "Before free jcr\n");
270    free_jcr(jcr);
271    Dmsg0(50, "======== End Job ==========\n");
272    sm_check(__FILE__, __LINE__, True);
273    return NULL;
274 }
275
276 /*
277  * Acquire the resources needed. These locks limit the
278  *  number of jobs by each resource. We have limits on
279  *  Jobs, Clients, Storage, and total jobs.
280  */
281 static int acquire_resource_locks(JCR *jcr)
282 {
283    time_t now = time(NULL);
284    time_t wtime = jcr->sched_time - now;
285
286    /* Wait until scheduled time arrives */
287    if (wtime > 0 && verbose) {
288       Jmsg(jcr, M_INFO, 0, _("Waiting %d seconds for start time.\n"), wtime);
289       set_jcr_job_status(jcr, JS_WaitStartTime);
290    }
291    while (wtime > 0) {
292       Dmsg2(100, "Waiting on sched time, jobid=%d secs=%d\n", jcr->JobId, wtime);
293       if (wtime > 30) {
294          wtime = 30;
295       }
296       bmicrosleep(wtime, 0);
297       if (job_canceled(jcr)) {
298          return 0;
299       }
300       wtime = jcr->sched_time - time(NULL);
301
302    }
303
304
305 #ifdef USE_SEMAPHORE
306    int stat;
307
308    /* Initialize semaphores */
309    if (jcr->store->sem.valid != SEMLOCK_VALID) {
310       if ((stat = sem_init(&jcr->store->sem, jcr->store->MaxConcurrentJobs)) != 0) {
311          Emsg1(M_ABORT, 0, _("Could not init Storage semaphore: ERR=%s\n"), strerror(stat));
312       }
313    }
314    if (jcr->client->sem.valid != SEMLOCK_VALID) {
315       if ((stat = sem_init(&jcr->client->sem, jcr->client->MaxConcurrentJobs)) != 0) {
316          Emsg1(M_ABORT, 0, _("Could not init Client semaphore: ERR=%s\n"), strerror(stat));
317       }
318    }
319    if (jcr->job->sem.valid != SEMLOCK_VALID) {
320       if ((stat = sem_init(&jcr->job->sem, jcr->job->MaxConcurrentJobs)) != 0) {
321          Emsg1(M_ABORT, 0, _("Could not init Job semaphore: ERR=%s\n"), strerror(stat));
322       }
323    }
324
325    for ( ;; ) {
326       /* Acquire semaphore */
327       set_jcr_job_status(jcr, JS_WaitJobRes);
328       if ((stat = sem_lock(&jcr->job->sem)) != 0) {
329          Emsg1(M_ABORT, 0, _("Could not acquire Job max jobs lock: ERR=%s\n"), strerror(stat));
330       }
331       set_jcr_job_status(jcr, JS_WaitClientRes);
332       if ((stat = sem_trylock(&jcr->client->sem)) != 0) {
333          if (stat == EBUSY) {
334             backoff_resource_locks(jcr, 1);
335             goto wait;
336          } else {
337             Emsg1(M_ABORT, 0, _("Could not acquire Client max jobs lock: ERR=%s\n"), strerror(stat));
338          }
339       }
340       set_jcr_job_status(jcr, JS_WaitStoreRes);
341       if ((stat = sem_trylock(&jcr->store->sem)) != 0) {
342          if (stat == EBUSY) {
343             backoff_resource_locks(jcr, 2);
344             goto wait;
345          } else {
346             Emsg1(M_ABORT, 0, _("Could not acquire Storage max jobs lock: ERR=%s\n"), strerror(stat));
347          }
348       }
349       set_jcr_job_status(jcr, JS_WaitMaxJobs);
350       if ((stat = sem_trylock(&job_lock)) != 0) {
351          if (stat == EBUSY) {
352             backoff_resource_locks(jcr, 3);
353             goto wait;
354          } else {
355             Emsg1(M_ABORT, 0, _("Could not acquire max jobs lock: ERR=%s\n"), strerror(stat));
356          }
357       }
358       break;
359
360 wait:
361       if (job_canceled(jcr)) {
362          return 0;
363       }
364       P(mutex);
365       /*
366        * Wait for a resource to be released either by backoff or
367        *  by a job terminating.
368        */
369       waiting++;
370       pthread_cond_wait(&resource_wait, &mutex);
371       waiting--;
372       V(mutex);
373       /* Try again */
374    }
375    jcr->acquired_resource_locks = 1;
376 #endif
377    return 1;
378 }
379
380 #ifdef USE_SEMAPHORE
381 /*
382  * We could not get all the resource locks because 
383  *  too many jobs are running, so release any locks
384  *  we did acquire, giving others a chance to use them
385  *  while we wait.
386  */
387 static void backoff_resource_locks(JCR *jcr, int count)
388 {
389    P(mutex);
390    switch (count) {
391    case 3:
392       sem_unlock(&jcr->store->sem);
393       /* Fall through wanted */
394    case 2:
395       sem_unlock(&jcr->client->sem);
396       /* Fall through wanted */
397    case 1:
398       sem_unlock(&jcr->job->sem);
399       break;
400    }
401    /*
402     * Since we released a lock, if there are any threads
403     *  waiting, wake them up so that they can try again.
404     */
405    if (waiting > 0) {
406       pthread_cond_broadcast(&resource_wait);
407    }
408    V(mutex);
409 }
410 #endif
411
412 /*
413  * This is called at the end of the job to release
414  *   any resource limits on the number of jobs. If
415  *   there are any other jobs waiting, we wake them
416  *   up so that they can try again.
417  */
418 static void release_resource_locks(JCR *jcr)
419 {
420    if (!jcr->acquired_resource_locks) {
421       return;                         /* Job canceled, no locks acquired */
422    }
423 #ifdef USE_SEMAPHORE
424    P(mutex);
425    sem_unlock(&jcr->store->sem);
426    sem_unlock(&jcr->client->sem);
427    sem_unlock(&jcr->job->sem);
428    sem_unlock(&job_lock);
429    if (waiting > 0) {
430       pthread_cond_broadcast(&resource_wait);
431    }
432    V(mutex);
433 #endif
434 }
435
436 /*
437  * Get or create a Client record for this Job
438  */
439 int get_or_create_client_record(JCR *jcr)
440 {
441    CLIENT_DBR cr;
442
443    memset(&cr, 0, sizeof(cr));
444    bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
445    cr.AutoPrune = jcr->client->AutoPrune;
446    cr.FileRetention = jcr->client->FileRetention;
447    cr.JobRetention = jcr->client->JobRetention;
448    if (jcr->client_name) {
449       free_pool_memory(jcr->client_name);
450    }
451    jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
452    strcpy(jcr->client_name, jcr->client->hdr.name);
453    if (!db_create_client_record(jcr, jcr->db, &cr)) {
454       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. ERR=%s\n"), 
455          db_strerror(jcr->db));
456       return 0;
457    }
458    jcr->jr.ClientId = cr.ClientId;
459    if (cr.Uname[0]) {
460       if (jcr->client_uname) {
461          free_pool_memory(jcr->client_uname);
462       }
463       jcr->client_uname = get_memory(strlen(cr.Uname) + 1);
464       strcpy(jcr->client_uname, cr.Uname);
465    }
466    Dmsg2(100, "Created Client %s record %d\n", jcr->client->hdr.name, 
467       jcr->jr.ClientId);
468    return 1;
469 }
470
471
472 /*
473  * Write status and such in DB
474  */
475 void update_job_end_record(JCR *jcr)
476 {
477    if (jcr->jr.EndTime == 0) {
478       jcr->jr.EndTime = time(NULL);
479    }
480    jcr->end_time = jcr->jr.EndTime;
481    jcr->jr.JobId = jcr->JobId;
482    jcr->jr.JobStatus = jcr->JobStatus;
483    jcr->jr.JobFiles = jcr->JobFiles;
484    jcr->jr.JobBytes = jcr->JobBytes;
485    jcr->jr.VolSessionId = jcr->VolSessionId;
486    jcr->jr.VolSessionTime = jcr->VolSessionTime;
487    if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
488       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
489          db_strerror(jcr->db));
490    }
491 }
492
493 /*
494  * Takes base_name and appends (unique) current
495  *   date and time to form unique job name.
496  *
497  *  Returns: unique job name in jcr->Job
498  *    date/time in jcr->start_time
499  */
500 void create_unique_job_name(JCR *jcr, char *base_name)
501 {
502    /* Job start mutex */
503    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
504    static time_t last_start_time = 0;
505    time_t now;
506    struct tm tm;
507    char dt[MAX_TIME_LENGTH];
508    char name[MAX_NAME_LENGTH];
509    char *p;
510
511    /* Guarantee unique start time -- maximum one per second, and
512     * thus unique Job Name 
513     */
514    P(mutex);                          /* lock creation of jobs */
515    now = time(NULL);
516    while (now == last_start_time) {
517       bmicrosleep(0, 500000);
518       now = time(NULL);
519    }
520    last_start_time = now;
521    V(mutex);                          /* allow creation of jobs */
522    jcr->start_time = now;
523    /* Form Unique JobName */
524    localtime_r(&now, &tm);
525    /* Use only characters that are permitted in Windows filenames */
526    strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", &tm); 
527    bstrncpy(name, base_name, sizeof(name));
528    name[sizeof(name)-22] = 0;          /* truncate if too long */
529    sprintf(jcr->Job, "%s.%s", name, dt); /* add date & time */
530    /* Convert spaces into underscores */
531    for (p=jcr->Job; *p; p++) {
532       if (*p == ' ') {
533          *p = '_';
534       }
535    }
536 }
537
538 /*
539  * Free the Job Control Record if no one is still using it.
540  *  Called from main free_jcr() routine in src/lib/jcr.c so
541  *  that we can do our Director specific cleanup of the jcr.
542  */
543 void dird_free_jcr(JCR *jcr)
544 {
545    Dmsg0(200, "Start dird free_jcr\n");
546
547    if (jcr->file_bsock) {
548       Dmsg0(200, "Close File bsock\n");
549       bnet_close(jcr->file_bsock);
550    }
551    if (jcr->store_bsock) {
552       Dmsg0(200, "Close Store bsock\n");
553       bnet_close(jcr->store_bsock);
554    }
555    if (jcr->fname) {  
556       Dmsg0(200, "Free JCR fname\n");
557       free_pool_memory(jcr->fname);
558    }
559    if (jcr->stime) {
560       Dmsg0(200, "Free JCR stime\n");
561       free_pool_memory(jcr->stime);
562    }
563    if (jcr->db) {
564       Dmsg0(200, "Close DB\n");
565       db_close_database(jcr, jcr->db);
566    }
567    if (jcr->RestoreWhere) {
568       free(jcr->RestoreWhere);
569    }
570    if (jcr->RestoreBootstrap) {
571       free(jcr->RestoreBootstrap);
572    }
573    if (jcr->client_uname) {
574       free_pool_memory(jcr->client_uname);
575    }
576    Dmsg0(200, "End dird free_jcr\n");
577 }
578
579 /*
580  * Set some defaults in the JCR necessary to
581  * run. These items are pulled from the job
582  * definition as defaults, but can be overridden
583  * later either by the Run record in the Schedule resource,
584  * or by the Console program.
585  */
586 void set_jcr_defaults(JCR *jcr, JOB *job)
587 {
588    jcr->job = job;
589    jcr->JobType = job->JobType;
590    jcr->JobLevel = job->level;
591    jcr->store = job->storage;
592    jcr->client = job->client;
593    if (jcr->client_name) {
594       free_pool_memory(jcr->client_name);
595    }
596    jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
597    strcpy(jcr->client_name, jcr->client->hdr.name);
598    jcr->pool = job->pool;
599    jcr->catalog = job->client->catalog;
600    jcr->fileset = job->fileset;
601    jcr->messages = job->messages; 
602    if (jcr->RestoreBootstrap) {
603       free(jcr->RestoreBootstrap);
604    }
605    /* This can be overridden by Console program */
606    if (job->RestoreBootstrap) {
607       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
608    }
609    /* If no default level given, set one */
610    if (jcr->JobLevel == 0) {
611       switch (jcr->JobType) {
612       case JT_VERIFY:
613          jcr->JobLevel = L_VERIFY_CATALOG;
614          break;
615       case JT_BACKUP:
616          jcr->JobLevel = L_INCREMENTAL;
617          break;
618       case JT_RESTORE:
619       case JT_ADMIN:
620          jcr->JobLevel = L_FULL;
621          break;
622       default:
623          break;
624       }
625    }
626 }
627
628 /*
629  * Edit codes into Run command
630  *  %% = %
631  *  %c = Client's name
632  *  %d = Director's name
633  *  %i = JobId
634  *  %e = Job Exit
635  *  %j = Job
636  *  %l = Job Level
637  *  %n = Job name
638  *  %t = Job type
639  *
640  *  omsg = edited output message
641  *  imsg = input string containing edit codes (%x)
642  *
643  */
644 static char *edit_run_codes(JCR *jcr, char *omsg, char *imsg) 
645 {
646    char *p;
647    const char *str;
648    char add[20];
649
650    *omsg = 0;
651    Dmsg1(200, "edit_run_codes: %s\n", imsg);
652    for (p=imsg; *p; p++) {
653       if (*p == '%') {
654          switch (*++p) {
655          case '%':
656             str = "%";
657             break;
658          case 'c':
659             str = jcr->client_name;
660             if (!str) {
661                str = "";
662             }
663             break;
664          case 'd':
665             str = my_name;
666             break;
667          case 'e':
668             str = job_status_to_str(jcr->JobStatus);
669             break;
670          case 'i':
671             sprintf(add, "%d", jcr->JobId);
672             str = add;
673             break;
674          case 'j':                    /* Job */
675             str = jcr->Job;
676             break;
677          case 'l':
678             str = job_level_to_str(jcr->JobLevel);
679             break;
680          case 'n':
681             str = jcr->job->hdr.name;
682             break;
683          case 't':
684             str = job_type_to_str(jcr->JobType);
685             break;
686          default:
687             add[0] = '%';
688             add[1] = *p;
689             add[2] = 0;
690             str = add;
691             break;
692          }
693       } else {
694          add[0] = *p;
695          add[1] = 0;
696          str = add;
697       }
698       Dmsg1(200, "add_str %s\n", str);
699       pm_strcat(&omsg, (char *)str);
700       Dmsg1(200, "omsg=%s\n", omsg);
701    }
702    return omsg;
703 }