]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_server.c
New var.c file + implement multiple simultaneous jobs
[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-2003 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 static void *connect_thread(void *arg);
52 static void *handle_UA_client_request(void *arg);
53
54
55 /* Global variables */
56 static int started = FALSE;
57 static workq_t ua_workq;
58
59 struct s_addr_port {
60    char *addr;
61    int port;
62 };
63
64 /* Called here by Director daemon to start UA (user agent)
65  * command thread. This routine creates the thread and then
66  * returns.
67  */
68 void start_UA_server(char *UA_addr, int UA_port)
69 {
70    pthread_t thid;
71    int status;
72    static struct s_addr_port arg;
73
74    arg.port = UA_port;
75    arg.addr = UA_addr;
76    if ((status=pthread_create(&thid, NULL, connect_thread, (void *)&arg)) != 0) {
77       Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), strerror(status));
78    }
79    started = TRUE;
80    return;
81 }
82
83 static void *connect_thread(void *arg)
84 {
85    struct s_addr_port *UA = (struct s_addr_port *)arg;
86
87    pthread_detach(pthread_self());
88
89    /*  ****FIXME**** put # 5 on config parameter */
90    bnet_thread_server(UA->addr, UA->port, 5, &ua_workq, handle_UA_client_request);
91    return NULL;
92 }
93
94 /*
95  * Handle Director User Agent commands   
96  *
97  */
98 static void *handle_UA_client_request(void *arg)
99 {
100    int stat;
101    UAContext ua;
102    BSOCK *UA_sock = (BSOCK *) arg;
103
104    pthread_detach(pthread_self());
105
106    memset(&ua, 0, sizeof(ua));
107    ua.automount = TRUE;
108    ua.verbose = 1;
109    ua.jcr = new_jcr(sizeof(JCR), dird_free_jcr);
110    ua.jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
111    ua.UA_sock = UA_sock;
112    ua.cmd = get_pool_memory(PM_FNAME);
113    ua.args = get_pool_memory(PM_FNAME);
114
115    create_unique_job_name(ua.jcr, "*Console*");
116    ua.jcr->sched_time = ua.jcr->start_time;
117    ua.jcr->JobType = JT_CONSOLE;
118
119    bnet_recv(ua.UA_sock);          /* Get first message */
120    if (!authenticate_user_agent(ua.UA_sock)) {
121       goto getout;
122    }
123
124    while (!ua.quit) {
125       stat = bnet_recv(ua.UA_sock);
126       if (stat >= 0) {
127          ua.cmd = check_pool_memory_size(ua.cmd, ua.UA_sock->msglen+1);
128          bstrncpy(ua.cmd, ua.UA_sock->msg, ua.UA_sock->msglen+1);
129          parse_ua_args(&ua);
130          if (ua.argc > 0 && ua.argk[0][0] == '.') {
131             do_a_dot_command(&ua, ua.cmd);
132          } else {
133             do_a_command(&ua, ua.cmd);
134          }
135          if (!ua.quit) {
136             if (ua.auto_display_messages) {
137                strcpy(ua.cmd, "messages");
138                qmessagescmd(&ua, ua.cmd);
139                ua.user_notified_msg_pending = FALSE;
140             } else if (!ua.user_notified_msg_pending && console_msg_pending) {
141                bsendmsg(&ua, _("You have messages.\n"));
142                ua.user_notified_msg_pending = TRUE;
143             }
144             bnet_sig(ua.UA_sock, BNET_EOD); /* send end of command */
145          }
146       } else if (is_bnet_stop(ua.UA_sock)) {
147          ua.quit = TRUE;
148          break;
149       } else { /* signal */
150          bnet_sig(ua.UA_sock, BNET_POLL);
151       }
152    }
153
154 getout:
155    if (ua.UA_sock) {
156       bnet_close(ua.UA_sock);
157       ua.UA_sock = NULL;
158    }
159
160    close_db(&ua);                     /* do this before freeing JCR */
161
162    if (ua.jcr) {
163       free_jcr(ua.jcr);
164       ua.jcr = NULL;
165    }
166    if (ua.prompt) {
167       free(ua.prompt);
168    }
169    if (ua.cmd) {
170       free_pool_memory(ua.cmd);
171    }
172    if (ua.args) {
173       free_pool_memory(ua.args);
174    }
175    return NULL;
176 }
177
178 /*
179  * Called from main Bacula thread 
180  */
181 void term_ua_server()
182 {
183    if (!started) {
184       return;
185    }
186    quit_cmd_thread = TRUE;
187 }