]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_server.c
Add heap stats to Dir and SD -- eliminate #ifdefs
[bacula/bacula] / bacula / src / dird / ua_server.c
1 /*
2  *
3  *   Bacula Director -- User Agent Server
4  *
5  *     Kern Sibbald, September MM
6  *
7  *    Version $Id$
8  */
9
10 /*
11    Copyright (C) 2000-2004 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33 /* Imported subroutines */
34
35 /* Imported variables */
36 extern int r_first;
37 extern int r_last;
38 extern struct s_res resources[];
39 extern int console_msg_pending;
40 extern char my_name[];
41
42 /* Static variables */
43
44 /* Exported variables */
45 int quit_cmd_thread = 0;
46
47 /* Imported functions */
48
49 /* Forward referenced functions */
50
51 extern "C" void *connect_thread(void *arg);
52
53 static void *handle_UA_client_request(void *arg);
54
55
56 /* Global variables */
57 static int started = FALSE;
58 static workq_t ua_workq;
59
60 struct s_addr_port {
61    char *addr;
62    int port;
63 };
64
65 /* Called here by Director daemon to start UA (user agent)
66  * command thread. This routine creates the thread and then
67  * returns.
68  */
69 void start_UA_server(char *UA_addr, int UA_port)
70 {
71    pthread_t thid;
72    int status;
73    static struct s_addr_port arg;
74
75    arg.port = UA_port;
76    arg.addr = UA_addr;
77    if ((status=pthread_create(&thid, NULL, connect_thread, (void *)&arg)) != 0) {
78       Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), strerror(status));
79    }
80    started = TRUE;
81    return;
82 }
83
84 extern "C" 
85 void *connect_thread(void *arg)
86 {
87    struct s_addr_port *UA = (struct s_addr_port *)arg;
88
89    pthread_detach(pthread_self());
90
91    /*  ****FIXME**** put # 10 on config parameter */
92    bnet_thread_server(UA->addr, UA->port, 10, &ua_workq, handle_UA_client_request);
93    return NULL;
94 }
95
96 /*
97  * Create a Job Control Record for a control "job",
98  *   filling in all the appropriate fields.
99  */
100 JCR *new_control_jcr(const char *base_name, int job_type)
101 {
102    JCR *jcr;
103    jcr = new_jcr(sizeof(JCR), dird_free_jcr);
104    jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
105    create_unique_job_name(jcr, base_name);
106    jcr->sched_time = jcr->start_time;
107    jcr->JobType = job_type;
108    jcr->JobLevel = L_NONE;
109    jcr->JobStatus = JS_Running;
110    jcr->JobId = 0;
111    /*
112     * None of these are really defined for control JCRs, so we
113     * simply take the first of each one. This ensures that there
114     * will be no null pointer references.
115     */
116    LockRes();
117    jcr->job = (JOB *)GetNextRes(R_JOB, NULL);
118    jcr->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
119    jcr->client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
120    jcr->pool = (POOL *)GetNextRes(R_POOL, NULL);
121    jcr->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
122    jcr->store = (STORE *)GetNextRes(R_STORAGE, NULL);
123    jcr->fileset = (FILESET *)GetNextRes(R_FILESET, NULL);
124    UnlockRes();
125    return jcr;
126 }
127
128 /*
129  * Handle Director User Agent commands   
130  *
131  */
132 static void *handle_UA_client_request(void *arg)
133 {
134    int stat;
135    UAContext *ua;
136    JCR *jcr;
137    BSOCK *UA_sock = (BSOCK *)arg;
138
139    pthread_detach(pthread_self());
140
141    jcr = new_control_jcr("*Console*", JT_CONSOLE);
142
143    ua = new_ua_context(jcr);
144    ua->UA_sock = UA_sock;
145
146    bnet_recv(ua->UA_sock);          /* Get first message */
147    if (!authenticate_user_agent(ua)) {
148       goto getout;
149    }
150
151    while (!ua->quit) {
152       stat = bnet_recv(ua->UA_sock);
153       if (stat >= 0) {
154          pm_strcpy(&ua->cmd, ua->UA_sock->msg);
155          parse_ua_args(ua);
156          if (ua->argc > 0 && ua->argk[0][0] == '.') {
157             do_a_dot_command(ua, ua->cmd);
158          } else {
159             do_a_command(ua, ua->cmd);
160          }
161          if (!ua->quit) {
162             if (ua->auto_display_messages) {
163                strcpy(ua->cmd, "messages");
164                qmessagescmd(ua, ua->cmd);
165                ua->user_notified_msg_pending = FALSE;
166             } else if (!ua->user_notified_msg_pending && console_msg_pending) {
167                bsendmsg(ua, _("You have messages.\n"));
168                ua->user_notified_msg_pending = TRUE;
169             }
170             bnet_sig(ua->UA_sock, BNET_EOD); /* send end of command */
171          }
172       } else if (is_bnet_stop(ua->UA_sock)) {
173          ua->quit = TRUE;
174          break;
175       } else { /* signal */
176          bnet_sig(ua->UA_sock, BNET_POLL);
177       }
178    }
179
180 getout:
181
182    close_db(ua);
183    free_ua_context(ua);
184    free_jcr(jcr);
185
186    return NULL;
187 }
188
189 /*
190  * Create a UAContext for a Job that is running so that
191  *   it can the User Agent routines and    
192  *   to ensure that the Job gets the proper output.
193  *   This is a sort of mini-kludge, and should be
194  *   unified at some point.
195  */
196 UAContext *new_ua_context(JCR *jcr)
197 {
198    UAContext *ua;
199
200    ua = (UAContext *)malloc(sizeof(UAContext));
201    memset(ua, 0, sizeof(UAContext));
202    ua->jcr = jcr;
203    ua->db = jcr->db;
204    ua->cmd = get_pool_memory(PM_FNAME);
205    ua->args = get_pool_memory(PM_FNAME);
206    ua->verbose = 1;
207    ua->automount = TRUE;
208    return ua;
209 }
210
211 void free_ua_context(UAContext *ua)
212 {
213    if (ua->cmd) {
214       free_pool_memory(ua->cmd);
215    }
216    if (ua->args) {
217       free_pool_memory(ua->args);
218    }
219    if (ua->prompt) {
220       free(ua->prompt);
221    }
222
223    if (ua->UA_sock) {
224       bnet_close(ua->UA_sock);
225    }
226    free(ua);
227 }
228
229
230 /*
231  * Called from main Bacula thread 
232  */
233 void term_ua_server()
234 {
235    if (!started) {
236       return;
237    }
238    quit_cmd_thread = TRUE;
239 }