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