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.
7 * Kern Sibbald, August MM
9 * This routine runs as a thread and must be thread reentrant.
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.
24 Copyright (C) 2000-2005 Kern Sibbald
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.
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.
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,
46 /* Forward referenced functions */
47 static char *find_msg_start(char *msg);
49 static char Job_status[] = "Status Job=%127s JobStatus=%d\n";
52 static char OK_msg[] = "1000 OK\n";
56 * Call appropriate processing routine
57 * If it is not a Jmsg or a ReqCat message,
58 * return it to the caller.
60 * This routine is called to get the next message from
61 * another daemon. If the message is in canonical message
62 * format and the type is known, it will be dispatched
63 * to the appropriate handler. If the message is
64 * in any other format, it will be returned.
66 * E.g. any message beginning with a digit will be returned.
67 * any message beginning with Jmsg will be processed.
70 int bget_dirmsg(BSOCK *bs)
73 char Job[MAX_NAME_LENGTH];
81 Dmsg2(900, "bget_dirmsg %d: %s\n", n, bs->msg);
83 if (is_bnet_stop(bs)) {
84 return n; /* error or terminate */
86 if (n == BNET_SIGNAL) { /* handle signal */
87 /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
89 case BNET_EOD: /* end of data */
92 bnet_fsend(bs, OK_msg);/* send response */
93 return n; /* end of data */
98 bnet_fsend(bs, OK_msg); /* send response */
101 // encode_time(time(NULL), Job);
102 // Dmsg1(100, "%s got heartbeat.\n", Job);
104 case BNET_HB_RESPONSE:
107 /* *****FIXME***** Implement more completely */
108 bnet_fsend(bs, "Status OK\n");
109 bnet_sig(bs, BNET_EOD);
111 case BNET_BTIME: /* send Bacula time */
113 bnet_fsend(bs, "btime %s\n", edit_uint64(get_current_btime(),ed1));
116 Emsg1(M_WARNING, 0, _("bget_dirmsg: unknown bnet signal %d\n"), bs->msglen);
122 /* Handle normal data */
124 if (n > 0 && B_ISDIGIT(bs->msg[0])) { /* response? */
125 return n; /* yes, return it */
129 * If we get here, it must be a request. Either
130 * a message to dispatch, or a catalog request.
133 if (sscanf(bs->msg, "%020s Job=%127s ", MsgType, Job) != 2) {
134 Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
137 if (!(jcr=get_jcr_by_full_name(Job))) {
138 Emsg1(M_ERROR, 0, _("Job not found: %s\n"), bs->msg);
141 Dmsg1(900, "Getmsg got jcr 0x%x\n", jcr);
143 /* Skip past "Jmsg Job=nnn" */
144 if (!(msg=find_msg_start(bs->msg))) {
145 Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
151 * Here we are expecting a message of the following format:
152 * Jmsg Job=nnn type=nnn level=nnn Message-string
154 if (bs->msg[0] == 'J') { /* Job message */
155 if (sscanf(bs->msg, "Jmsg Job=%127s type=%d level=%d",
156 Job, &type, &level) != 3) {
157 Emsg1(M_ERROR, 0, _("Malformed message: %s\n"), bs->msg);
161 Dmsg1(900, "Got msg: %s\n", bs->msg);
163 skip_nonspaces(&msg); /* skip type=nnn */
165 skip_nonspaces(&msg); /* skip level=nnn */
167 msg++; /* skip leading space */
169 Dmsg1(900, "Dispatch msg: %s", msg);
170 dispatch_message(jcr, type, level, msg);
175 * Here we expact a CatReq message
176 * CatReq Job=nn Catalog-Request-Message
178 if (bs->msg[0] == 'C') { /* Catalog request */
179 Dmsg2(900, "Catalog req jcr 0x%x: %s", jcr, bs->msg);
180 catalog_request(jcr, bs, msg);
181 Dmsg1(900, "Calling freejcr 0x%x\n", jcr);
185 if (bs->msg[0] == 'U') { /* Catalog update */
186 Dmsg2(900, "Catalog upd jcr 0x%x: %s", jcr, bs->msg);
187 catalog_update(jcr, bs, msg);
188 Dmsg1(900, "Calling freejcr 0x%x\n", jcr);
192 if (bs->msg[0] == 'M') { /* Mount request */
193 Dmsg1(900, "Mount req: %s", bs->msg);
194 mount_request(jcr, bs, msg);
198 if (bs->msg[0] == 'S') { /* Status change */
200 char Job[MAX_NAME_LENGTH];
201 if (sscanf(bs->msg, Job_status, &Job, &JobStatus) == 2) {
202 jcr->SDJobStatus = JobStatus; /* current status */
211 static char *find_msg_start(char *msg)
215 skip_nonspaces(&p); /* skip message type */
217 skip_nonspaces(&p); /* skip Job */
218 skip_spaces(&p); /* after spaces come the message */
223 * Get response from FD or SD to a command we
224 * sent. Check that the response agrees with what we expect.
226 * Returns: false on failure
229 bool response(JCR *jcr, BSOCK *bs, char *resp, const char *cmd, e_prtmsg prtmsg)
233 if (is_bnet_error(bs)) {
236 if ((n = bget_dirmsg(bs)) >= 0) {
238 if (strcmp(bs->msg, resp) == 0) {
241 Dmsg1(900, "Bad response: ERR=%s", bs->msg);
242 if (prtmsg == DISPLAY_ERROR) {
243 Jmsg(jcr, M_FATAL, 0, _("Bad response to %s command: wanted %s got: %s\n"),
248 Jmsg(jcr, M_FATAL, 0, _("Socket error on %s command: ERR=%s\n"),
249 cmd, bnet_strerror(bs));