3 * Bacula Director -- User Agent Server
5 * Kern Sibbald, September MM
9 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* Imported subroutines */
33 extern void run_job(JCR *jcr);
35 /* Imported variables */
36 extern struct s_jl joblevels[];
39 extern struct s_res resources[];
40 extern int console_msg_pending;
42 extern char my_name[];
44 /* Static variables */
46 /* Exported variables */
47 int quit_cmd_thread = 0;
49 /* Imported functions */
51 /* Forward referenced functions */
53 static void *connect_thread(void *arg);
54 static void handle_UA_client_request(void *arg);
57 /* Global variables */
58 static int started = FALSE;
59 static workq_t ua_workq;
61 /* Called here by Director daemon to start UA (user agent)
62 * command thread. This routine creates the thread and then
65 void start_UA_server(int UA_port)
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));
78 static void *connect_thread(void *arg)
80 int UA_port = (int)arg;
82 pthread_detach(pthread_self());
84 bnet_thread_server(UA_port, 5, &ua_workq, handle_UA_client_request);
89 * Handle Director User Agent commands
92 static void handle_UA_client_request(void *arg)
95 static char cmd[1000];
97 BSOCK *UA_sock = (BSOCK *) arg;
99 pthread_detach(pthread_self());
101 memset(&ua, 0, sizeof(ua));
103 ua.jcr = new_jcr(sizeof(JCR), dird_free_jcr);
104 close_msg(ua.jcr); /* we don't handle messages */
105 ua.jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
106 ua.UA_sock = UA_sock;
107 ua.cmd = (char *) get_pool_memory(PM_FNAME);
108 ua.args = (char *) get_pool_memory(PM_FNAME);
110 create_unique_job_name(ua.jcr, "*Console*");
111 ua.jcr->sched_time = ua.jcr->start_time;
112 ua.jcr->JobType = JT_CONSOLE;
114 bnet_recv(ua.UA_sock); /* Get first message */
115 if (!authenticate_user_agent(ua.UA_sock)) {
121 stat = bnet_recv(ua.UA_sock);
123 strncpy(cmd, ua.UA_sock->msg, sizeof(cmd));
124 cmd[sizeof(cmd)-1] = 0; /* ensure it is terminated/trucated */
125 parse_command_args(&ua);
126 if (ua.argc > 0 && ua.argk[0][0] == '.') {
127 quit = !do_a_dot_command(&ua, cmd);
129 quit = !do_a_command(&ua, cmd);
132 if (ua.auto_display_messages) {
133 strcpy(cmd, "messages");
134 qmessagescmd(&ua, cmd);
135 ua.user_notified_msg_pending = FALSE;
136 } else if (!ua.user_notified_msg_pending && console_msg_pending) {
137 bsendmsg(&ua, _("You have messages.\n"));
138 ua.user_notified_msg_pending = TRUE;
140 bnet_sig(ua.UA_sock, BNET_EOD); /* send end of command */
142 } else if (stat == 0) {
143 if (ua.UA_sock->msglen == BNET_TERMINATE) {
146 bnet_sig(ua.UA_sock, BNET_POLL);
148 break; /* error, exit */
154 bnet_close(ua.UA_sock);
167 free_pool_memory(ua.cmd);
170 free_pool_memory(ua.args);
176 * Called from main Bacula thread
178 void term_ua_server()
183 quit_cmd_thread = TRUE;