From 2b805e7ef98a06d474bf7f3be8df2d3420cc5e1a Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Tue, 24 Feb 2004 15:39:42 +0000 Subject: [PATCH] Work on state file. Add popup if Abort on Windows. Attempt cleaner shutdown git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@1081 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/filed/filed.c | 5 +- bacula/src/lib/bnet_server.c | 11 +- bacula/src/lib/bsys.c | 2 +- bacula/src/lib/jcr.c | 27 ++-- bacula/src/lib/message.c | 3 + bacula/src/lib/protos.h | 235 ++++++++++++++++++----------------- 6 files changed, 154 insertions(+), 129 deletions(-) diff --git a/bacula/src/filed/filed.c b/bacula/src/filed/filed.c index 3a7b5bb85b..aa536f1617 100644 --- a/bacula/src/filed/filed.c +++ b/bacula/src/filed/filed.c @@ -54,6 +54,7 @@ static char *configfile = NULL; static int foreground = 0; static int inetd_request = 0; static workq_t dir_workq; /* queue of work from Director */ +static pthread_t server_tid; static void usage() @@ -227,6 +228,7 @@ Without that I don't know who I am :-(\n"), configfile); start_watchdog(); /* start watchdog thread */ init_jcr_subsystem(); /* start JCR watchdogs etc. */ + server_tid = pthread_self(); if (inetd_request) { /* Socket is on fd 0 */ @@ -248,6 +250,7 @@ Without that I don't know who I am :-(\n"), configfile); void terminate_filed(int sig) { + bnet_stop_thread_server(server_tid); write_state_file(me->working_directory, "bacula-fd", me->FDport); delete_pid_file(me->pid_directory, "bacula-fd", me->FDport); if (configfile != NULL) { @@ -261,5 +264,5 @@ void terminate_filed(int sig) stop_watchdog(); close_memory_pool(); /* release free memory in pool */ sm_dump(False); /* dump orphaned buffers */ - exit(1); + exit(sig); } diff --git a/bacula/src/lib/bnet_server.c b/bacula/src/lib/bnet_server.c index 45c0dd0448..26f7f3c88d 100644 --- a/bacula/src/lib/bnet_server.c +++ b/bacula/src/lib/bnet_server.c @@ -46,6 +46,15 @@ int allow_severity = LOG_NOTICE; int deny_severity = LOG_WARNING; #endif +static bool quit = false; + +void +bnet_stop_thread_server(pthread_t tid) +{ + quit = true; + pthread_kill(tid, TIMEOUT_SIGNAL); +} + /* Become Threaded Network Server */ void bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq, @@ -122,7 +131,7 @@ bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_w /* * Wait for a connection from the client process. */ - for (;;) { + for (;!quit;) { fd_set sockset; FD_ZERO(&sockset); FD_SET(sockfd, &sockset); diff --git a/bacula/src/lib/bsys.c b/bacula/src/lib/bsys.c index e9438c79e4..13a5f8295b 100644 --- a/bacula/src/lib/bsys.c +++ b/bacula/src/lib/bsys.c @@ -359,7 +359,7 @@ struct s_state_hdr { static struct s_state_hdr state_hdr = { "Bacula State\n", - 2, + 3, 0 }; diff --git a/bacula/src/lib/jcr.c b/bacula/src/lib/jcr.c index da48fd0f23..6a283b6537 100755 --- a/bacula/src/lib/jcr.c +++ b/bacula/src/lib/jcr.c @@ -75,11 +75,18 @@ void term_last_jobs_list() void read_last_jobs_list(int fd, uint64_t addr) { struct s_last_job *je, job; + uint32_t num; if (addr == 0 || lseek(fd, addr, SEEK_SET) < 0) { return; } - for ( ;; ) { + if (read(fd, &num, sizeof(num)) < 0) { + return; + } + if (num > 4 * MAX_LAST_JOBS) { /* sanity check */ + return; + } + for ( ; num; num--) { if (read(fd, &job, sizeof(job)) < 0) { return; } @@ -93,32 +100,34 @@ void read_last_jobs_list(int fd, uint64_t addr) if (last_jobs->size() > MAX_LAST_JOBS) { last_jobs->remove(last_jobs->first()); } - job.JobId = 0; /* zap last job */ - } else { - break; } } } uint64_t write_last_jobs_list(int fd, uint64_t addr) { - struct s_last_job *je, job; + struct s_last_job *je; + uint32_t num; + if (lseek(fd, addr, SEEK_SET) < 0) { return 0; } if (last_jobs) { + /* First record is number of entires */ + num = last_jobs->size(); + if (write(fd, &num, sizeof(num)) < 0) { + return 0; + } foreach_dlist(je, last_jobs) { if (write(fd, je, sizeof(struct s_last_job)) < 0) { return 0; } } } - /* Write a zero record to terminate list */ - memset(&job, 0, sizeof(job)); - write(fd, &job, sizeof(job)); + /* Return current address */ ssize_t stat = lseek(fd, 0, SEEK_CUR); if (stat < 0) { - return 0; + stat = 0; } return stat; diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index 803dff0640..168edf630c 100755 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -583,6 +583,9 @@ void dispatch_message(JCR *jcr, int type, int level, char *msg) if (type == M_ABORT || type == M_ERROR_TERM) { fputs(msg, stdout); /* print this here to INSURE that it is printed */ +#if defined(HAVE_CYGWIN) || defined(HAVE_WIN32) + MessageBox(NULL, msg, "Bacula", MB_OK); +#endif } /* Now figure out where to send the message */ diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index 84432493db..5351307c02 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -26,99 +26,99 @@ struct JCR; /* attr.c */ -ATTR *new_attr(); -void free_attr(ATTR *attr); -int unpack_attributes_record(JCR *jcr, int32_t stream, char *rec, ATTR *attr); -void build_attr_output_fnames(JCR *jcr, ATTR *attr); -void print_ls_output(JCR *jcr, ATTR *attr); +ATTR *new_attr(); +void free_attr(ATTR *attr); +int unpack_attributes_record(JCR *jcr, int32_t stream, char *rec, ATTR *attr); +void build_attr_output_fnames(JCR *jcr, ATTR *attr); +void print_ls_output(JCR *jcr, ATTR *attr); /* base64.c */ -void base64_init (void); -int to_base64 (intmax_t value, char *where); -int from_base64 (intmax_t *value, char *where); -int bin_to_base64 (char *buf, char *bin, int len); +void base64_init (void); +int to_base64 (intmax_t value, char *where); +int from_base64 (intmax_t *value, char *where); +int bin_to_base64 (char *buf, char *bin, int len); /* bsys.c */ -char *bstrncpy (char *dest, const char *src, int maxlen); -char *bstrncat (char *dest, const char *src, int maxlen); -void *b_malloc (const char *file, int line, size_t size); +char *bstrncpy (char *dest, const char *src, int maxlen); +char *bstrncat (char *dest, const char *src, int maxlen); +void *b_malloc (const char *file, int line, size_t size); #ifndef DEBUG -void *bmalloc (size_t size); +void *bmalloc (size_t size); #endif -void *brealloc (void *buf, size_t size); -void *bcalloc (size_t size1, size_t size2); -int bsnprintf (char *str, int32_t size, const char *format, ...); -int bvsnprintf (char *str, int32_t size, const char *format, va_list ap); -int pool_sprintf (char *pool_buf, const char *fmt, ...); -void create_pid_file (char *dir, const char *progname, int port); -int delete_pid_file (char *dir, const char *progname, int port); -void drop (char *uid, char *gid); -int bmicrosleep (time_t sec, long usec); -char *bfgets (char *s, int size, FILE *fd); -void make_unique_filename (POOLMEM **name, int Id, char *what); +void *brealloc (void *buf, size_t size); +void *bcalloc (size_t size1, size_t size2); +int bsnprintf (char *str, int32_t size, const char *format, ...); +int bvsnprintf (char *str, int32_t size, const char *format, va_list ap); +int pool_sprintf (char *pool_buf, const char *fmt, ...); +void create_pid_file (char *dir, const char *progname, int port); +int delete_pid_file (char *dir, const char *progname, int port); +void drop (char *uid, char *gid); +int bmicrosleep (time_t sec, long usec); +char *bfgets (char *s, int size, FILE *fd); +void make_unique_filename (POOLMEM **name, int Id, char *what); #ifndef HAVE_STRTOLL -long long int strtoll (const char *ptr, char **endptr, int base); +long long int strtoll (const char *ptr, char **endptr, int base); #endif void read_state_file(char *dir, const char *progname, int port); /* bnet.c */ -int32_t bnet_recv (BSOCK *bsock); -int bnet_send (BSOCK *bsock); -int bnet_fsend (BSOCK *bs, const char *fmt, ...); -int bnet_set_buffer_size (BSOCK *bs, uint32_t size, int rw); -int bnet_sig (BSOCK *bs, int sig); -int bnet_ssl_server (BSOCK *bsock, char *password, int ssl_need, int ssl_has); -int bnet_ssl_client (BSOCK *bsock, char *password, int ssl_need); -BSOCK * bnet_connect (JCR *jcr, int retry_interval, - int max_retry_time, const char *name, char *host, char *service, - int port, int verbose); -void bnet_close (BSOCK *bsock); -BSOCK * init_bsock (JCR *jcr, int sockfd, const char *who, char *ip, - int port, struct sockaddr_in *client_addr); -BSOCK * dup_bsock (BSOCK *bsock); -void term_bsock (BSOCK *bsock); -char * bnet_strerror (BSOCK *bsock); -char * bnet_sig_to_ascii (BSOCK *bsock); -int bnet_wait_data (BSOCK *bsock, int sec); -int bnet_wait_data_intr (BSOCK *bsock, int sec); -int bnet_despool_to_bsock (BSOCK *bsock); -int is_bnet_stop (BSOCK *bsock); -int is_bnet_error (BSOCK *bsock); -void bnet_suppress_error_messages(BSOCK *bsock, int flag); +int32_t bnet_recv (BSOCK *bsock); +int bnet_send (BSOCK *bsock); +int bnet_fsend (BSOCK *bs, const char *fmt, ...); +int bnet_set_buffer_size (BSOCK *bs, uint32_t size, int rw); +int bnet_sig (BSOCK *bs, int sig); +int bnet_ssl_server (BSOCK *bsock, char *password, int ssl_need, int ssl_has); +int bnet_ssl_client (BSOCK *bsock, char *password, int ssl_need); +BSOCK * bnet_connect (JCR *jcr, int retry_interval, + int max_retry_time, const char *name, char *host, char *service, + int port, int verbose); +void bnet_close (BSOCK *bsock); +BSOCK * init_bsock (JCR *jcr, int sockfd, const char *who, char *ip, + int port, struct sockaddr_in *client_addr); +BSOCK * dup_bsock (BSOCK *bsock); +void term_bsock (BSOCK *bsock); +char * bnet_strerror (BSOCK *bsock); +char * bnet_sig_to_ascii (BSOCK *bsock); +int bnet_wait_data (BSOCK *bsock, int sec); +int bnet_wait_data_intr (BSOCK *bsock, int sec); +int bnet_despool_to_bsock (BSOCK *bsock); +int is_bnet_stop (BSOCK *bsock); +int is_bnet_error (BSOCK *bsock); +void bnet_suppress_error_messages(BSOCK *bsock, int flag); /* bget_msg.c */ -int bget_msg(BSOCK *sock); +int bget_msg(BSOCK *sock); /* bpipe.c */ -BPIPE * open_bpipe(char *prog, int wait, const char *mode); -int close_wpipe(BPIPE *bpipe); -int close_bpipe(BPIPE *bpipe); +BPIPE * open_bpipe(char *prog, int wait, const char *mode); +int close_wpipe(BPIPE *bpipe); +int close_bpipe(BPIPE *bpipe); /* cram-md5.c */ int cram_md5_get_auth(BSOCK *bs, char *password, int ssl_need); int cram_md5_auth(BSOCK *bs, char *password, int ssl_need); void hmac_md5(uint8_t* text, int text_len, uint8_t* key, - int key_len, uint8_t *hmac); + int key_len, uint8_t *hmac); /* crc32.c */ uint32_t bcrc32(uint8_t *buf, int len); /* daemon.c */ -void daemon_start (); +void daemon_start (); /* edit.c */ -uint64_t str_to_uint64(char *str); -int64_t str_to_int64(char *str); -char * edit_uint64_with_commas (uint64_t val, char *buf); -char * add_commas (char *val, char *buf); -char * edit_uint64 (uint64_t val, char *buf); -int duration_to_utime (char *str, utime_t *value); -int size_to_uint64(char *str, int str_len, uint64_t *rtn_value); -char *edit_utime (utime_t val, char *buf); -int is_a_number (const char *num); -int is_an_integer (const char *n); -bool is_name_valid (char *name, POOLMEM **msg); +uint64_t str_to_uint64(char *str); +int64_t str_to_int64(char *str); +char * edit_uint64_with_commas (uint64_t val, char *buf); +char * add_commas (char *val, char *buf); +char * edit_uint64 (uint64_t val, char *buf); +int duration_to_utime (char *str, utime_t *value); +int size_to_uint64(char *str, int str_len, uint64_t *rtn_value); +char *edit_utime (utime_t val, char *buf); +int is_a_number (const char *num); +int is_an_integer (const char *n); +bool is_name_valid (char *name, POOLMEM **msg); /* jcr.c (most definitions are in src/jcr.h) */ void init_last_jobs_list(); @@ -131,36 +131,37 @@ void write_state_file(char *dir, const char *progname, int port); /* lex.c */ -LEX * lex_close_file (LEX *lf); -LEX * lex_open_file (LEX *lf, char *fname, LEX_ERROR_HANDLER *scan_error); -int lex_get_char (LEX *lf); -void lex_unget_char (LEX *lf); -const char * lex_tok_to_str (int token); -int lex_get_token (LEX *lf, int expect); +LEX * lex_close_file (LEX *lf); +LEX * lex_open_file (LEX *lf, char *fname, LEX_ERROR_HANDLER *scan_error); +int lex_get_char (LEX *lf); +void lex_unget_char (LEX *lf); +const char * lex_tok_to_str (int token); +int lex_get_token (LEX *lf, int expect); /* message.c */ -void my_name_is (int argc, char *argv[], const char *name); -void init_msg (JCR *jcr, MSGS *msg); -void term_msg (void); -void close_msg (JCR *jcr); -void add_msg_dest (MSGS *msg, int dest, int type, char *where, char *dest_code); -void rem_msg_dest (MSGS *msg, int dest, int type, char *where); -void Jmsg (JCR *jcr, int type, int level, const char *fmt, ...); -void dispatch_message (JCR *jcr, int type, int level, char *buf); -void init_console_msg (char *wd); -void free_msgs_res (MSGS *msgs); -int open_spool_file (JCR *jcr, BSOCK *bs); -int close_spool_file (JCR *jcr, BSOCK *bs); -void dequeue_messages (JCR *jcr); +void my_name_is (int argc, char *argv[], const char *name); +void init_msg (JCR *jcr, MSGS *msg); +void term_msg (void); +void close_msg (JCR *jcr); +void add_msg_dest (MSGS *msg, int dest, int type, char *where, char *dest_code); +void rem_msg_dest (MSGS *msg, int dest, int type, char *where); +void Jmsg (JCR *jcr, int type, int level, const char *fmt, ...); +void dispatch_message (JCR *jcr, int type, int level, char *buf); +void init_console_msg (char *wd); +void free_msgs_res (MSGS *msgs); +int open_spool_file (JCR *jcr, BSOCK *bs); +int close_spool_file (JCR *jcr, BSOCK *bs); +void dequeue_messages (JCR *jcr); /* bnet_server.c */ -void bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq, - void *handle_client_request(void *bsock)); -void bnet_server (int port, void handle_client_request(BSOCK *bsock)); -int net_connect (int port); -BSOCK * bnet_bind (int port); -BSOCK * bnet_accept (BSOCK *bsock, char *who); +void bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq, + void *handle_client_request(void *bsock)); +void bnet_stop_thread_server(pthread_t tid); +void bnet_server (int port, void handle_client_request(BSOCK *bsock)); +int net_connect (int port); +BSOCK * bnet_bind (int port); +BSOCK * bnet_accept (BSOCK *bsock, char *who); /* idcache.c */ char *getuser(uid_t uid); @@ -170,37 +171,37 @@ void free_getgroup_cache(); /* signal.c */ -void init_signals (void terminate(int sig)); -void init_stack_dump (void); +void init_signals (void terminate(int sig)); +void init_stack_dump (void); /* scan.c */ -void strip_trailing_junk (char *str); -void strip_trailing_slashes (char *dir); -bool skip_spaces (char **msg); -bool skip_nonspaces (char **msg); -int fstrsch (char *a, char *b); -int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc, - char **argk, char **argv, int max_args); -char *next_arg(char **s); +void strip_trailing_junk (char *str); +void strip_trailing_slashes (char *dir); +bool skip_spaces (char **msg); +bool skip_nonspaces (char **msg); +int fstrsch (char *a, char *b); +int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc, + char **argk, char **argv, int max_args); +char *next_arg(char **s); /* util.c */ -int is_buf_zero (char *buf, int len); -void lcase (char *str); -void bash_spaces (char *str); -void unbash_spaces (char *str); -char * encode_time (time_t time, char *buf); -char * encode_mode (mode_t mode, char *buf); -int do_shell_expansion (char *name, int name_len); -void jobstatus_to_ascii (int JobStatus, char *msg, int maxlen); -int pm_strcat (POOLMEM **pm, const char *str); -int pm_strcpy (POOLMEM **pm, const char *str); -int run_program (char *prog, int wait, POOLMEM *results); -char * job_type_to_str (int type); -char * job_status_to_str (int stat); -char * job_level_to_str (int level); -void make_session_key (char *key, char *seed, int mode); -POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, const char *to); -void set_working_directory(char *wd); +int is_buf_zero (char *buf, int len); +void lcase (char *str); +void bash_spaces (char *str); +void unbash_spaces (char *str); +char * encode_time (time_t time, char *buf); +char * encode_mode (mode_t mode, char *buf); +int do_shell_expansion (char *name, int name_len); +void jobstatus_to_ascii (int JobStatus, char *msg, int maxlen); +int pm_strcat (POOLMEM **pm, const char *str); +int pm_strcpy (POOLMEM **pm, const char *str); +int run_program (char *prog, int wait, POOLMEM *results); +char * job_type_to_str (int type); +char * job_status_to_str (int stat); +char * job_level_to_str (int level); +void make_session_key (char *key, char *seed, int mode); +POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, const char *to); +void set_working_directory(char *wd); /* watchdog.c */ -- 2.39.5