"Fossies" - the Fresh Open Source Software Archive

Member "xorriso-1.5.4/libisofs/libiso_msgs.c" (30 Jan 2021, 10663 Bytes) of package /linux/misc/xorriso-1.5.4.pl02.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "libiso_msgs.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.5.1_vs_1.5.2.

    1 
    2 /* libiso_msgs   (generated from libdax_msgs : Fri Feb 22 19:42:52 CET 2008)
    3    Message handling facility of libisofs.
    4    Copyright (C) 2006 - 2016 Thomas Schmitt <scdbackup@gmx.net>,
    5 
    6    This file is part of the libisofs project; you can redistribute it and/or
    7    modify it under the terms of the GNU General Public License version 2
    8    or later as published by the Free Software Foundation.
    9    See COPYING file for details.
   10 
   11 */
   12 
   13 #ifdef HAVE_CONFIG_H
   14 #include "../config.h"
   15 #endif
   16 
   17 #include <stdio.h>
   18 #include <sys/types.h>
   19 #include <unistd.h>
   20 #include <string.h>
   21 #include <stdlib.h>
   22 #include <errno.h>
   23 #include <sys/time.h>
   24 
   25 /* Only this single source module is entitled to do this */ 
   26 #define LIBISO_MSGS_H_INTERNAL 1
   27 
   28 /* All participants in the messaging system must do this */
   29 #include "libiso_msgs.h"
   30 
   31 
   32 /* ----------------------------- libiso_msgs_item ------------------------- */
   33 
   34 
   35 static int libiso_msgs_item_new(struct libiso_msgs_item **item,
   36                          struct libiso_msgs_item *link, int flag)
   37 {
   38  int ret;
   39  struct libiso_msgs_item *o;
   40  struct timeval tv;
   41 
   42  (*item)= o= 
   43            (struct libiso_msgs_item *) malloc(sizeof(struct libiso_msgs_item));
   44  if(o==NULL)
   45    return(-1);
   46  o->timestamp= 0.0;
   47  ret= gettimeofday(&tv, NULL);
   48  if(ret==0)
   49    o->timestamp= tv.tv_sec+0.000001*tv.tv_usec;
   50  o->process_id= getpid();
   51  o->origin= -1;
   52  o->severity= LIBISO_MSGS_SEV_ALL;
   53  o->priority= LIBISO_MSGS_PRIO_ZERO;
   54  o->error_code= 0;
   55  o->msg_text= NULL;
   56  o->os_errno= 0;
   57  o->prev= link;
   58  o->next= NULL;
   59  if(link!=NULL) {
   60    if(link->next!=NULL) {
   61      link->next->prev= o;
   62      o->next= link->next;
   63    }
   64    link->next= o;
   65  }
   66  return(1);
   67 }
   68 
   69 
   70 /** Detaches item from its queue and eventually readjusts start, end pointers
   71     of the queue */
   72 int libiso_msgs_item_unlink(struct libiso_msgs_item *o,
   73                             struct libiso_msgs_item **chain_start,
   74                             struct libiso_msgs_item **chain_end, int flag)
   75 {
   76  if(o->prev!=NULL)
   77    o->prev->next= o->next;
   78  if(o->next!=NULL)
   79    o->next->prev= o->prev;
   80  if(chain_start!=NULL)
   81    if(*chain_start == o)
   82      *chain_start= o->next;
   83  if(chain_end!=NULL)
   84    if(*chain_end == o)
   85      *chain_end= o->prev;
   86  o->next= o->prev= NULL;
   87  return(1);
   88 }
   89 
   90 
   91 int libiso_msgs_item_destroy(struct libiso_msgs_item **item,
   92                              int flag)
   93 {
   94  struct libiso_msgs_item *o;
   95 
   96  o= *item;
   97  if(o==NULL)
   98    return(0);
   99  libiso_msgs_item_unlink(o,NULL,NULL,0); 
  100  if(o->msg_text!=NULL)
  101    free((char *) o->msg_text);
  102  free((char *) o);
  103  *item= NULL;
  104  return(1);
  105 }
  106 
  107 
  108 int libiso_msgs_item_get_msg(struct libiso_msgs_item *item,
  109                              int *error_code, char **msg_text, int *os_errno,
  110                              int flag)
  111 {
  112  *error_code= item->error_code;
  113  *msg_text= item->msg_text;
  114  *os_errno= item->os_errno;
  115  return(1);
  116 }
  117 
  118 
  119 int libiso_msgs_item_get_origin(struct libiso_msgs_item *item,
  120                    double *timestamp, pid_t *process_id, int *origin,
  121                    int flag)
  122 {
  123  *timestamp= item->timestamp;
  124  *process_id= item->process_id;
  125  *origin= item->origin;
  126  return(1);
  127 }
  128 
  129 
  130 int libiso_msgs_item_get_rank(struct libiso_msgs_item *item,
  131                               int *severity, int *priority, int flag)
  132 {
  133  *severity= item->severity;
  134  *priority= item->priority;
  135  return(1);
  136 }
  137 
  138 
  139 /* ------------------------------- libiso_msgs ---------------------------- */
  140 
  141 
  142 int libiso_msgs_new(struct libiso_msgs **m, int flag)
  143 {
  144  struct libiso_msgs *o;
  145 
  146  (*m)= o= (struct libiso_msgs *) malloc(sizeof(struct libiso_msgs));
  147  if(o==NULL)
  148    return(-1);
  149  o->refcount= 1;
  150  o->oldest= NULL;
  151  o->youngest= NULL;
  152  o->count= 0;
  153  o->queue_severity= LIBISO_MSGS_SEV_ALL;
  154  o->print_severity= LIBISO_MSGS_SEV_NEVER;
  155  strcpy(o->print_id,"libiso: ");
  156 
  157 #ifndef LIBISO_MSGS_SINGLE_THREADED
  158  pthread_mutex_init(&(o->lock_mutex),NULL);
  159 #endif
  160 
  161  return(1);
  162 }
  163 
  164 
  165 static int libiso_msgs_lock(struct libiso_msgs *m, int flag)
  166 {
  167 
  168 #ifndef LIBISO_MSGS_SINGLE_THREADED
  169  int ret;
  170 
  171  ret= pthread_mutex_lock(&(m->lock_mutex));
  172  if(ret!=0)
  173    return(0);
  174 #endif
  175 
  176  return(1);
  177 }
  178 
  179 
  180 static int libiso_msgs_unlock(struct libiso_msgs *m, int flag)
  181 {
  182 
  183 #ifndef LIBISO_MSGS_SINGLE_THREADED
  184  int ret;
  185 
  186  ret= pthread_mutex_unlock(&(m->lock_mutex));
  187  if(ret!=0)
  188    return(0);
  189 #endif
  190 
  191  return(1);
  192 }
  193 
  194 
  195 int libiso_msgs_destroy(struct libiso_msgs **m, int flag)
  196 {
  197  struct libiso_msgs *o;
  198  struct libiso_msgs_item *item, *next_item;
  199 
  200  o= *m;
  201  if(o==NULL)
  202    return(0);
  203  if(o->refcount > 1) {
  204    if(libiso_msgs_lock(*m,0)<=0)
  205      return(-1);
  206    o->refcount--;
  207    libiso_msgs_unlock(*m,0);
  208    *m= NULL;
  209    return(1);
  210  }
  211 
  212 #ifndef LIBISO_MSGS_SINGLE_THREADED
  213  if(pthread_mutex_destroy(&(o->lock_mutex))!=0) {
  214    pthread_mutex_unlock(&(o->lock_mutex));
  215    pthread_mutex_destroy(&(o->lock_mutex));
  216  }
  217 #endif
  218 
  219  for(item= o->oldest; item!=NULL; item= next_item) {
  220    next_item= item->next;
  221    libiso_msgs_item_destroy(&item,0);
  222  }
  223  free((char *) o);
  224  *m= NULL;
  225  return(1);
  226 }
  227 
  228 
  229 int libiso_msgs_refer(struct libiso_msgs **pt, struct libiso_msgs *m, int flag)
  230 {
  231  if(libiso_msgs_lock(m,0)<=0)
  232    return(0);
  233  m->refcount++;
  234  *pt= m;
  235  libiso_msgs_unlock(m,0);
  236  return(1);
  237 }
  238 
  239 
  240 int libiso_msgs_set_severities(struct libiso_msgs *m, int queue_severity,
  241                                int print_severity, char *print_id, int flag)
  242 {
  243  if(libiso_msgs_lock(m,0)<=0)
  244    return(0);
  245  m->queue_severity= queue_severity;
  246  m->print_severity= print_severity;
  247  strncpy(m->print_id,print_id,80);
  248  m->print_id[80]= 0;
  249  libiso_msgs_unlock(m,0);
  250  return(1);
  251 }
  252 
  253 
  254 int libiso_msgs__text_to_sev(char *severity_name, int *severity,
  255                              int flag)
  256 {
  257  if(strncmp(severity_name,"NEVER",5)==0)
  258    *severity= LIBISO_MSGS_SEV_NEVER;
  259  else if(strncmp(severity_name,"ABORT",5)==0)
  260    *severity= LIBISO_MSGS_SEV_ABORT;
  261  else if(strncmp(severity_name,"FATAL",5)==0)
  262    *severity= LIBISO_MSGS_SEV_FATAL;
  263  else if(strncmp(severity_name,"FAILURE",7)==0)
  264    *severity= LIBISO_MSGS_SEV_FAILURE;
  265  else if(strncmp(severity_name,"MISHAP",6)==0)
  266    *severity= LIBISO_MSGS_SEV_MISHAP;
  267  else if(strncmp(severity_name,"SORRY",5)==0)
  268    *severity= LIBISO_MSGS_SEV_SORRY;
  269  else if(strncmp(severity_name,"WARNING",7)==0)
  270    *severity= LIBISO_MSGS_SEV_WARNING;
  271  else if(strncmp(severity_name,"HINT",4)==0)
  272    *severity= LIBISO_MSGS_SEV_HINT;
  273  else if(strncmp(severity_name,"NOTE",4)==0)
  274    *severity= LIBISO_MSGS_SEV_NOTE;
  275  else if(strncmp(severity_name,"UPDATE",6)==0)
  276    *severity= LIBISO_MSGS_SEV_UPDATE;
  277  else if(strncmp(severity_name,"DEBUG",5)==0)
  278    *severity= LIBISO_MSGS_SEV_DEBUG;
  279  else if(strncmp(severity_name,"ERRFILE",7)==0)
  280    *severity= LIBISO_MSGS_SEV_ERRFILE;
  281  else if(strncmp(severity_name,"ALL",3)==0)
  282    *severity= LIBISO_MSGS_SEV_ALL;
  283  else {
  284    *severity= LIBISO_MSGS_SEV_ALL;
  285    return(0);
  286  }
  287  return(1);
  288 }
  289 
  290 
  291 int libiso_msgs__sev_to_text(int severity, char **severity_name,
  292                              int flag)
  293 {
  294  if(flag&1) {
  295    *severity_name= "ALL ERRFILE DEBUG UPDATE NOTE HINT WARNING SORRY MISHAP FAILURE FATAL ABORT NEVER";
  296    return(1);
  297  }
  298  *severity_name= "";
  299  if(severity>=LIBISO_MSGS_SEV_NEVER)
  300    *severity_name= "NEVER";
  301  else if(severity>=LIBISO_MSGS_SEV_ABORT)
  302    *severity_name= "ABORT";
  303  else if(severity>=LIBISO_MSGS_SEV_FATAL)
  304    *severity_name= "FATAL";
  305  else if(severity>=LIBISO_MSGS_SEV_FAILURE)
  306    *severity_name= "FAILURE";
  307  else if(severity>=LIBISO_MSGS_SEV_MISHAP)
  308    *severity_name= "MISHAP";
  309  else if(severity>=LIBISO_MSGS_SEV_SORRY)
  310    *severity_name= "SORRY";
  311  else if(severity>=LIBISO_MSGS_SEV_WARNING)
  312    *severity_name= "WARNING";
  313  else if(severity>=LIBISO_MSGS_SEV_HINT)
  314    *severity_name= "HINT";
  315  else if(severity>=LIBISO_MSGS_SEV_NOTE)
  316    *severity_name= "NOTE";
  317  else if(severity>=LIBISO_MSGS_SEV_UPDATE)
  318    *severity_name= "UPDATE";
  319  else if(severity>=LIBISO_MSGS_SEV_DEBUG)
  320    *severity_name= "DEBUG";
  321  else if(severity>=LIBISO_MSGS_SEV_ERRFILE)
  322    *severity_name= "ERRFILE";
  323  else if(severity>=LIBISO_MSGS_SEV_ALL)
  324    *severity_name= "ALL";
  325  else {
  326    *severity_name= "";
  327    return(0);
  328  }
  329  return(1);
  330 }
  331 
  332 
  333 int libiso_msgs_submit(struct libiso_msgs *m, int origin, int error_code,
  334                        int severity, int priority, char *msg_text,
  335                        int os_errno, int flag)
  336 {
  337  int ret;
  338  char *textpt,*sev_name,sev_text[81];
  339  struct libiso_msgs_item *item= NULL;
  340 
  341  if(severity >= m->print_severity) {
  342    if(msg_text==NULL)
  343      textpt= "";
  344    else
  345      textpt= msg_text;
  346    sev_text[0]= 0;
  347    ret= libiso_msgs__sev_to_text(severity,&sev_name,0);
  348    if(ret>0)
  349      sprintf(sev_text,"%s : ",sev_name);
  350 
  351    fprintf(stderr,"%s%s%s\n",m->print_id,sev_text,textpt);
  352    if(os_errno!=0) {
  353      ret= libiso_msgs_lock(m,0);
  354      if(ret<=0)
  355        return(-1);
  356      fprintf(stderr,"%s( Most recent system error: %d  '%s' )\n",
  357                     m->print_id,os_errno,strerror(os_errno));
  358      libiso_msgs_unlock(m,0);
  359    }
  360 
  361  }
  362  if(severity < m->queue_severity)
  363    return(0);
  364 
  365  ret= libiso_msgs_lock(m,0);
  366  if(ret<=0)
  367    return(-1);
  368  ret= libiso_msgs_item_new(&item,m->youngest,0);
  369  if(ret<=0)
  370    goto failed;
  371  item->origin= origin;
  372  item->error_code= error_code;
  373  item->severity= severity;
  374  item->priority= priority;
  375  if(msg_text!=NULL) {
  376    item->msg_text= malloc(strlen(msg_text)+1);
  377    if(item->msg_text==NULL)
  378      goto failed;
  379    strcpy(item->msg_text,msg_text);
  380  }
  381  item->os_errno= os_errno;
  382  if(m->oldest==NULL)
  383    m->oldest= item;
  384  m->youngest= item;
  385  m->count++;
  386  libiso_msgs_unlock(m,0);
  387 
  388 /*
  389 fprintf(stderr,"libiso_experimental: message submitted to queue (now %d)\n",
  390                 m->count);
  391 */
  392 
  393  return(1);
  394 failed:;
  395  libiso_msgs_item_destroy(&item,0);
  396  libiso_msgs_unlock(m,0);
  397  return(-1);
  398 }
  399 
  400 
  401 int libiso_msgs_obtain(struct libiso_msgs *m, struct libiso_msgs_item **item,
  402                        int severity, int priority, int flag)
  403 {
  404  int ret;
  405  struct libiso_msgs_item *im, *next_im= NULL;
  406 
  407  *item= NULL;
  408  ret= libiso_msgs_lock(m,0);
  409  if(ret<=0)
  410    return(-1);
  411  for(im= m->oldest; im!=NULL; im= next_im) {
  412    for(; im!=NULL; im= next_im) {
  413      next_im= im->next;
  414      if(im->severity>=severity)
  415    break;
  416      libiso_msgs_item_unlink(im,&(m->oldest),&(m->youngest),0);
  417      libiso_msgs_item_destroy(&im,0); /* severity too low: delete */
  418    }
  419    if(im==NULL)
  420  break;
  421    if(im->priority>=priority)
  422  break;
  423  }
  424  if(im==NULL)
  425    {ret= 0; goto ex;}
  426  libiso_msgs_item_unlink(im,&(m->oldest),&(m->youngest),0);
  427  *item= im;
  428  ret= 1;
  429 ex:;
  430  libiso_msgs_unlock(m,0);
  431  return(ret);
  432 }
  433 
  434 
  435 int libiso_msgs_destroy_item(struct libiso_msgs *m,
  436                              struct libiso_msgs_item **item, int flag)
  437 {
  438  int ret;
  439 
  440  ret= libiso_msgs_lock(m,0);
  441  if(ret<=0)
  442    return(-1);
  443  ret= libiso_msgs_item_destroy(item,0);
  444  libiso_msgs_unlock(m,0);
  445  return(ret);
  446 }
  447