Made Xorriso_process_msg_queues() thread-safe

This commit is contained in:
2012-09-13 13:09:54 +00:00
parent a2507830fb
commit cce834f76f
6 changed files with 220 additions and 62 deletions

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);
}