]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/status.c
Fix Win32 crash
[bacula/bacula] / bacula / src / filed / status.c
1 /*
2  *  Bacula File Daemon Status routines
3  *
4  *    Kern Sibbald, August MMI
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "filed.h"
31
32 extern char my_name[];
33 extern struct s_last_job last_job;
34 extern time_t daemon_start_time;
35
36 /*
37  * General status generator
38  */
39 static void do_status(void sendit(char *msg, int len, void *sarg), void *arg) 
40 {
41    int sec, bps;
42    char *msg, b1[32], b2[32], b3[32];
43    int found, len;
44    JCR *njcr;
45    char dt[MAX_TIME_LENGTH];
46
47    msg = (char *)get_pool_memory(PM_MESSAGE);
48    found = 0;
49    len = Mmsg(&msg, "%s Version: " VERSION " (" BDATE ")\n", my_name);
50    sendit(msg, len, arg);
51    bstrftime(dt, sizeof(dt), daemon_start_time);
52    len = Mmsg(&msg, _("Daemon started %s, %d Job%s run.\n"), dt, last_job.NumJobs,
53         last_job.NumJobs == 1 ? "" : "s");
54    sendit(msg, len, arg);
55    if (last_job.NumJobs > 0) {
56       char termstat[30];
57
58       bstrftime(dt, sizeof(dt), last_job.end_time);
59       len = Mmsg(&msg, _("Last Job %s finished at %s\n"), last_job.Job, dt);
60       sendit(msg, len, arg);
61
62       jobstatus_to_ascii(last_job.JobStatus, termstat, sizeof(termstat));
63       len = Mmsg(&msg, _("  Files=%s Bytes=%s Termination Status=%s\n"), 
64            edit_uint64_with_commas(last_job.JobFiles, b1),
65            edit_uint64_with_commas(last_job.JobBytes, b2),
66            termstat);
67       sendit(msg, len, arg);
68    }
69    Dmsg0(200, "Begin status jcr loop.\n");
70    lock_jcr_chain();
71    for (njcr=NULL; (njcr=get_next_jcr(njcr)); ) {
72       bstrftime(dt, sizeof(dt), njcr->start_time);
73       if (njcr->JobId == 0) {
74          len = Mmsg(&msg, _("Director connected at: %s\n"), dt);
75       } else {
76          len = Mmsg(&msg, _("JobId %d Job %s is running.\n"), 
77                     njcr->JobId, njcr->Job);
78          sendit(msg, len, arg);
79          len = Mmsg(&msg, _("    %s Job started: %s\n"), 
80                     job_type_to_str(njcr->JobType), dt);
81       }
82       sendit(msg, len, arg);
83       if (njcr->JobId == 0) {
84          free_locked_jcr(njcr);
85          continue;
86       }
87       sec = time(NULL) - njcr->start_time;
88       if (sec <= 0) {
89          sec = 1;
90       }
91       bps = njcr->JobBytes / sec;
92       len = Mmsg(&msg,  _("    Files=%s Bytes=%s Bytes/sec=%s\n"), 
93            edit_uint64_with_commas(njcr->JobFiles, b1),
94            edit_uint64_with_commas(njcr->JobBytes, b2),
95            edit_uint64_with_commas(bps, b3));
96       sendit(msg, len, arg);
97       len = Mmsg(&msg, _("    Files Examined=%s\n"), 
98            edit_uint64_with_commas(njcr->num_files_examined, b1));
99       sendit(msg, len, arg);
100       if (njcr->JobFiles > 0) {
101          P(njcr->mutex);
102          len = Mmsg(&msg, _("    Processing file: %s\n"), njcr->last_fname);
103          V(njcr->mutex);
104          sendit(msg, len, arg);
105       }
106
107       found = 1;
108       if (njcr->store_bsock) {
109          len = Mmsg(&msg, "    SDReadSeqNo=%" lld " fd=%d\n",
110              njcr->store_bsock->read_seqno, njcr->store_bsock->fd);
111          sendit(msg, len, arg);
112       } else {
113          len = Mmsg(&msg, _("    SDSocket closed.\n"));
114          sendit(msg, len, arg);
115       }
116       free_locked_jcr(njcr);
117    }
118    unlock_jcr_chain();
119    Dmsg0(200, "Begin status jcr loop.\n");
120    if (!found) {
121       len = Mmsg(&msg, _("No jobs running.\n"));
122       sendit(msg, len, arg);
123    }
124    free_pool_memory(msg);
125 }
126
127 /*
128  * Send to Director 
129  */
130 static void sendit(char *msg, int len, void *arg)
131 {
132    BSOCK *user = (BSOCK *)arg;
133
134    memcpy(user->msg, msg, len+1);
135    user->msglen = len+1;
136    bnet_send(user);
137 }
138                                    
139 /*
140  * Status command from Director
141  */
142 int status_cmd(JCR *jcr)
143 {
144    BSOCK *user = jcr->dir_bsock;
145
146    bnet_fsend(user, "\n");
147    do_status(sendit, (void *)user);
148    bnet_fsend(user, "====\n");
149
150    bnet_sig(user, BNET_EOD);
151    return 1;
152 }
153
154
155 #ifdef HAVE_CYGWIN
156 #include <windows.h>
157
158 static char buf[100];
159 int bacstat = 0;
160
161 struct s_win32_arg {
162    HWND hwnd;
163    int idlist;
164 };
165
166 /*
167  * Put message in Window List Box
168  */
169 static void win32_sendit(char *msg, int len, void *marg)
170 {
171    struct s_win32_arg *arg = (struct s_win32_arg *)marg;
172
173    if (len > 0) {
174       msg[len-1] = 0;                 /* eliminate newline */
175    }
176    SendDlgItemMessage(arg->hwnd, arg->idlist, LB_ADDSTRING, 0, (LONG)msg);
177    
178 }
179
180 void FillStatusBox(HWND hwnd, int idlist)
181 {
182    struct s_win32_arg arg;
183
184    arg.hwnd = hwnd;
185    arg.idlist = idlist;
186
187    /* Empty box */
188    for ( ; SendDlgItemMessage(hwnd, idlist, LB_DELETESTRING, 0, (LONG)0) > 0; )
189       { }
190    do_status(win32_sendit, (void *)&arg);
191 }
192
193 char *bac_status(int stat)
194 {
195    JCR *njcr;
196    char *termstat = _("Bacula Idle");
197
198    bacstat = 0;
199    if (last_job.NumJobs > 0) {
200       switch (last_job.JobStatus) {
201         case JS_ErrorTerminated:
202             bacstat = -1;
203             termstat = _("Last Job Erred");
204             break;
205         default:
206             break;
207       }
208    }
209    Dmsg0(200, "Begin bac_status jcr loop.\n");
210    lock_jcr_chain();
211    for (njcr=NULL; (njcr=get_next_jcr(njcr)); ) {
212       if (njcr->JobId != 0) {
213          bacstat = 1;
214          termstat = _("Bacula Running");
215          free_locked_jcr(njcr);
216          break;
217       }
218       free_locked_jcr(njcr);
219    }
220    unlock_jcr_chain();
221    Dmsg0(200, "End bac_status jcr loop.\n");
222    strcpy(buf, termstat);
223    return buf;
224 }
225
226 #endif /* HAVE_CYGWIN */