]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/getmsg.c
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / dird / getmsg.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2018 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *
21  *   Bacula Director -- routines to receive network data and
22  *    handle network signals. These routines handle the connections
23  *    to the Storage daemon and the File daemon.
24  *
25  *     Kern Sibbald, August MM
26  *
27  *    This routine runs as a thread and must be thread reentrant.
28  *
29  *  Basic tasks done here:
30  *    Handle  network signals (signals).
31  *       Signals always have return status 0 from bnet_recv() and
32  *       a zero or negative message length.
33  *    Pass appropriate messages back to the caller (responses).
34  *       Responses always have a digit as the first character.
35  *    Handle requests for message and catalog services (requests).
36  *       Requests are any message that does not begin with a digit.
37  *       In affect, they are commands.
38  *
39  */
40
41 #include "bacula.h"
42 #include "dird.h"
43
44 /* Forward referenced functions */
45 static char *find_msg_start(char *msg);
46
47 static char Job_status[] = "Status JobId=%ld JobStatus=%d\n";
48 #ifdef needed
49 static char Device_update[]   = "DevUpd JobId=%127s "
50    "device=%127s "
51    "append=%d read=%d num_writers=%d "
52    "open=%d labeled=%d offline=%d "
53    "reserved=%d max_writers=%d "
54    "autoselect=%d autochanger=%d "
55    "changer_name=%127s media_type=%127s volume_name=%127s "
56    "DevReadTime=%d DevWriteTime=%d DevReadBytes=%d "
57    "DevWriteBytes=%d\n";
58 #endif
59
60
61 static char OK_msg[] = "1000 OK\n";
62
63
64 static void set_jcr_sd_job_status(JCR *jcr, int SDJobStatus)
65 {
66    bool set_waittime=false;
67    Dmsg2(800, "set_jcr_sd_job_status(%s, %c)\n", jcr->Job, SDJobStatus);
68    /* if wait state is new, we keep current time for watchdog MaxWaitTime */
69    switch (SDJobStatus) {
70       case JS_WaitMedia:
71       case JS_WaitMount:
72       case JS_WaitMaxJobs:
73          set_waittime = true;
74       default:
75          break;
76    }
77
78    if (job_waiting(jcr)) {
79       set_waittime = false;
80    }
81
82    if (set_waittime) {
83       /* set it before JobStatus */
84       Dmsg0(800, "Setting wait_time\n");
85       jcr->wait_time = time(NULL);
86    }
87    jcr->SDJobStatus = SDJobStatus;
88    if (jcr->SDJobStatus == JS_Incomplete) {
89       jcr->setJobStatus(JS_Incomplete);
90    }
91
92 }
93
94 /*
95  * See if we are pointing to a message id
96  *   Look for: [XYnnnn]
97  */
98 static bool is_msgid(char *msg)
99 {
100    if (!msg) return false;
101    char *end = strchr(msg, ']');
102    if (!end) return false;
103    if ((end - msg) != 7) return false;
104    if (!B_ISUPPER(msg[1]) || !B_ISUPPER(msg[2])) return false;
105    for (int i=3; i<7; i++) {
106       if (!B_ISDIGIT(msg[i])) return false;
107    }
108    return true;
109 }
110
111 /*
112  * Get a message
113  *  Call appropriate processing routine
114  *  If it is not a Jmsg or a ReqCat message,
115  *   return it to the caller.
116  *
117  *  This routine is called to get the next message from
118  *  another daemon. If the message is in canonical message
119  *  format and the type is known, it will be dispatched
120  *  to the appropriate handler.  If the message is
121  *  in any other format, it will be returned.
122  *
123  *  E.g. any message beginning with a digit will be passed
124  *       through to the caller.
125  *  All other messages are expected begin with some identifier
126  *    -- for the moment only the first character is checked, but
127  *    at a later time, the whole identifier (e.g. Jmsg, CatReq, ...)
128  *    could be checked. 
129  *    This is followed by JobId=nnn <user-defined>
130  *    info. The identifier is used to dispatch the message to the right
131  *    place (Job message, catalog request, ...). The Job is used to lookup
132  *    the JCR so that the action is performed on the correct jcr, and
133  *    the rest of the message is up to the user.  Note, DevUpd uses
134  *    *System* for the Job name, and hence no JCR is obtained. This
135  *    is a *rare* case where a jcr is not really needed.
136  *
137  */
138 int bget_dirmsg(BSOCK *bs)
139 {
140    int32_t n = BNET_TERMINATE;
141    char Job[MAX_NAME_LENGTH];
142    JobId_t JobId = 0;
143    char MsgType[20];
144    int type;
145    utime_t mtime;                     /* message time */
146    JCR *jcr = bs->jcr();
147    char *msg;
148
149    for ( ; !bs->is_stop() && !bs->is_timed_out(); ) {
150       n = bs->recv();
151       Dmsg4(200, "bget_dirmsg n=%d msglen=%ld is_stop=%d: %s\n", n, bs->msglen, bs->is_stop(), bs->msg);
152
153       if (bs->is_stop() || bs->is_timed_out()) {
154          return n;                    /* error or terminate */
155       }
156       if (n == BNET_SIGNAL) {          /* handle signal */
157          /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
158          switch (bs->msglen) {
159          case BNET_EOD:            /* end of data */
160             return n;
161          case BNET_EOD_POLL:
162             bs->fsend(OK_msg);/* send response */
163             return n;              /* end of data */
164          case BNET_TERMINATE:
165             bs->set_terminated();
166             return n;
167          case BNET_POLL:
168             bs->fsend(OK_msg); /* send response */
169             break;
170          case BNET_HEARTBEAT:
171 //          encode_time(time(NULL), Job);
172 //          Dmsg1(100, "%s got heartbeat.\n", Job);
173             break;
174          case BNET_HB_RESPONSE:
175             break;
176          case BNET_STATUS:
177             /* *****FIXME***** Implement more completely */
178             bs->fsend("Status OK\n");
179             bs->signal(BNET_EOD);
180             break;
181          case BNET_BTIME:             /* send Bacula time */
182             char ed1[50];
183             bs->fsend("btime %s\n", edit_uint64(get_current_btime(),ed1));
184             break;
185          default:
186             Jmsg1(jcr, M_WARNING, 0, _("bget_dirmsg: unknown bnet signal %d\n"), bs->msglen);
187             return n;
188          }
189          continue;
190       }
191
192       /* Handle normal data */
193
194       if (n > 0 && B_ISDIGIT(bs->msg[0])) {      /* response? */
195          return n;                    /* yes, return it */
196       }
197
198       /*
199        * If we get here, it must be a request.  Either
200        *  a message to dispatch, or a catalog request.
201        *  Try to fulfill it.
202        */
203       if ((sscanf(bs->msg, "%020s JobId=%ld ", MsgType, &JobId) != 2) &&
204           (sscanf(bs->msg, "%020s Job=%127s ", MsgType, Job) != 2) &&
205           (sscanf(bs->msg, "%020s Job=x", MsgType) != 1)) {
206          if (is_msgid(strchr(bs->msg, '['))) {
207             return n;
208          }
209          Jmsg1(jcr, M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
210          continue;
211       }
212
213       /* Skip past first two fields: "Jmsg JobId=nnn" */
214       if (!(msg=find_msg_start(bs->msg))) {
215          Jmsg1(jcr, M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
216          continue;
217       }
218
219       /*
220        * Here we are expecting a message of the following format:
221        *   Jmsg JobId=nnn type=nnn level=nnn Message-string
222        * Note, level should really be mtime, but that changes
223        *   the protocol.
224        */
225       if (bs->msg[0] == 'J') {           /* Job message */
226          if ((sscanf(bs->msg, "Jmsg JobId=%ld type=%d level=%lld",
227                      &JobId, &type, &mtime) != 3) &&
228              (sscanf(bs->msg, "Jmsg Job=%127s type=%d level=%lld",
229                                  Job, &type, &mtime) != 3)) {
230             Jmsg1(jcr, M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
231             continue;
232          }
233          Dmsg1(900, "Got msg: %s\n", bs->msg);
234          skip_spaces(&msg);
235          skip_nonspaces(&msg);        /* skip type=nnn */
236          skip_spaces(&msg);
237          skip_nonspaces(&msg);        /* skip level=nnn */
238          if (*msg == ' ') {
239             msg++;                    /* skip leading space */
240          }
241          Dmsg1(900, "Dispatch msg: %s", msg);
242          dispatch_message(jcr, type, mtime, msg);
243          continue;
244       }
245       /*
246        * Here we expact a CatReq message
247        *   CatReq JobId=nn Catalog-Request-Message
248        */
249       if (bs->msg[0] == 'C') {        /* Catalog request */
250          Dmsg2(900, "Catalog req jcr=%p: %s", jcr, bs->msg);
251          catalog_request(jcr, bs);
252          continue;
253       }
254       if (bs->msg[0] == 'U') {        /* SD sending attributes */
255          Dmsg2(900, "Catalog upd jcr=%p: %s", jcr, bs->msg);
256          catalog_update(jcr, bs);
257          continue;
258       }
259       if (bs->msg[0] == 'B') {        /* SD sending file spool attributes */
260          Dmsg2(100, "Blast attributes jcr=%p: %s", jcr, bs->msg);
261          char filename[256];
262          if (sscanf(bs->msg, "BlastAttr JobId=%ld File=%255s",
263                     &JobId, filename) != 2) {
264             Jmsg1(jcr, M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
265             continue;
266          }
267          unbash_spaces(filename);
268          if (despool_attributes_from_file(jcr, filename)) {
269             bs->fsend("1000 OK BlastAttr\n");
270          } else {
271             bs->fsend("1990 ERROR BlastAttr\n");
272          }
273          continue;
274       }
275       if (bs->msg[0] == 'M') {        /* Mount request */
276          Dmsg1(900, "Mount req: %s", bs->msg);
277          mount_request(jcr, bs, msg);
278          continue;
279       }
280       /* Get Progress: files, bytes, bytes/sec */
281       if (bs->msg[0] == 'P') {       /* Progress report */
282          uint32_t files, bps;
283          uint64_t bytes;
284          if ((sscanf(bs->msg, "Progress JobId=%ld files=%ld bytes=%lld bps=%ld\n",
285                 &JobId,  &files, &bytes, &bps) == 4) ||
286              (sscanf(bs->msg, "Progress JobId=x files=%ld bytes=%lld bps=%ld\n",
287                 &files, &bytes, &bps) == 3) ||
288              (sscanf(bs->msg, "Progress Job=x files=%ld bytes=%lld bps=%ld\n",
289                 &files, &bytes, &bps) == 3)) {
290             Dmsg2(900, "JobId=%d %s", jcr->JobId, bs->msg);
291             /* Save progress data */
292             jcr->JobFiles = files;
293             jcr->JobBytes = bytes;
294             jcr->LastRate = bps;
295          } else {
296             Jmsg1(jcr, M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
297          }
298          continue;
299       }
300       if (bs->msg[0] == 'S') {       /* Status change */
301          int JobStatus;
302          if (sscanf(bs->msg, Job_status, &JobId, &JobStatus) == 2) {
303             set_jcr_sd_job_status(jcr, JobStatus); /* current status */
304          } else {
305             Jmsg1(jcr, M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
306          }
307          continue;
308       }
309 #ifdef needed
310       /* No JCR for Device Updates! */
311       if (bs->msg[0] = 'D') {         /* Device update */
312          DEVICE *dev;
313          POOL_MEM dev_name, changer_name, media_type, volume_name;
314          int dev_open, dev_append, dev_read, dev_labeled;
315          int dev_offline, dev_autochanger, dev_autoselect;
316          int dev_num_writers, dev_max_writers, dev_reserved;
317          uint64_t dev_read_time, dev_write_time, dev_write_bytes, dev_read_bytes;
318          uint64_t dev_PoolId;
319          Dmsg1(100, "<stored: %s", bs->msg);
320          if (sscanf(bs->msg, Device_update,
321              &Job, dev_name.c_str(),
322              &dev_append, &dev_read,
323              &dev_num_writers, &dev_open,
324              &dev_labeled, &dev_offline, &dev_reserved,
325              &dev_max_writers, &dev_autoselect,
326              &dev_autochanger,
327              changer_name.c_str(), media_type.c_str(),
328              volume_name.c_str(),
329              &dev_read_time, &dev_write_time, &dev_read_bytes,
330              &dev_write_bytes) != 19) {
331             Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
332          } else {
333             unbash_spaces(dev_name);
334             dev = (DEVICE *)GetResWithName(R_DEVICE, dev_name.c_str());
335             if (!dev) {
336                continue;
337             }
338             unbash_spaces(changer_name);
339             unbash_spaces(media_type);
340             unbash_spaces(volume_name);
341             bstrncpy(dev->ChangerName, changer_name.c_str(), sizeof(dev->ChangerName));
342             bstrncpy(dev->MediaType, media_type.c_str(), sizeof(dev->MediaType));
343             bstrncpy(dev->VolumeName, volume_name.c_str(), sizeof(dev->VolumeName));
344             /* Note, these are copied because they are boolean rather than
345              *  integer.
346              */
347             dev->open = dev_open;
348             dev->append = dev_append;
349             dev->read = dev_read;
350             dev->labeled = dev_labeled;
351             dev->offline = dev_offline;
352             dev->autoselect = dev_autoselect;
353             dev->autochanger = dev_autochanger > 0;
354             dev->num_drives = dev_autochanger;    /* does double duty */
355             dev->PoolId = dev_PoolId;
356             dev->num_writers = dev_num_writers;
357             dev->max_writers = dev_max_writers;
358             dev->reserved = dev_reserved;
359             dev->found = true;
360             dev->DevReadTime = dev_read_time; /* TODO : have to update database */
361             dev->DevWriteTime = dev_write_time;
362             dev->DevReadBytes = dev_read_bytes;
363             dev->DevWriteBytes = dev_write_bytes;
364          }
365          continue;
366       }
367 #endif
368       return n;
369    }
370    return n;
371 }
372
373 static char *find_msg_start(char *msg)
374 {
375    char *p = msg;
376
377    skip_nonspaces(&p);                /* skip message type */
378    skip_spaces(&p);
379    skip_nonspaces(&p);                /* skip Job */
380    skip_spaces(&p);                   /* after spaces come the message */
381    return p;
382 }
383
384 /*
385  * Get response from FD or SD to a command we
386  * sent. Check that the response agrees with what we expect.
387  *
388  *  Returns: false on failure
389  *           true  on success
390  */
391 bool response(JCR *jcr, BSOCK *bs, char *resp, const char *cmd, e_prtmsg prtmsg)
392 {
393    int n;
394
395    if (bs->is_error()) {
396       return false;
397    }
398    if ((n = bget_dirmsg(bs)) >= 0) {
399       if (strcmp(bs->msg, resp) == 0) {
400          return true;
401       }
402       if (prtmsg == DISPLAY_ERROR) {
403          Jmsg(jcr, M_FATAL, 0, _("Bad response to %s command: wanted %s, got %s\n"),
404             cmd, resp, bs->msg);
405       }
406       return false;
407    }
408    Jmsg(jcr, M_FATAL, 0, _("Socket error on %s command: ERR=%s\n"),
409          cmd, bs->bstrerror());
410    return false;
411 }