]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/getmsg.c
Fix list nextvol to find Pool from run record
[bacula/bacula] / bacula / src / dird / getmsg.c
1 /*
2  *
3  *   Bacula Director -- routines to receive network data and
4  *    handle network signals. These routines handle the connections
5  *    to the Storage daemon and the File daemon.
6  *
7  *     Kern Sibbald, August MM
8  *
9  *    This routine runs as a thread and must be thread reentrant.
10  *
11  *  Basic tasks done here:
12  *    Handle  network signals (signals).  
13  *       Signals always have return status 0 from bnet_recv() and
14  *       a zero or negative message length.
15  *    Pass appropriate messages back to the caller (responses).  
16  *       Responses always have a digit as the first character.
17  *    Handle requests for message and catalog services (requests).  
18  *       Requests are any message that does not begin with a digit.
19  *       In affect, they are commands.
20  *
21  *   Version $Id$
22  */
23 /*
24    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
25
26    This program is free software; you can redistribute it and/or
27    modify it under the terms of the GNU General Public License as
28    published by the Free Software Foundation; either version 2 of
29    the License, or (at your option) any later version.
30
31    This program is distributed in the hope that it will be useful,
32    but WITHOUT ANY WARRANTY; without even the implied warranty of
33    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34    General Public License for more details.
35
36    You should have received a copy of the GNU General Public
37    License along with this program; if not, write to the Free
38    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
39    MA 02111-1307, USA.
40
41  */
42
43 #include "bacula.h"
44 #include "dird.h"
45
46 /* Forward referenced functions */
47 static char *find_msg_start(char *msg);
48
49 static char OK_msg[] = "1000 OK\n";
50
51 /*
52  * Get a message
53  *  Call appropriate processing routine
54  *  If it is not a Jmsg or a ReqCat message,
55  *   return it to the caller.
56  *
57  *  This routine is called to get the next message from
58  *  another daemon. If the message is in canonical message
59  *  format and the type is known, it will be dispatched
60  *  to the appropriate handler.  If the message is
61  *  in any other format, it will be returned. 
62  *
63  *  E.g. any message beginning with a digit will be returned.
64  *       any message beginning with Jmsg will be processed.
65  *
66  */
67 int bget_dirmsg(BSOCK *bs)
68 {
69    int32_t n;
70    char Job[MAX_NAME_LENGTH];
71    char MsgType[20];
72    int type, level;
73    JCR *jcr;
74    char *msg;
75
76    for (;;) {
77       n = bnet_recv(bs);
78       Dmsg2(120, "bget_dirmsg %d: %s\n", n, bs->msg);
79
80       if (is_bnet_stop(bs)) {
81          return n;                    /* error or terminate */
82       }
83       if (n == BNET_SIGNAL) {          /* handle signal */
84          /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
85          switch (bs->msglen) {
86          case BNET_EOD:            /* end of data */
87             return n;
88          case BNET_EOD_POLL:
89             bnet_fsend(bs, OK_msg);/* send response */
90             return n;              /* end of data */
91          case BNET_TERMINATE:
92             bs->terminated = 1;
93             return n;
94          case BNET_POLL:
95             bnet_fsend(bs, OK_msg); /* send response */
96             break;
97          case BNET_HEARTBEAT:
98 //          encode_time(time(NULL), Job);
99 //          Dmsg1(000, "%s got heartbeat.\n", Job);
100             break;
101          case BNET_HB_RESPONSE:
102             break;
103          case BNET_STATUS:
104             /* *****FIXME***** Implement more completely */
105             bnet_fsend(bs, "Status OK\n");
106             bnet_sig(bs, BNET_EOD);
107             break;
108          default:
109             Emsg1(M_WARNING, 0, _("bget_dirmsg: unknown bnet signal %d\n"), bs->msglen);
110             return n;
111          }
112          continue;
113       }
114      
115       /* Handle normal data */
116
117       if (n > 0 && B_ISDIGIT(bs->msg[0])) {      /* response? */
118          return n;                    /* yes, return it */
119       }
120         
121       /*
122        * If we get here, it must be a request.  Either
123        *  a message to dispatch, or a catalog request.
124        *  Try to fulfill it.
125        */
126       if (sscanf(bs->msg, "%020s Job=%127s ", MsgType, Job) != 2) {
127          Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
128          continue;
129       }
130       if (!(jcr=get_jcr_by_full_name(Job))) {
131          Emsg1(M_ERROR, 0, _("Job not found: %s\n"), bs->msg);
132          continue;
133       }
134       Dmsg1(200, "Getmsg got jcr 0x%x\n", jcr);
135
136       /* Skip past "Jmsg Job=nnn" */
137       if (!(msg=find_msg_start(bs->msg))) {
138          Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
139          free_jcr(jcr);
140          continue;
141       }
142
143       /* 
144        * Here we are expecting a message of the following format:
145        *   Jmsg Job=nnn type=nnn level=nnn Message-string
146        */
147       if (bs->msg[0] == 'J') {           /* Job message */
148          if (sscanf(bs->msg, "Jmsg Job=%127s type=%d level=%d", 
149                     Job, &type, &level) != 3) {
150             Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
151             free_jcr(jcr);
152             continue;
153          }
154          Dmsg1(120, "Got msg: %s\n", bs->msg);
155          skip_spaces(&msg);
156          skip_nonspaces(&msg);        /* skip type=nnn */
157          skip_spaces(&msg);
158          skip_nonspaces(&msg);        /* skip level=nnn */
159          if (*msg == ' ') {
160             msg++;                    /* skip leading space */
161          }
162          Dmsg1(120, "Dispatch msg: %s", msg);
163          dispatch_message(jcr, type, level, msg);
164          free_jcr(jcr);
165          continue;
166       }
167       /* 
168        * Here we expact a CatReq message
169        *   CatReq Job=nn Catalog-Request-Message
170        */
171       if (bs->msg[0] == 'C') {        /* Catalog request */
172          Dmsg2(120, "Catalog req jcr 0x%x: %s", jcr, bs->msg);
173          catalog_request(jcr, bs, msg);
174          Dmsg1(200, "Calling freejcr 0x%x\n", jcr);
175          free_jcr(jcr);
176          continue;
177       }
178       if (bs->msg[0] == 'U') {        /* Catalog update */
179          Dmsg2(120, "Catalog upd jcr 0x%x: %s", jcr, bs->msg);
180          catalog_update(jcr, bs, msg);
181          Dmsg1(200, "Calling freejcr 0x%x\n", jcr);
182          free_jcr(jcr);
183          continue;
184       }
185       if (bs->msg[0] == 'M') {        /* Mount request */
186          Dmsg1(120, "Mount req: %s", bs->msg);
187          mount_request(jcr, bs, msg);
188          free_jcr(jcr);
189          continue;
190       }
191       return n;
192    }
193 }
194
195 static char *find_msg_start(char *msg)
196 {
197    char *p = msg;
198
199    skip_nonspaces(&p);                /* skip message type */
200    skip_spaces(&p);
201    skip_nonspaces(&p);                /* skip Job */
202    skip_spaces(&p);                   /* after spaces come the message */
203    return p;
204 }
205
206 /*
207  * Get response from File daemon to a command we
208  * sent. Check that the response agrees with what we expect.
209  *
210  *  Returns: 0 on failure
211  *           1 on success
212  */
213 int response(JCR *jcr, BSOCK *fd, char *resp, char *cmd, e_prtmsg prtmsg)
214 {
215    int n;
216
217    if (is_bnet_error(fd)) {
218       return 0;
219    }
220    if ((n = bget_dirmsg(fd)) >= 0) {
221       Dmsg0(110, fd->msg);
222       if (strcmp(fd->msg, resp) == 0) {
223          return 1;
224       }
225       if (prtmsg == DISPLAY_ERROR) {
226          Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to %s command: wanted %s got: %s\n"),
227             cmd, resp, fd->msg);
228       }
229       return 0;
230    } 
231    Jmsg(jcr, M_FATAL, 0, _("Socket error from Filed on %s command: ERR=%s\n"),
232          cmd, bnet_strerror(fd));
233    return 0;
234 }