]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/getmsg.c
ca6273cc02aa0697aaf84ebb54712e067627e5e4
[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  */
64 int32_t bget_msg(BSOCK *bs, int rtn)
65 {
66    int32_t n;
67    char Job[MAX_NAME_LENGTH];
68    char MsgType[20];
69    int type, level;
70    JCR *jcr;
71    char *msg;
72
73    for (;;) {
74       n = bnet_recv(bs);
75       Dmsg2(120, "bget_msg %d: %s\n", n, bs->msg);
76
77       if (n < 0) {
78          return n;                    /* error return */
79       }
80       if (n == 0) {                   /* handle signal */
81          /* 0 return from bnet_recv() => network signal */
82          switch (bs->msglen) {
83             case BNET_EOF:               /* deprecated */
84             case BNET_EOD:               /* end of data */
85                return 0;
86             case BNET_EOD_POLL:
87                bnet_fsend(bs, OK_msg);   /* send response */
88                return 0;                 /* end of data */
89             case BNET_TERMINATE:
90                bs->terminated = 1;
91                return 0;
92             case BNET_POLL:
93                bnet_fsend(bs, OK_msg);   /* send response */
94                break;
95             case BNET_HEARTBEAT:
96                bnet_sig(bs, BNET_HB_RESPONSE);
97                break;
98             case BNET_STATUS:
99                /* *****FIXME***** Implement */
100                bnet_fsend(bs, "Status OK\n");
101                bnet_sig(bs, BNET_EOD);
102                break;
103             default:
104                Emsg1(M_WARNING, 0, _("bget_msg: unknown signal %d\n"), bs->msglen);
105                return 0;
106          }
107          continue;
108       }
109      
110       /* Handle normal data */
111
112       if (ISDIGIT(bs->msg[0])) {      /* response? */
113          return n;                    /* yes, return it */
114       }
115         
116       /*
117        * If we get here, it must be a request.  Either
118        *  a message to dispatch, or a catalog request.
119        *  Try to fulfill it.
120        */
121       if (sscanf(bs->msg, "%020s Job=%127s ", MsgType, Job) != 2) {
122          Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
123          continue;
124       }
125       if (!(jcr=get_jcr_by_full_name(Job))) {
126          Emsg1(M_ERROR, 0, _("Job not found: %s\n"), bs->msg);
127          continue;
128       }
129
130       /* Skip past "Jmsg Job=nnn" */
131       if (!(msg=find_msg_start(bs->msg))) {
132          Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
133          free_jcr(jcr);
134          continue;
135       }
136
137       /* 
138        * Here we are expecting a message of the following format:
139        *   Jmsg Job=nnn type=nnn level=nnn Message-string
140        */
141       if (bs->msg[0] == 'J') {           /* Job message */
142          if (sscanf(bs->msg, "Jmsg Job=%127s type=%d level=%d", 
143                     Job, &type, &level) != 3) {
144             Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
145             free_jcr(jcr);
146             continue;
147          }
148          Dmsg1(120, "Got msg: %s\n", bs->msg);
149          skip_spaces(&msg);
150          skip_nonspaces(&msg);        /* skip type=nnn */
151          skip_spaces(&msg);
152          skip_nonspaces(&msg);        /* skip level=nnn */
153          if (*msg == ' ') {
154             msg++;                    /* skip leading space */
155          }
156          Dmsg1(120, "Dispatch msg: %s", msg);
157          dispatch_message(jcr, type, level, msg);
158          free_jcr(jcr);
159          continue;
160       }
161       /* 
162        * Here we expact a CatReq message
163        *   CatReq Job=nn Catalog-Request-Message
164        */
165       if (bs->msg[0] == 'C') {        /* Catalog request */
166          Dmsg1(120, "Catalog req: %s", bs->msg);
167          catalog_request(jcr, bs, msg);
168          free_jcr(jcr);
169          continue;
170       }
171       if (bs->msg[0] == 'U') {        /* Catalog update */
172          catalog_update(jcr, bs, msg);
173          free_jcr(jcr);
174          continue;
175       }
176       if (bs->msg[0] == 'M') {        /* Mount request */
177          Dmsg1(120, "Mount req: %s", bs->msg);
178          mount_request(jcr, bs, msg);
179          free_jcr(jcr);
180          continue;
181       }
182       return n;
183    }
184 }
185
186 static char *find_msg_start(char *msg)
187 {
188    char *p = msg;
189
190    skip_nonspaces(&p);                /* skip message type */
191    skip_spaces(&p);
192    skip_nonspaces(&p);                /* skip Job */
193    skip_spaces(&p);                   /* after spaces come the message */
194    return p;
195 }
196
197 /*
198  * Get response from File daemon to a command we
199  * sent. Check that the response agrees with what we expect.
200  *
201  *  Returns: 0 on failure
202  *           1 on success
203  */
204 int response(BSOCK *fd, char *resp, char *cmd)
205 {
206    int n;
207
208    if (fd->errors) {
209       return 0;
210    }
211    if ((n = bget_msg(fd, 0)) > 0) {
212       Dmsg0(10, fd->msg);
213       if (strcmp(fd->msg, resp) == 0) {
214          return 1;
215       }
216       Emsg3(M_FATAL, 0, _("<filed: bad response to %s command: wanted %s got: %s\n"),
217          cmd, resp, fd->msg);
218       return 0;
219    } 
220    Emsg2(M_FATAL, 0, _("<filed: Socket error from Filed on %s command: ERR=%s\n"),
221          cmd, bnet_strerror(fd));
222    return 0;
223 }