Made Xorriso_process_msg_queues() thread-safe

This commit is contained in:
Thomas Schmitt 2012-09-13 13:09:54 +00:00
parent d3ca2f9c83
commit fcc00d4311
6 changed files with 220 additions and 62 deletions

View File

@ -320,6 +320,7 @@ int Xorriso_new(struct XorrisO ** xorriso,char *progname, int flag)
}
m->lib_msg_queue_lock_ini= 0;
m->result_msglists_lock_ini= 0;
m->write_to_channel_lock_ini= 0;
m->msglist_stackfill= 0;
m->status_history_max= Xorriso_status_history_maX;
m->scsi_log= 0;
@ -330,6 +331,7 @@ int Xorriso_new(struct XorrisO ** xorriso,char *progname, int flag)
Xorriso__text_to_sev(m->abort_on_text, &m->abort_on_severity, 0);
m->abort_on_is_default= 1;
m->problem_status= 0;
m->problem_status_lock_ini= 0;
m->problem_status_text[0]= 0;
m->errfile_log[0]= 0;
m->errfile_mode= 0;
@ -444,6 +446,14 @@ int Xorriso_new(struct XorrisO ** xorriso,char *progname, int flag)
if(ret != 0)
goto failure;
m->result_msglists_lock_ini= 1;
ret= pthread_mutex_init(&(m->write_to_channel_lock), NULL);
if(ret != 0)
goto failure;
m->result_msglists_lock_ini= 1;
ret= pthread_mutex_init(&(m->problem_status_lock), NULL);
if(ret != 0)
goto failure;
m->problem_status_lock_ini= 1;
if(leafname != NULL)
free(leafname);
@ -524,13 +534,18 @@ int Xorriso_destroy(struct XorrisO **xorriso, int flag)
for(i= 0; i < Xorriso_max_appended_partitionS; i++)
if(m->appended_partitions[i] != NULL)
free(m->appended_partitions[i]);
Xorriso_detach_libraries(m, flag&1);
if(m->lib_msg_queue_lock_ini)
pthread_mutex_destroy(&(m->lib_msg_queue_lock));
if(m->result_msglists_lock_ini)
pthread_mutex_destroy(&(m->result_msglists_lock));
if(m->write_to_channel_lock_ini)
pthread_mutex_destroy(&(m->write_to_channel_lock));
if(m->problem_status_lock_ini)
pthread_mutex_destroy(&(m->problem_status_lock));
Xorriso_detach_libraries(m, flag&1);
free((char *) m);
*xorriso= NULL;
return(1);

View File

@ -470,8 +470,12 @@ cannot:;
int Xorriso_process_msg_queues(struct XorrisO *xorriso, int flag)
{
int ret, error_code= 0, os_errno= 0, count= 0, pass, imgid, tunneled;
int name_prefix_code, locked= 0, uret;
char severity[80];
int name_prefix_code;
char severity[80], *text= NULL;
#ifdef Xorriso_fetch_with_msg_queueS
int locked= 0, uret;
#endif
#ifdef Xorriso_with_libjtE
char *msg;
@ -481,26 +485,35 @@ int Xorriso_process_msg_queues(struct XorrisO *xorriso, int flag)
ret= 1; goto ex;
}
#ifdef Xorriso_fetch_with_msg_queueS
Xorriso_alloc_meM(text, char, sizeof(xorriso->info_text));
ret= pthread_mutex_lock(&(xorriso->lib_msg_queue_lock));
if(ret != 0) {
Xorriso_msgs_submit(xorriso, 0,
"Cannot aquire mutex lock for processing library message queues",
errno, "FATAL", 0);
ret= -1; goto ex;
}
locked= 1;
ret, "FATAL", 0);
} else
locked= 1;
#else /* Xorriso_fetch_with_msg_queueS */
text= xorriso->info_text;
#endif /* ! Xorriso_fetch_with_msg_queueS */
for(pass= 0; pass< 3; pass++) {
while(1) {
tunneled= 0;
if(pass==0) {
ret= 0;
#ifdef Xorriso_with_libjtE
if(xorriso->libjte_handle != NULL) {
msg= libjte_get_next_message(xorriso->libjte_handle);
if(msg != NULL) {
sprintf(xorriso->info_text, "%1.4095s", msg);
sprintf(text, "%1.4095s", msg);
free(msg);
strcpy(severity, "NOTE");
error_code= 0;
@ -511,11 +524,9 @@ int Xorriso_process_msg_queues(struct XorrisO *xorriso, int flag)
#endif /* Xorriso_with_libjtE */
} else if(pass==1)
ret= iso_obtain_msgs("ALL", &error_code, &imgid,
xorriso->info_text, severity);
ret= iso_obtain_msgs("ALL", &error_code, &imgid, text, severity);
else {
ret= burn_msgs_obtain("ALL", &error_code, xorriso->info_text, &os_errno,
severity);
ret= burn_msgs_obtain("ALL", &error_code, text, &os_errno, severity);
if((error_code>=0x00030000 && error_code<0x00040000) ||
(error_code>=0x00050000 && error_code<0x00060000))
tunneled= -1; /* "libisofs:" */
@ -538,28 +549,44 @@ int Xorriso_process_msg_queues(struct XorrisO *xorriso, int flag)
name_prefix_code= 0;
else
name_prefix_code= pass + tunneled;
Xorriso_msgs_submit(xorriso, error_code, xorriso->info_text, os_errno,
Xorriso_msgs_submit(xorriso, error_code, text, os_errno,
severity, name_prefix_code << 2);
count++;
}
}
if(xorriso->library_msg_direct_print && count>0) {
sprintf(xorriso->info_text," (%d library messages repeated by xorriso)\n",
count);
sprintf(text," (%d library messages repeated by xorriso)\n", count);
#ifdef Xorriso_fetch_with_msg_queueS
Xorriso_msgs_submit(xorriso, 0, text, 0, "NOTE", 256);
#else /* Xorriso_fetch_with_msg_queueS */
Xorriso_info(xorriso, 0);
#endif /* Xorriso_fetch_with_msg_queueS */
}
ret= 1;
ex:;
#ifdef Xorriso_fetch_with_msg_queueS
if(locked) {
uret= pthread_mutex_unlock(&(xorriso->lib_msg_queue_lock));
if(uret != 0) {
Xorriso_msgs_submit(xorriso, 0,
"Cannot release mutex lock for processing library message queues",
errno, "FATAL", 0);
uret, "FATAL", 0);
ret= -1;
}
}
return(1);
Xorriso_free_meM(text);
#endif /* Xorriso_fetch_with_msg_queueS */
return(ret);
}

View File

@ -30,12 +30,16 @@
#include <sys/wait.h>
#include "xorriso.h"
#include "xorriso_private.h"
#include "xorrisoburn.h"
#ifdef Xorriso_fetch_with_msg_queueS
#include <pthread.h>
#endif
/* @param flag bit0= do not warn of wildcards
bit1= these are disk_paths
*/
@ -207,17 +211,55 @@ int Xorriso_set_problem_status(struct XorrisO *xorriso, char *severity,
char *sev_text= "ALL";
int sev, ret;
#ifdef Xorriso_fetch_with_msg_queueS
int locked= 0, uret;
static int complaints= 0, complaint_limit= 5;
#endif
if(severity[0])
sev_text= severity;
ret= Xorriso__text_to_sev(sev_text, &sev, 0);
if(ret<=0)
return(0);
#ifdef Xorriso_fetch_with_msg_queueS
ret= pthread_mutex_lock(&(xorriso->problem_status_lock));
if(ret != 0) {
/* Cannot report failure through the failing message output system */
complaints++;
if(complaints < complaint_limit)
fprintf(stderr,
"xorriso : pthread_mutex_lock() for problem_status returns %d\n",
ret);
} else
locked= 1;
#endif /* Xorriso_fetch_with_msg_queueS */
xorriso->problem_status= sev;
strcpy(xorriso->problem_status_text, sev_text);
if(sev > xorriso->eternal_problem_status) {
xorriso->eternal_problem_status= sev;
strcpy(xorriso->eternal_problem_status_text, sev_text);
}
#ifdef Xorriso_fetch_with_msg_queueS
if(locked) {
uret= pthread_mutex_unlock(&(xorriso->problem_status_lock));
if(uret != 0) {
/* Cannot report failure through the failing message output system */
complaints++;
if(complaints < complaint_limit)
fprintf(stderr,
"xorriso : pthread_mutex_unlock() for problem_status returns %d\n",
uret);
}
}
#endif /* Xorriso_fetch_with_msg_queueS */
return(1);
}

View File

@ -564,12 +564,12 @@ static int Xorriso_write_to_msglist(struct XorrisO *xorriso,
struct Xorriso_lsT **xorriso_msglist,
char *text, int flag)
{
int ret;
int ret, locked= 0;
struct Xorriso_lsT *msglist;
ret= Xorriso_lock_outlists(xorriso, 0);
if(ret <= 0)
return(-1);
if(ret > 0)
locked= 1;
msglist= *xorriso_msglist;
ret= Xorriso_lst_append_binary(&msglist, text, strlen(text) + 1, 0);
if(ret <= 0) {
@ -579,7 +579,8 @@ static int Xorriso_write_to_msglist(struct XorrisO *xorriso,
*xorriso_msglist= msglist;
ret= 1;
ex:;
Xorriso_unlock_outlists(xorriso, 0);
if(locked)
Xorriso_unlock_outlists(xorriso, 0);
return(ret);
}
@ -601,11 +602,30 @@ bit15= with bit1 to bit3: close depicted log file
static int num_channels= 4;
static char channel_prefixes[4][4]= {".","R","I","M"};
#ifdef Xorriso_fetch_with_msg_queueS
static int complaints= 0, complaint_limit= 5;
int locked= 0, uret;
#endif
text= in_text; /* might change due to backslash encoding */
if(channel_no<0 || channel_no>=num_channels)
{ret= -1; goto ex;}
#ifdef Xorriso_fetch_with_msg_queueS
ret= pthread_mutex_lock(&(xorriso->write_to_channel_lock));
if(ret != 0) {
/* Cannot report failure through the failing message output system */
complaints++;
if(complaints <= complaint_limit)
fprintf(stderr,
"xorriso : pthread_mutex_lock() for write_to_channel returns %d\n",
ret);
/* Intentionally not aborting here */;
} else
locked= 1;
#endif /* Xorriso_fetch_with_msg_queueS */
/* Logfiles */
logfile_fp= xorriso->logfile_fp[channel_no];
pktlog_fp= xorriso->pktlog_fp;
@ -772,6 +792,23 @@ bit15= with bit1 to bit3: close depicted log file
ex:
if(text != in_text && text != NULL)
free(text);
#ifdef Xorriso_fetch_with_msg_queueS
if(locked) {
uret= pthread_mutex_unlock(&(xorriso->write_to_channel_lock));
if(uret != 0) {
/* Cannot report failure through the failing message output system */
complaints++;
if(complaints <= complaint_limit)
fprintf(stderr,
"xorriso : pthread_mutex_unlock() for write_to_channel returns %d\n",
uret);
}
}
#endif /* Xorriso_fetch_with_msg_queueS */
return(ret);
}
@ -782,10 +819,8 @@ int Xorriso_push_outlists(struct XorrisO *xorriso, int *stack_handle,
int ret, locked= 0;
ret= Xorriso_lock_outlists(xorriso, 0);
if(ret != 0)
{ret= -1; goto ex;}
locked= 1;
if(ret > 0)
locked= 1;
if(xorriso->msglist_stackfill + 1 >= Xorriso_max_outlist_stacK) {
Xorriso_msgs_submit(xorriso, 0,
"Overflow of message output redirection stack", 0, "FATAL", 0);
@ -832,9 +867,8 @@ int Xorriso_fetch_outlists(struct XorrisO *xorriso, int stack_handle,
flag|= 3;
ret= Xorriso_lock_outlists(xorriso, 0);
if(ret != 0)
{ret= -1; goto ex;}
locked= 1;
if(ret > 0)
locked= 1;
if(stack_handle == -1)
stack_handle= xorriso->msglist_stackfill - 1;
@ -869,9 +903,8 @@ int Xorriso_pull_outlists(struct XorrisO *xorriso, int stack_handle,
int i, ret, locked= 0;
ret= Xorriso_lock_outlists(xorriso, 0);
if(ret != 0)
{ret= -1; goto ex;}
locked= 1;
if(ret > 0)
locked= 1;
if(stack_handle < 0 || stack_handle >= xorriso->msglist_stackfill) {
Xorriso_msgs_submit(xorriso, 0,
@ -1042,7 +1075,11 @@ int Xorriso_process_errfile(struct XorrisO *xorriso,
}
#ifdef Xorriso_fetch_with_msg_queueS
/* Important: This function must stay thread-safe with all use of xorriso. */
#else
/* Note: It is ok to submit xorriso->info_text as msg_text here. */
#endif
/* flag:
bit0= for Xorriso_info() : use pager (as with result)
bit1= for Xorriso_info() : permission to suppress output
@ -1054,13 +1091,14 @@ int Xorriso_process_errfile(struct XorrisO *xorriso,
else: ""
bit6= append carriage return rather than line feed (if not os_errno)
bit7= perform Xorriso_process_msg_queues() first
bit8= do not prepend name prefix and severity
*/
int Xorriso_msgs_submit(struct XorrisO *xorriso,
int error_code, char msg_text[], int os_errno,
char severity[], int flag)
{
int ret, lt, li, sev, i;
char *sev_text= "FATAL", prefix[80];
char *sev_text= "FATAL", prefix[80], *text= NULL;
static char pfx_list[20][16]= {
"xorriso : ", "libisofs: ", "libburn : ", "libisoburn: ",
"", "", "", "", "", "", "", "", "", "", "", "" };
@ -1077,45 +1115,70 @@ int Xorriso_msgs_submit(struct XorrisO *xorriso,
Xorriso__text_to_sev(sev_text, &sev, 0);
else
sev_text= severity;
if(xorriso->problem_status<sev) {
xorriso->problem_status= sev;
strcpy(xorriso->problem_status_text, sev_text);
}
if(xorriso->eternal_problem_status<sev) {
xorriso->eternal_problem_status= sev;
strcpy(xorriso->eternal_problem_status_text, sev_text);
}
if(xorriso->problem_status<sev)
Xorriso_set_problem_status(xorriso, sev_text, 0);
/* Report problem event */
if(sev<xorriso->report_about_severity && sev<xorriso->abort_on_severity)
return(2);
sprintf(prefix,"%s%s : ", pfx_list[(flag>>2)&15], sev_text);
li= strlen(prefix);
{ret= 2; goto ex;}
lt= strlen(msg_text);
if(!(flag & 256)) {
sprintf(prefix,"%s%s : ", pfx_list[(flag>>2)&15], sev_text);
li= strlen(prefix);
} else {
prefix[0]= 0;
li= 0;
}
if(lt > ((int) sizeof(xorriso->info_text)) - li - 2)
lt= sizeof(xorriso->info_text)-li-2;
if(msg_text==xorriso->info_text) {
for(i= lt; i>=0; i--)
msg_text[i+li]= msg_text[i];
for(i=0; i<li; i++)
msg_text[i]= prefix[i];
#ifdef Xorriso_fetch_with_msg_queueS
Xorriso_alloc_meM(text, char, sizeof(xorriso->info_text));
#else /* Xorriso_fetch_with_msg_queueS */
text= xorriso->info_text;
#endif /* ! Xorriso_fetch_with_msg_queueS */
if(msg_text == text) {
if(li > 0) {
for(i= lt; i>=0; i--)
msg_text[i+li]= msg_text[i];
for(i=0; i<li; i++)
msg_text[i]= prefix[i];
}
} else {
strcpy(xorriso->info_text, prefix);
strncpy(xorriso->info_text+li, msg_text, lt);
if(li > 0)
strcpy(text, prefix);
strncpy(text + li, msg_text, lt);
}
if((flag&64) && os_errno<=0)
xorriso->info_text[li+lt]= '\r';
text[li+lt]= '\r';
else
xorriso->info_text[li+lt]= '\n';
xorriso->info_text[li+lt+1]= 0;
text[li+lt]= '\n';
text[li+lt+1]= 0;
if(os_errno>0)
sprintf(text + strlen(text) - 1, " : %s\n", strerror(os_errno));
#ifdef Xorriso_fetch_with_msg_queueS
Xorriso_write_to_channel(xorriso, text, 2, 0);
#else /* Xorriso_fetch_with_msg_queueS */
if(os_errno>0) {
sprintf(xorriso->info_text + strlen(xorriso->info_text) - 1,
" : %s\n", strerror(os_errno));
}
Xorriso_info(xorriso,4|(flag&3));
return(1);
#endif /* ! Xorriso_fetch_with_msg_queueS */
ex:;
#ifdef Xorriso_fetch_with_msg_queueS
Xorriso_free_meM(text);
#endif /* ! Xorriso_fetch_with_msg_queueS */
return(ret);
}

View File

@ -19,6 +19,12 @@
#define Xorriso_private_includeD yes
/* <<< Disable this to disable pthread_mutex locking on message and result
output.
*/
#define Xorriso_fetch_with_msg_queueS yes
/* for uint32_t */
#ifdef HAVE_STDINT_H
#include <stdint.h>
@ -566,6 +572,9 @@ struct XorrisO { /* the global context of xorriso */
pthread_mutex_t lib_msg_queue_lock;
pthread_mutex_t result_msglists_lock;
int write_to_channel_lock_ini;
pthread_mutex_t write_to_channel_lock;
int status_history_max; /* for -status long_history */
/* 0= no logging of SCSI commands, 1= to stderr */
@ -579,6 +588,8 @@ struct XorrisO { /* the global context of xorriso */
int abort_on_is_default; /* will be set to 0 by first -abort_on */
int problem_status; /* Severity rank number. 0= no abort condition present */
char problem_status_text[20];
int problem_status_lock_ini;
pthread_mutex_t problem_status_lock;
char errfile_log[SfileadrL]; /* for -errfile_log */
int errfile_mode; /* bit0= marked */

View File

@ -1 +1 @@
#define Xorriso_timestamP "2012.09.11.122559"
#define Xorriso_timestamP "2012.09.13.130910"