3 * Bacula Director -- User Agent Server
5 * Kern Sibbald, September MM
11 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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.
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.
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,
33 /* Imported subroutines */
35 /* Imported variables */
38 extern struct s_res resources[];
39 extern int console_msg_pending;
40 extern char my_name[];
42 /* Static variables */
44 /* Exported variables */
45 int quit_cmd_thread = 0;
47 /* Imported functions */
49 /* Forward referenced functions */
51 extern "C" void *connect_thread(void *arg);
53 static void *handle_UA_client_request(void *arg);
56 /* Global variables */
57 static int started = FALSE;
58 static workq_t ua_workq;
65 /* Called here by Director daemon to start UA (user agent)
66 * command thread. This routine creates the thread and then
69 void start_UA_server(dlist *addrs)
73 static dlist *myaddrs = addrs;
75 if ((status=pthread_create(&thid, NULL, connect_thread, (void *)myaddrs)) != 0) {
78 Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.strerror());
85 void *connect_thread(void *arg)
87 pthread_detach(pthread_self());
89 /* ****FIXME**** put # 10 (timeout) on config parameter */
90 bnet_thread_server((dlist*)arg, 10, &ua_workq, handle_UA_client_request);
95 * Create a Job Control Record for a control "job",
96 * filling in all the appropriate fields.
98 JCR *new_control_jcr(const char *base_name, int job_type)
101 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
102 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
103 create_unique_job_name(jcr, base_name);
104 jcr->sched_time = jcr->start_time;
105 jcr->JobType = job_type;
106 jcr->JobLevel = L_NONE;
107 jcr->JobStatus = JS_Running;
110 * None of these are really defined for control JCRs, so we
111 * simply take the first of each one. This ensures that there
112 * will be no null pointer references.
115 jcr->job = (JOB *)GetNextRes(R_JOB, NULL);
116 jcr->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
117 jcr->client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
118 jcr->pool = (POOL *)GetNextRes(R_POOL, NULL);
119 jcr->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
120 jcr->store = (STORE *)GetNextRes(R_STORAGE, NULL);
121 jcr->fileset = (FILESET *)GetNextRes(R_FILESET, NULL);
127 * Handle Director User Agent commands
130 static void *handle_UA_client_request(void *arg)
135 BSOCK *UA_sock = (BSOCK *)arg;
137 pthread_detach(pthread_self());
139 jcr = new_control_jcr("*Console*", JT_CONSOLE);
141 ua = new_ua_context(jcr);
142 ua->UA_sock = UA_sock;
144 bnet_recv(ua->UA_sock); /* Get first message */
145 if (!authenticate_user_agent(ua)) {
150 stat = bnet_recv(ua->UA_sock);
152 pm_strcpy(ua->cmd, ua->UA_sock->msg);
154 if (ua->argc > 0 && ua->argk[0][0] == '.') {
155 do_a_dot_command(ua, ua->cmd);
157 do_a_command(ua, ua->cmd);
160 if (ua->auto_display_messages) {
161 strcpy(ua->cmd, "messages");
162 qmessagescmd(ua, ua->cmd);
163 ua->user_notified_msg_pending = FALSE;
164 } else if (!ua->user_notified_msg_pending && console_msg_pending) {
165 bsendmsg(ua, _("You have messages.\n"));
166 ua->user_notified_msg_pending = TRUE;
168 bnet_sig(ua->UA_sock, BNET_EOD); /* send end of command */
170 } else if (is_bnet_stop(ua->UA_sock)) {
172 } else { /* signal */
173 bnet_sig(ua->UA_sock, BNET_POLL);
186 * Create a UAContext for a Job that is running so that
187 * it can the User Agent routines and
188 * to ensure that the Job gets the proper output.
189 * This is a sort of mini-kludge, and should be
190 * unified at some point.
192 UAContext *new_ua_context(JCR *jcr)
196 ua = (UAContext *)malloc(sizeof(UAContext));
197 memset(ua, 0, sizeof(UAContext));
200 ua->cmd = get_pool_memory(PM_FNAME);
201 ua->args = get_pool_memory(PM_FNAME);
203 ua->automount = TRUE;
207 void free_ua_context(UAContext *ua)
210 free_pool_memory(ua->cmd);
213 free_pool_memory(ua->args);
220 bnet_close(ua->UA_sock);
227 * Called from main Bacula thread
229 void term_ua_server()
234 quit_cmd_thread = TRUE;