]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dircmd.c
First cut new SD lock + restore buffer bug fix
[bacula/bacula] / bacula / src / stored / dircmd.c
1 /*
2  *  This file handles accepting Director Commands
3  *
4  *    Most Director commands are handled here, with the 
5  *    exception of the Job command command and subsequent 
6  *    subcommands that are handled
7  *    in job.c.  
8  *
9  *    File daemon commands are handled in fdcmd.c
10  *
11  *     Kern Sibbald, May MMI
12  *
13  *   Version $Id$
14  *  
15  */
16 /*
17    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
18
19    This program is free software; you can redistribute it and/or
20    modify it under the terms of the GNU General Public License as
21    published by the Free Software Foundation; either version 2 of
22    the License, or (at your option) any later version.
23
24    This program is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27    General Public License for more details.
28
29    You should have received a copy of the GNU General Public
30    License along with this program; if not, write to the Free
31    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32    MA 02111-1307, USA.
33
34  */
35
36 #include "bacula.h"
37 #include "stored.h"
38
39 /* Exported variables */
40
41 /* Imported variables */
42 extern BSOCK *filed_chan;
43 extern int r_first, r_last;
44 extern struct s_res resources[];
45 extern char my_name[];
46 extern time_t daemon_start_time;
47 extern struct s_last_job last_job;
48
49 /* Static variables */
50 static char derrmsg[]       = "3900 Invalid command\n";
51 static char OKsetdebug[]   = "3000 OK setdebug=%d\n";
52
53
54 /* Imported functions */
55 extern void terminate_child();
56 extern int job_cmd(JCR *jcr);
57
58 /* Forward referenced functions */
59 static int label_cmd(JCR *jcr);
60 static int setdebug_cmd(JCR *jcr);
61 static int cancel_cmd(JCR *cjcr);
62 static int mount_cmd(JCR *jcr);
63 static int unmount_cmd(JCR *jcr);
64 static int status_cmd(JCR *sjcr);
65 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname);
66
67 struct s_cmds {
68    char *cmd;
69    int (*func)(JCR *jcr);
70 };
71
72 /*  
73  * The following are the recognized commands from the Director. 
74  */
75 static struct s_cmds cmds[] = {
76    {"JobId=",    job_cmd},            /* start Job */
77    {"setdebug=", setdebug_cmd},       /* set debug level */
78    {"cancel",    cancel_cmd},
79    {"label",     label_cmd},          /* label a tape */
80    {"mount",     mount_cmd},
81    {"unmount",   unmount_cmd},
82    {"status",    status_cmd},
83    {NULL,        NULL}                /* list terminator */
84 };
85
86
87 /* 
88  * Connection request. We accept connections either from the 
89  *  Director or a Client.
90  * 
91  * Note, we are running as a seperate thread of the Storage daemon.
92  *  and it is because a Director has made a connection with
93  *  us on the "Message" channel.    
94  *
95  * Basic tasks done here:  
96  *  - Create a JCR record
97  *  - Authenticate the Director
98  *  - We wait for a command
99  *  - We execute the command
100  *  - We continue or exit depending on the return status
101  */
102 void connection_request(void *arg)
103 {
104    BSOCK *bs = (BSOCK *)arg;
105    JCR *jcr;
106    int i, found, quit;
107    int bnet_stat = 0;
108    char name[MAX_NAME_LENGTH];
109
110    if (bnet_recv(bs) <= 0) {
111       Emsg0(M_ERROR, 0, "Connection request failed.\n");
112       return;
113    }
114
115    /* 
116     * See if this is a File daemon connection
117     */
118    if (sscanf(bs->msg, "Hello Start Job %127s calling\n", name) == 1) {
119       handle_filed_connection(bs, name);
120       return;
121    }
122    
123    jcr = new_jcr(sizeof(JCR), stored_free_jcr);     /* create Job Control Record */
124    jcr->dir_bsock = bs;               /* save Director bsock */
125
126    Dmsg0(1000, "stored in start_job\n");
127
128    /*
129     * Authenticate the Director
130     */
131    if (!authenticate_director(jcr)) {
132       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
133       free_jcr(jcr);
134       return;
135    }
136    Dmsg0(90, "Message channel init completed.\n");
137
138    for (quit=0; !quit;) {
139
140       /* Read command */
141       if ((bnet_stat = bnet_recv(bs)) <= 0) {
142          break;                       /* connection terminated */
143       }
144       Dmsg1(9, "<dird: %s\n", bs->msg);
145       found = FALSE;
146       for (i=0; cmds[i].cmd; i++) {
147          if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
148             if (!cmds[i].func(jcr)) {    /* do command */
149                quit = TRUE;              /* error, get out */
150                Dmsg1(90, "Command %s requsts quit\n", cmds[i].cmd);
151             }
152             found = TRUE;            /* indicate command found */
153             break;
154          }
155       }
156       if (!found) {                   /* command not found */
157          bnet_fsend(bs, derrmsg);
158          quit = TRUE;
159          break;
160       }
161    }
162    if (bnet_stat != BNET_TERMINATE) {
163       bnet_sig(bs, BNET_TERMINATE);
164    }
165    free_jcr(jcr);
166    return;
167 }
168
169 /*
170  * Set debug level as requested by the Director
171  *
172  */
173 static int setdebug_cmd(JCR *jcr)
174 {
175    BSOCK *dir = jcr->dir_bsock;
176    int level;
177
178    Dmsg1(10, "setdebug_cmd: %s", dir->msg);
179    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
180       bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
181       return 0;
182    }
183    debug_level = level;
184    return bnet_fsend(dir, OKsetdebug, level);
185 }
186
187
188 /*
189  * Cancel a Job
190  */
191 static int cancel_cmd(JCR *cjcr)
192 {
193    BSOCK *dir = cjcr->dir_bsock;
194    int oldStatus;
195    char Job[MAX_NAME_LENGTH];
196    JCR *jcr;
197
198    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
199       if (!(jcr=get_jcr_by_full_name(Job))) {
200          bnet_fsend(dir, _("3992 Job %s not found.\n"), Job);
201       } else {
202          P(jcr->mutex);
203          oldStatus = jcr->JobStatus;
204          jcr->JobStatus = JS_Cancelled;
205          if (!jcr->authenticated && jcr->JobStatus == JS_WaitFD) {
206             pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
207          }
208          V(jcr->mutex);
209          if (jcr->file_bsock) {
210             bnet_sig(jcr->file_bsock, BNET_TERMINATE);
211          }
212          bnet_fsend(dir, _("3000 Job %s Status=%c marked to be cancelled.\n"), 
213             jcr->Job, oldStatus);
214          free_jcr(jcr);
215       }
216    } else {
217       bnet_fsend(dir, _("3993 Error scanning cancel command.\n"));
218    }
219    bnet_sig(dir, BNET_EOD);
220    return 1;
221 }
222
223 /*
224  * Label a tape
225  *
226  */
227 static int label_cmd(JCR *jcr) 
228 {
229    char *dname, *volname, *poolname, *mtype;
230    BSOCK *dir = jcr->dir_bsock;
231    DEVRES *device;
232    DEVICE *dev;
233    int found = 0;
234
235    dname = (char *) get_memory(dir->msglen+1);
236    volname = (char *) get_memory(dir->msglen+1);
237    poolname = (char *) get_memory(dir->msglen+1);
238    mtype = (char *) get_memory(dir->msglen+1);
239    if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s", 
240        dname, volname, poolname, mtype) == 4) {
241       unbash_spaces(dname);
242       unbash_spaces(volname);
243       unbash_spaces(poolname);
244       unbash_spaces(mtype);
245       device = NULL;
246       LockRes();
247       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
248          /* Find resource, and make sure we were able to open it */
249          if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
250             Dmsg1(20, "Found device %s\n", device->hdr.name);
251             found = 1;
252             break;
253          }
254       }
255       UnlockRes();
256       if (found) {
257          /******FIXME**** compare MediaTypes */
258          jcr->device = device;
259          dev = device->dev;
260
261          P(dev->mutex);
262          if (!(dev->state & ST_OPENED)) {
263             if (open_dev(dev, volname, READ_WRITE) < 0) {
264                bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
265             } else {
266                label_volume_if_ok(jcr, dev, volname, poolname);
267                force_close_dev(dev);
268             }
269          } else if (dev->dev_blocked && 
270                     dev->dev_blocked != BST_DOING_ACQUIRE) {  /* device blocked? */
271             label_volume_if_ok(jcr, dev, volname, poolname);
272          } else if (dev->state & ST_READ || dev->num_writers) {
273             if (dev->state & ST_READ) {
274                 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
275                    dev_name(dev));
276             } else {
277                 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
278                    dev_name(dev), dev->num_writers);
279             }
280          } else {                     /* device not being used */
281             label_volume_if_ok(jcr, dev, volname, poolname);
282          }
283          V(dev->mutex);
284       } else {
285          bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
286       }
287    } else {
288       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
289       strcpy(dname, dir->msg);
290       bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), dname);
291    }
292    free_memory(dname);
293    free_memory(volname);
294    free_memory(poolname);
295    free_memory(mtype);
296    bnet_sig(dir, BNET_EOD);
297    return 1;
298 }
299
300 /* 
301  * Read the tape label and determine if we can safely
302  * label the tape (not a Bacula volume), then label it.
303  *
304  *  Enter with the mutex set
305  */
306 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *vname, char *poolname)
307 {
308    BSOCK *dir = jcr->dir_bsock;
309    DEV_BLOCK *block;
310    brwsteal_t hold;
311 #ifndef NEW_LOCK
312    int blocked;
313    pthread_t no_wait_id;
314    
315    blocked = dev->dev_blocked;        /* save any prev blocked state */
316    dev->dev_blocked = BST_WRITING_LABEL;
317    no_wait_id = dev->no_wait_id;
318    dev->no_wait_id = pthread_self();  /* let us use the tape */
319 #endif
320    
321    new_steal_device_lock(dev, &hold, BST_WRITING_LABEL);
322    V(dev->mutex);
323    strcpy(jcr->VolumeName, vname);
324    block = new_block(dev);
325    switch (read_dev_volume_label(jcr, dev, block)) {                
326       case VOL_NAME_ERROR:
327       case VOL_VERSION_ERROR:
328       case VOL_LABEL_ERROR:
329       case VOL_OK:
330          bnet_fsend(dir, _("3901 Cannot label Volume because it is \
331 already labeled: %s\n"), dev->VolHdr.VolName);
332          break;
333       case VOL_IO_ERROR:
334       case VOL_NO_LABEL:
335          write_volume_label_to_dev(jcr, jcr->device, vname, poolname);
336          strcpy(jcr->VolumeName, vname);
337          bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"), 
338             vname, dev->dev_name);
339          break;
340       default:
341          bnet_fsend(dir, _("3902 Cannot label Volume. \
342 Unknown status %d from read_volume_label()\n"), jcr->label_status);
343          break;
344    }
345    free_block(block);
346    P(dev->mutex);
347    new_return_device_lock(dev, &hold);
348 #ifndef NEW_LOCK
349    dev->dev_blocked = blocked;        /* reset blocked state */
350    dev->no_wait_id = no_wait_id;      /* reset blocking thread id */
351 #endif
352 }
353
354
355 /* 
356  * Read the tape label
357  *
358  *  Enter with the mutex set
359  */
360 static int read_label(JCR *jcr, DEVICE *dev)
361 {
362    int stat;
363    BSOCK *dir = jcr->dir_bsock;
364    DEV_BLOCK *block;
365    brwsteal_t hold;
366 #ifndef NEW_LOCK
367    int blocked;
368    pthread_t no_wait_id;
369    
370    blocked = dev->dev_blocked;        /* save any prev blocked state */
371    no_wait_id = dev->no_wait_id;
372    dev->dev_blocked = BST_DOING_ACQUIRE;
373    dev->no_wait_id = pthread_self();  /* let us use the tape */
374 #endif
375    new_steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
376    V(dev->mutex);                     /* release lock */
377    
378    jcr->VolumeName[0] = 0;
379    block = new_block(dev);
380    dev->state &= ~ST_LABEL;           /* force read of label */
381    switch (read_dev_volume_label(jcr, dev, block)) {                
382       case VOL_OK:
383          bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolName);
384          stat = 1;
385          break;
386       default:
387          bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s\n"),
388             dev->dev_name, jcr->errmsg);
389          stat = 0;
390          break;
391    }
392    free_block(block);
393    P(dev->mutex);
394    new_return_device_lock(dev, &hold);
395 #ifndef NEW_LOCK
396    dev->dev_blocked = blocked;        /* reset blocked state */
397    dev->no_wait_id = no_wait_id;      /* reset blocking thread id */
398 #endif
399    return stat;
400 }
401
402 /*
403  * Mount command from Director
404  */
405 static int mount_cmd(JCR *jcr)
406 {
407    char *dev_name;
408    BSOCK *dir = jcr->dir_bsock;
409    DEVRES *device;
410    DEVICE *dev;
411    int found = 0;
412
413    dev_name = (char *) get_memory(dir->msglen);
414    if (sscanf(dir->msg, "mount %s", dev_name) == 1) {
415       unbash_spaces(dev_name);
416       device = NULL;
417       LockRes();
418       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
419          /* Find resource, and make sure we were able to open it */
420          if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
421             Dmsg1(20, "Found device %s\n", device->hdr.name);
422             found = 1;
423             break;
424          }
425       }
426       UnlockRes();
427       if (found) {
428          jcr->device = device;
429          dev = device->dev;
430          P(dev->mutex);
431          switch (dev->dev_blocked) {         /* device blocked? */
432             DEV_BLOCK *block;
433             case BST_WAITING_FOR_SYSOP:
434                /* Someone is waiting, wake him */
435                Dmsg0(90, "Waiting for mount attempt to wake thread\n");
436                pthread_cond_signal(&dev->wait_next_vol);
437                bnet_fsend(dir, "3001 OK mount. Device=%s\n", dev->dev_name);
438                break;
439
440             case BST_UNMOUNTED_WAITING_FOR_SYSOP:
441             case BST_UNMOUNTED:
442                /* We freed the device, so reopen it and wake any waiting threads */
443                if (open_dev(dev, NULL, READ_WRITE) < 0) {
444                   bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"), 
445                      strerror_dev(dev));
446                   break;
447                }
448                block = new_block(dev);
449                read_dev_volume_label(jcr, dev, block);
450                free_block(block);
451                if (dev->dev_blocked == BST_UNMOUNTED) {
452                   Dmsg0(90, "Unmounted unblocking device\n");
453                   read_label(jcr, dev);
454                   new_unlock_device(dev);
455                   unblock_device(dev);
456                } else {
457                   Dmsg0(90, "Unmounted waiting for mount attempt to wake thread\n");
458                   dev->dev_blocked = BST_WAITING_FOR_SYSOP;
459                   pthread_cond_signal(&dev->wait_next_vol);
460                }
461                if (dev->state & ST_LABEL) {
462                   bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"), 
463                      dev->dev_name, dev->VolHdr.VolName);
464                } else {
465                   bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"), 
466                              dev->dev_name);
467                }
468                break;
469
470             case BST_DOING_ACQUIRE:
471                bnet_fsend(dir, _("3001 Device %s is mounted; doing acquire.\n"), 
472                           dev->dev_name);
473                break;
474
475             case BST_WRITING_LABEL:
476                bnet_fsend(dir, _("3903 Device %s is being labeled.\n"), dev->dev_name);
477                break;
478
479             case BST_NOT_BLOCKED:
480                if (dev->state & ST_OPENED) {
481                   if (dev->state & ST_LABEL) {
482                      bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"),
483                         dev->dev_name, dev->VolHdr.VolName);
484                   } else {
485                      bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"), 
486                                 dev->dev_name);
487                   }
488                } else {
489                   if (!dev_is_tape(dev)) {
490                      bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
491                      break;
492                   }
493                   if (open_dev(dev, NULL, READ_WRITE) < 0) {
494                      bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"), 
495                         strerror_dev(dev));
496                      break;
497                   }
498                   read_label(jcr, dev);
499                   if (dev->state & ST_LABEL) {
500                      bnet_fsend(dir, _("3001 Device %s is mounted with Volume %s\n"), 
501                         dev->dev_name, dev->VolHdr.VolName);
502                   } else {
503                      bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"), 
504                                 dev->dev_name);
505                   }
506                }
507                break;
508
509             default:
510                bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
511                break;
512          }
513          V(dev->mutex);
514       } else {
515          bnet_fsend(dir, _("3999 Device %s not found\n"), dev_name);
516       }
517    } else {
518       strcpy(dev_name, dir->msg);
519       bnet_fsend(dir, _("3906 Error scanning mount command: %s\n"), dev_name);
520    }
521    free_memory(dev_name);
522    bnet_sig(dir, BNET_EOD);
523    return 1;
524 }
525
526 /*
527  * unmount command from Director
528  */
529 static int unmount_cmd(JCR *jcr)
530 {
531    char *dname;
532    BSOCK *dir = jcr->dir_bsock;
533    DEVRES *device;
534    DEVICE *dev;
535    int found = 0;
536
537    dname = (char *) get_memory(dir->msglen+1);
538    if (sscanf(dir->msg, "unmount %s", dname) == 1) {
539       unbash_spaces(dname);
540       device = NULL;
541       LockRes();
542       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
543          /* Find resource, and make sure we were able to open it */
544          if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
545             Dmsg1(20, "Found device %s\n", device->hdr.name);
546             found = 1;
547             break;
548          }
549       }
550       UnlockRes();
551       if (found) {
552          jcr->device = device;
553          dev = device->dev;
554          P(dev->mutex);
555          if (!(dev->state & ST_OPENED)) {
556             Dmsg0(90, "Device already unmounted\n");
557             bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"), dev_name(dev));
558
559          } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
560             Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
561                dev->dev_blocked);
562             if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
563                offline_dev(dev);
564             }
565             force_close_dev(dev);
566             dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
567             bnet_fsend(dir, _("3001 Device %s unmounted.\n"), dev_name(dev));
568
569          } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
570             bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
571                dev_name(dev));
572
573          } else if (dev->dev_blocked == BST_WRITING_LABEL) {
574             bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
575                dev_name(dev));
576
577          } else if (dev->state & ST_READ || dev->num_writers) {
578             if (dev->state & ST_READ) {
579                 Dmsg0(90, "Device in read mode\n");
580                 bnet_fsend(dir, _("3904 Device %s is busy with 1 reader.\n"),
581                    dev_name(dev));
582             } else {
583                 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
584                 bnet_fsend(dir, _("3905 Device %s is busy with %d writer(s).\n"),
585                    dev_name(dev), dev->num_writers);
586             }
587
588          } else {                     /* device not being used */
589             Dmsg0(90, "Device not in use, unmounting\n");
590             new_lock_device_state(dev, BST_UNMOUNTED);
591             block_device(dev, BST_UNMOUNTED);
592             if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
593                offline_dev(dev);
594             }
595             force_close_dev(dev);
596             bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
597          }
598          V(dev->mutex);
599       } else {
600          bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
601       }
602    } else {
603       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
604       strcpy(dname, dir->msg);
605       bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), dname);
606    }
607    free_memory(dname);
608    bnet_sig(dir, BNET_EOD);
609    return 1;
610 }
611
612 /*
613  * Status command from Director
614  */
615 static int status_cmd(JCR *jcr)
616 {
617    DEVRES *device;
618    DEVICE *dev;
619    int found, bps, sec, bpb;
620    BSOCK *user = jcr->dir_bsock;
621    char dt[MAX_TIME_LENGTH];
622    char b1[30], b2[30], b3[30];
623
624    bnet_fsend(user, "\n%s Version: " VERSION " (" DATE ")\n", my_name);
625    bstrftime(dt, sizeof(dt), daemon_start_time);
626    bnet_fsend(user, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
627         last_job.NumJobs == 1 ? "" : "s");
628    if (last_job.NumJobs > 0) {
629       char termstat[30];
630
631       bstrftime(dt, sizeof(dt), last_job.end_time);
632       bnet_fsend(user, _("Last Job %s finished at %s\n"), last_job.Job, dt);
633
634       jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
635       bnet_fsend(user, _("  Files=%s Bytes=%s Termination Status=%s\n"), 
636            edit_uint64_with_commas(last_job.JobFiles, b1),
637            edit_uint64_with_commas(last_job.JobBytes, b2),
638            termstat);
639    }
640
641    LockRes();
642    for (device=NULL;  (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
643       dev = device->dev;
644       if (dev) {
645          if (dev->state & ST_OPENED) {
646             if (dev->state & ST_LABEL) {
647                bnet_fsend(user, _("Device %s is mounted with Volume %s\n"), 
648                   dev_name(dev), dev->VolHdr.VolName);
649             } else {
650                bnet_fsend(user, _("Device %s open but no Bacula volume is mounted.\n"), dev_name(dev));
651             }
652             switch (dev->dev_blocked) {
653                case BST_UNMOUNTED:
654                   bnet_fsend(user, _("    Deviced is blocked. User unmounted.\n"));
655                   break;
656                case BST_UNMOUNTED_WAITING_FOR_SYSOP:
657                   bnet_fsend(user, _("    Deviced is blocked. User unmounted during wait for media/mount.\n"));
658                   break;
659                case BST_WAITING_FOR_SYSOP:
660                   if (jcr->JobStatus == JS_WaitMount) {
661                      bnet_fsend(user, _("    Device is blocked waiting for mount.\n"));
662                   } else {
663                      bnet_fsend(user, _("    Device is blocked waiting for appendable media.\n"));
664                   }
665                   break;
666                case BST_DOING_ACQUIRE:
667                   bnet_fsend(user, _("    Device is being initialized.\n"));
668                   break;
669                case BST_WRITING_LABEL:
670                   bnet_fsend(user, _("    Device is blocked labeling a Volume.\n"));
671                   break;
672                default:
673                   break;
674             }
675             bpb = dev->VolCatInfo.VolCatBlocks;
676             if (bpb <= 0) {
677                bpb = 1;
678             }
679             bpb = dev->VolCatInfo.VolCatBytes / bpb;
680             bnet_fsend(user, _("    Total Bytes=%s Blocks=%s Bytes/block=%s\n"),
681                edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, b1),
682                edit_uint64_with_commas(dev->VolCatInfo.VolCatBlocks, b2), 
683                edit_uint64_with_commas(bpb, b3));
684             bnet_fsend(user, _("    Positioned at File=%s Block=%s\n"), 
685                edit_uint64_with_commas(dev->file, b1),
686                edit_uint64_with_commas(dev->block_num, b2));
687
688          } else {
689             bnet_fsend(user, _("Device %s is not open.\n"), dev_name(dev));
690          }
691       }
692    }
693    UnlockRes();
694
695    found = 0;
696    lock_jcr_chain();
697    /* NOTE, we reuse a calling argument jcr. Be warned! */ 
698    for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
699       if (jcr->JobStatus == JS_WaitFD) {
700          bnet_fsend(user, _("%s Job %s waiting for Client connection.\n"),
701             job_type_to_str(jcr->JobType), jcr->Job);
702       }
703       if (jcr->device) {
704          bnet_fsend(user, _("%s %s job %s is using device %s\n"), 
705                    job_level_to_str(jcr->JobLevel),
706                    job_type_to_str(jcr->JobType),
707                    jcr->Job, jcr->device->device_name);
708          sec = time(NULL) - jcr->run_time;
709          if (sec <= 0) {
710             sec = 1;
711          }
712          bps = jcr->JobBytes / sec;
713          bnet_fsend(user, _("    Files=%s Bytes=%s Bytes/sec=%s\n"), 
714             edit_uint64_with_commas(jcr->JobFiles, b1),
715             edit_uint64_with_commas(jcr->JobBytes, b2),
716             edit_uint64_with_commas(bps, b3));
717          found = 1;
718 #ifdef DEBUG
719          if (jcr->file_bsock) {
720             bnet_fsend(user, "    FDReadSeqNo=%" lld " fd=%d\n", 
721                jcr->file_bsock->read_seqno, jcr->file_bsock->fd);
722          } else {
723             bnet_fsend(user, "    FDSocket closed\n");
724          }
725 #endif
726       }
727       free_locked_jcr(jcr);
728    }
729    unlock_jcr_chain();
730    if (!found) {
731       bnet_fsend(user, _("No jobs running.\n"));
732    }
733
734 #ifdef full_status
735    bnet_fsend(user, "\n\n");
736    dump_resource(R_DEVICE, resources[R_DEVICE-r_first].res_head, sendit, user);
737 #endif
738    bnet_fsend(user, "====\n");
739
740    bnet_sig(user, BNET_EOD);
741    return 1;
742 }