]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_dotcmds.c
btape test and fill improvements + first cut Qmsg
[bacula/bacula] / bacula / src / dird / ua_dotcmds.c
1 /*
2  *
3  *   Bacula Director -- User Agent Commands
4  *     These are "dot" commands, i.e. commands preceded
5  *        by a period. These commands are meant to be used
6  *        by a program, so there is no prompting, and the
7  *        returned results are (supposed to be) predictable.
8  *
9  *     Kern Sibbald, April MMII
10  *
11  *   Version $Id$
12  */
13
14 /*
15    Copyright (C) 2002 Kern Sibbald and John Walker
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34 #include "bacula.h"
35 #include "dird.h"
36
37 /* Imported variables */
38 extern int r_first;
39 extern int r_last;
40 extern struct s_res resources[];
41 extern char my_name[];
42
43 /* Imported functions */
44 extern int qmessagescmd(UAContext *ua, char *cmd);
45 extern int quit_cmd(UAContext *ua, char *cmd);
46
47 /* Forward referenced functions */
48 static int diecmd(UAContext *ua, char *cmd);
49 static int jobscmd(UAContext *ua, char *cmd);
50 static int filesetscmd(UAContext *ua, char *cmd);
51 static int clientscmd(UAContext *ua, char *cmd);
52 static int msgscmd(UAContext *ua, char *cmd);
53 static int poolscmd(UAContext *ua, char *cmd);
54 static int storagecmd(UAContext *ua, char *cmd);
55 static int defaultscmd(UAContext *ua, char *cmd);
56 static int typescmd(UAContext *ua, char *cmd);
57 static int levelscmd(UAContext *ua, char *cmd);
58
59 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; }; 
60 static struct cmdstruct commands[] = {
61  { N_(".die"),        diecmd,       NULL},
62  { N_(".jobs"),       jobscmd,      NULL},
63  { N_(".filesets"),   filesetscmd,  NULL},
64  { N_(".clients"),    clientscmd,   NULL},
65  { N_(".msgs"),       msgscmd,      NULL},
66  { N_(".pools"),      poolscmd,     NULL},
67  { N_(".types"),      typescmd,     NULL},
68  { N_(".levels"),     levelscmd,    NULL},
69  { N_(".storage"),    storagecmd,   NULL},
70  { N_(".defaults"),   defaultscmd,  NULL},
71  { N_(".messages"),   qmessagescmd, NULL},
72  { N_(".quit"),       quit_cmd,     NULL},
73  { N_(".exit"),       quit_cmd,     NULL} 
74              };
75 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
76
77 /*
78  * Execute a command from the UA
79  */
80 int do_a_dot_command(UAContext *ua, char *cmd)
81 {
82    int i;
83    int len, stat;  
84    bool found = false;
85
86    stat = 1;
87
88    Dmsg1(400, "Dot command: %s\n", ua->UA_sock->msg);
89    if (ua->argc == 0) {
90       return 1;
91    }
92
93    len = strlen(ua->argk[0]);
94    if (len == 1) {
95       return 1;                       /* no op */
96    }
97    for (i=0; i<(int)comsize; i++) {     /* search for command */
98       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
99          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
100          found = true;
101          break;
102       }
103    }
104    if (!found) {
105       pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
106       ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
107       bnet_send(ua->UA_sock);
108    }
109    return stat;
110 }
111
112 /*
113  * Create segmentation fault 
114  */
115 static int diecmd(UAContext *ua, char *cmd)
116 {
117    JCR *jcr = NULL;
118    int a;
119    
120    bsendmsg(ua, "The Director will segment fault.\n");
121    a = jcr->JobId; /* ref NULL pointer */
122    jcr->JobId = 1000; /* another ref NULL pointer */
123    return 0;
124 }
125
126 static int jobscmd(UAContext *ua, char *cmd)
127 {
128    JOB *job = NULL;
129    LockRes();
130    while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
131       bsendmsg(ua, "%s\n", job->hdr.name);
132    }
133    UnlockRes();
134    return 1;
135 }
136
137 static int filesetscmd(UAContext *ua, char *cmd)
138 {
139    FILESET *fs = NULL;
140    LockRes();
141    while ( (fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs)) ) {
142       bsendmsg(ua, "%s\n", fs->hdr.name);
143    }
144    UnlockRes();
145    return 1;
146 }
147
148 static int clientscmd(UAContext *ua, char *cmd)
149 {
150    CLIENT *client = NULL;
151    LockRes();
152    while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
153       bsendmsg(ua, "%s\n", client->hdr.name);
154    }
155    UnlockRes();
156    return 1;
157 }
158
159 static int msgscmd(UAContext *ua, char *cmd)
160 {
161    MSGS *msgs = NULL;
162    LockRes();
163    while ( (msgs = (MSGS *)GetNextRes(R_MSGS, (RES *)msgs)) ) {
164       bsendmsg(ua, "%s\n", msgs->hdr.name);
165    }
166    UnlockRes();
167    return 1;
168 }
169
170 static int poolscmd(UAContext *ua, char *cmd)
171 {
172    POOL *pool = NULL;
173    LockRes();
174    while ( (pool = (POOL *)GetNextRes(R_POOL, (RES *)pool)) ) {
175       bsendmsg(ua, "%s\n", pool->hdr.name);
176    }
177    UnlockRes();
178    return 1;
179 }
180
181 static int storagecmd(UAContext *ua, char *cmd)
182 {
183    STORE *store = NULL;
184    LockRes();
185    while ( (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)) ) {
186       bsendmsg(ua, "%s\n", store->hdr.name);
187    }
188    UnlockRes();
189    return 1;
190 }
191
192
193 static int typescmd(UAContext *ua, char *cmd)
194 {
195    bsendmsg(ua, "Backup\n");
196    bsendmsg(ua, "Restore\n");
197    bsendmsg(ua, "Admin\n");
198    bsendmsg(ua, "Verify\n");
199    return 1;
200 }
201
202 static int levelscmd(UAContext *ua, char *cmd)
203 {
204    bsendmsg(ua, "Incremental\n");
205    bsendmsg(ua, "Full\n");
206    bsendmsg(ua, "Differential\n");
207    bsendmsg(ua, "Catalog\n");
208    bsendmsg(ua, "InitCatalog\n");
209    bsendmsg(ua, "VolumeToCatalog\n");
210    return 1;
211 }
212
213
214
215 /*
216  * Return default values for a job
217  */
218 static int defaultscmd(UAContext *ua, char *cmd)
219 {
220    JOB *job;       
221    if (ua->argc == 2 && strcmp(ua->argk[1], "job") == 0) {
222       job = (JOB *)GetResWithName(R_JOB, ua->argv[1]);
223       if (job) {
224          bsendmsg(ua, "job=%s", job->hdr.name);
225          bsendmsg(ua, "pool=%s", job->pool->hdr.name);
226          bsendmsg(ua, "messages=%s", job->messages->hdr.name);
227          bsendmsg(ua, "client=%s", job->client->hdr.name);
228          bsendmsg(ua, "storage=%s", job->storage->hdr.name);
229          bsendmsg(ua, "where=%s", job->RestoreWhere?job->RestoreWhere:"");
230          bsendmsg(ua, "level=%s", level_to_str(job->level));
231          bsendmsg(ua, "type=%s", job_type_to_str(job->JobType));
232          bsendmsg(ua, "fileset=%s", job->fileset->hdr.name);
233       }
234    }
235    return 1;
236 }