]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/getmsg.c
This commit was manufactured by cvs2svn to create tag
[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-2005 Kern Sibbald
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 Job_status[] = "Status Job=%127s JobStatus=%d\n";
50
51 static char OK_msg[] = "1000 OK\n";
52
53 /*
54  * Get a message
55  *  Call appropriate processing routine
56  *  If it is not a Jmsg or a ReqCat message,
57  *   return it to the caller.
58  *
59  *  This routine is called to get the next message from
60  *  another daemon. If the message is in canonical message
61  *  format and the type is known, it will be dispatched
62  *  to the appropriate handler.  If the message is
63  *  in any other format, it will be returned. 
64  *
65  *  E.g. any message beginning with a digit will be returned.
66  *       any message beginning with Jmsg will be processed.
67  *
68  */
69 int bget_dirmsg(BSOCK *bs)
70 {
71    int32_t n;
72    char Job[MAX_NAME_LENGTH];
73    char MsgType[20];
74    int type, level;
75    JCR *jcr;
76    char *msg;
77
78    for (;;) {
79       n = bnet_recv(bs);
80       Dmsg2(120, "bget_dirmsg %d: %s\n", n, bs->msg);
81
82       if (is_bnet_stop(bs)) {
83          return n;                    /* error or terminate */
84       }
85       if (n == BNET_SIGNAL) {          /* handle signal */
86          /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
87          switch (bs->msglen) {
88          case BNET_EOD:            /* end of data */
89             return n;
90          case BNET_EOD_POLL:
91             bnet_fsend(bs, OK_msg);/* send response */
92             return n;              /* end of data */
93          case BNET_TERMINATE:
94             bs->terminated = 1;
95             return n;
96          case BNET_POLL:
97             bnet_fsend(bs, OK_msg); /* send response */
98             break;
99          case BNET_HEARTBEAT:
100 //          encode_time(time(NULL), Job);
101 //          Dmsg1(100, "%s got heartbeat.\n", Job);
102             break;
103          case BNET_HB_RESPONSE:
104             break;
105          case BNET_STATUS:
106             /* *****FIXME***** Implement more completely */
107             bnet_fsend(bs, "Status OK\n");
108             bnet_sig(bs, BNET_EOD);
109             break;
110          case BNET_BTIME:             /* send Bacula time */
111             char ed1[50];
112             bnet_fsend(bs, "btime %s\n", edit_uint64(get_current_btime(),ed1));
113             break;
114          default:
115             Emsg1(M_WARNING, 0, _("bget_dirmsg: unknown bnet signal %d\n"), bs->msglen);
116             return n;
117          }
118          continue;
119       }
120      
121       /* Handle normal data */
122
123       if (n > 0 && B_ISDIGIT(bs->msg[0])) {      /* response? */
124          return n;                    /* yes, return it */
125       }
126         
127       /*
128        * If we get here, it must be a request.  Either
129        *  a message to dispatch, or a catalog request.
130        *  Try to fulfill it.
131        */
132       if (sscanf(bs->msg, "%020s Job=%127s ", MsgType, Job) != 2) {
133          Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
134          continue;
135       }
136       if (!(jcr=get_jcr_by_full_name(Job))) {
137          Emsg1(M_ERROR, 0, _("Job not found: %s\n"), bs->msg);
138          continue;
139       }
140       Dmsg1(200, "Getmsg got jcr 0x%x\n", jcr);
141
142       /* Skip past "Jmsg Job=nnn" */
143       if (!(msg=find_msg_start(bs->msg))) {
144          Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
145          free_jcr(jcr);
146          continue;
147       }
148
149       /* 
150        * Here we are expecting a message of the following format:
151        *   Jmsg Job=nnn type=nnn level=nnn Message-string
152        */
153       if (bs->msg[0] == 'J') {           /* Job message */
154          if (sscanf(bs->msg, "Jmsg Job=%127s type=%d level=%d", 
155                     Job, &type, &level) != 3) {
156             Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
157             free_jcr(jcr);
158             continue;
159          }
160          Dmsg1(120, "Got msg: %s\n", bs->msg);
161          skip_spaces(&msg);
162          skip_nonspaces(&msg);        /* skip type=nnn */
163          skip_spaces(&msg);
164          skip_nonspaces(&msg);        /* skip level=nnn */
165          if (*msg == ' ') {
166             msg++;                    /* skip leading space */
167          }
168          Dmsg1(120, "Dispatch msg: %s", msg);
169          dispatch_message(jcr, type, level, msg);
170          free_jcr(jcr);
171          continue;
172       }
173       /* 
174        * Here we expact a CatReq message
175        *   CatReq Job=nn Catalog-Request-Message
176        */
177       if (bs->msg[0] == 'C') {        /* Catalog request */
178          Dmsg2(120, "Catalog req jcr 0x%x: %s", jcr, bs->msg);
179          catalog_request(jcr, bs, msg);
180          Dmsg1(200, "Calling freejcr 0x%x\n", jcr);
181          free_jcr(jcr);
182          continue;
183       }
184       if (bs->msg[0] == 'U') {        /* Catalog update */
185          Dmsg2(120, "Catalog upd jcr 0x%x: %s", jcr, bs->msg);
186          catalog_update(jcr, bs, msg);
187          Dmsg1(200, "Calling freejcr 0x%x\n", jcr);
188          free_jcr(jcr);
189          continue;
190       }
191       if (bs->msg[0] == 'M') {        /* Mount request */
192          Dmsg1(120, "Mount req: %s", bs->msg);
193          mount_request(jcr, bs, msg);
194          free_jcr(jcr);
195          continue;
196       }
197       if (bs->msg[0] == 'S') {       /* Status change */
198         int JobStatus;
199         char Job[MAX_NAME_LENGTH];
200         if (sscanf(bs->msg, Job_status, &Job, &JobStatus) == 2) {
201            jcr->SDJobStatus = JobStatus; /* current status */
202            free_jcr(jcr);
203            continue;
204         }
205       }
206       return n;
207    }
208 }
209
210 static char *find_msg_start(char *msg)
211 {
212    char *p = msg;
213
214    skip_nonspaces(&p);                /* skip message type */
215    skip_spaces(&p);
216    skip_nonspaces(&p);                /* skip Job */
217    skip_spaces(&p);                   /* after spaces come the message */
218    return p;
219 }
220
221 /*
222  * Get response from File daemon to a command we
223  * sent. Check that the response agrees with what we expect.
224  *
225  *  Returns: 0 on failure
226  *           1 on success
227  */
228 int response(JCR *jcr, BSOCK *fd, char *resp, const char *cmd, e_prtmsg prtmsg)
229 {
230    int n;
231
232    if (is_bnet_error(fd)) {
233       return 0;
234    }
235    if ((n = bget_dirmsg(fd)) >= 0) {
236       Dmsg0(110, fd->msg);
237       if (strcmp(fd->msg, resp) == 0) {
238          return 1;
239       }
240       if (prtmsg == DISPLAY_ERROR) {
241          Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to %s command: wanted %s got: %s\n"),
242             cmd, resp, fd->msg);
243       }
244       return 0;
245    } 
246    Jmsg(jcr, M_FATAL, 0, _("Socket error from Filed on %s command: ERR=%s\n"),
247          cmd, bnet_strerror(fd));
248    return 0;
249 }