]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_dotcmds.c
Documentation, RecycleOldestVol, fix create_media bug
[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 quitcmd(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
54 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; }; 
55 static struct cmdstruct commands[] = {
56  { N_(".die"),        diecmd,       NULL},
57  { N_(".jobs"),       jobscmd,      NULL},
58  { N_(".filesets"),   filesetscmd,  NULL},
59  { N_(".clients"),    clientscmd,   NULL},
60  { N_(".msgs"),       msgscmd,      NULL},
61  { N_(".messages"),   qmessagescmd, NULL},
62  { N_(".quit"),       quitcmd,      NULL},
63  { N_(".exit"),       quitcmd,      NULL} 
64              };
65 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
66
67 /*
68  * Execute a command from the UA
69  */
70 int do_a_dot_command(UAContext *ua, char *cmd)
71 {
72    unsigned int i;
73    int len, stat;  
74    int found;
75
76    found = 0;
77    stat = 1;
78
79    Dmsg1(200, "Dot command: %s\n", ua->UA_sock->msg);
80    if (ua->argc == 0) {
81       return 1;
82    }
83
84    len = strlen(ua->argk[0]);
85    if (len == 1) {
86       return 1;                       /* no op */
87    }
88    for (i=0; i<comsize; i++) {     /* search for command */
89       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
90          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
91          found = 1;
92          break;
93       }
94    }
95    if (!found) {
96       strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
97       ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
98       bnet_send(ua->UA_sock);
99    }
100    return stat;
101 }
102
103 /*
104  * Create segmentation fault 
105  */
106 static int diecmd(UAContext *ua, char *cmd)
107 {
108    JCR *jcr = NULL;
109    int a;
110    
111    a = jcr->JobId; /* ref NULL pointer */
112    return 0;
113 }
114
115 static int jobscmd(UAContext *ua, char *cmd)
116 {
117    JOB *job = NULL;
118    LockRes();
119    while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
120       bsendmsg(ua, "%s\n", job->hdr.name);
121    }
122    UnlockRes();
123    return 1;
124 }
125
126 static int filesetscmd(UAContext *ua, char *cmd)
127 {
128    FILESET *fs = NULL;
129    LockRes();
130    while ( (fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs)) ) {
131       bsendmsg(ua, "%s\n", fs->hdr.name);
132    }
133    UnlockRes();
134    return 1;
135 }
136
137 static int clientscmd(UAContext *ua, char *cmd)
138 {
139    CLIENT *client = NULL;
140    LockRes();
141    while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
142       bsendmsg(ua, "%s\n", client->hdr.name);
143    }
144    UnlockRes();
145    return 1;
146 }
147
148 static int msgscmd(UAContext *ua, char *cmd)
149 {
150    MSGS *msgs = NULL;
151    LockRes();
152    while ( (msgs = (MSGS *)GetNextRes(R_MSGS, (RES *)msgs)) ) {
153       bsendmsg(ua, "%s\n", msgs->hdr.name);
154    }
155    UnlockRes();
156    return 1;
157 }