Changeset 229 in openpam


Ignore:
Timestamp:
May 24, 2003, 7:32:38 PM (16 years ago)
Author:
Dag-Erling Smørgrav
Message:

Reorganize. Document. Replace malloc() + sprintf() with asprintf().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/openpam_log.c

    r227 r229  
    3232 * SUCH DAMAGE.
    3333 *
    34  * $P4: //depot/projects/openpam/lib/openpam_log.c#20 $
     34 * $P4: //depot/projects/openpam/lib/openpam_log.c#21 $
    3535 */
    3636
     
    4848int _openpam_debug = 0;
    4949
    50 #if defined(openpam_log)
     50#if !defined(openpam_log)
    5151
    5252/*
    5353 * OpenPAM extension
    5454 *
    55  * Log a message through syslog(3)
    56  */
    57 
    58 void
    59 _openpam_log(int level, const char *func, const char *fmt, ...)
    60 {
    61         va_list ap;
    62         char *format;
    63         int len, priority;
    64 
    65         switch (level) {
    66         case PAM_LOG_DEBUG:
    67                 if (!_openpam_debug)
    68                         return;
    69                 priority = LOG_DEBUG;
    70                 break;
    71         case PAM_LOG_VERBOSE:
    72                 priority = LOG_INFO;
    73                 break;
    74         case PAM_LOG_NOTICE:
    75                 priority = LOG_NOTICE;
    76                 break;
    77         case PAM_LOG_ERROR:
    78         default:
    79                 priority = LOG_ERR;
    80                 break;
    81         }
    82         va_start(ap, fmt);
    83         for (len = strlen(fmt); len > 0 && isspace(fmt[len]); len--)
    84                 /* nothing */;
    85         if ((format = malloc(strlen(func) + len + 16)) != NULL) {
    86                 sprintf(format, "in %s(): %.*s\n", func, len, fmt);
    87                 vsyslog(priority, format, ap);
    88                 free(format);
    89         } else {
    90                 vsyslog(priority, fmt, ap);
    91         }
    92         va_end(ap);
    93 }
    94 
    95 #else
    96 
    97 /*
    98  * If openpam_log isn't defined as a macro, we're on a platform that
    99  * doesn't support varadic macros (or it does but we aren't aware of
    100  * it).  Do the next best thing.
     55 * Log a message through syslog
    10156 */
    10257
     
    12984}
    13085
     86#else
     87
     88void
     89_openpam_log(int level, const char *func, const char *fmt, ...)
     90{
     91        va_list ap;
     92        char *format;
     93        int priority;
     94
     95        switch (level) {
     96        case PAM_LOG_DEBUG:
     97                if (!_openpam_debug)
     98                        return;
     99                priority = LOG_DEBUG;
     100                break;
     101        case PAM_LOG_VERBOSE:
     102                priority = LOG_INFO;
     103                break;
     104        case PAM_LOG_NOTICE:
     105                priority = LOG_NOTICE;
     106                break;
     107        case PAM_LOG_ERROR:
     108        default:
     109                priority = LOG_ERR;
     110                break;
     111        }
     112        va_start(ap, fmt);
     113        if (asprintf(&format, "in %s(): %s", func, fmt) > 0) {
     114                vsyslog(priority, format, ap);
     115                free(format);
     116        } else {
     117                vsyslog(priority, fmt, ap);
     118        }
     119        va_end(ap);
     120}
     121
    131122#endif
     123
     124/**
     125 * The =openpam_log function logs messages using =syslog.  It is primarily
     126 * intended for internal use by the library and modules.
     127 *
     128 * The =level argument indicates the importance of the message.  The
     129 * following levels are defined:
     130 *
     131 *      =PAM_LOG_DEBUG:
     132 *              Debugging messages.  These messages are normally not
     133 *              logged unless the global integer variable :_openpam_debug
     134 *              is set to a non-zero value, in which case they are logged
     135 *              with a =syslog priority of =LOG_DEBUG.
     136 *      =PAM_LOG_VERBOSE:
     137 *              Information about the progress of the authentication
     138 *              process, or other non-essential messages.  These messages
     139 *              are logged with a =syslog priority of =LOG_INFO.
     140 *      =PAM_LOG_NOTICE:
     141 *              Messages relating to non-fatal errors.  These messages are
     142 *              logged with a =syslog priority of =LOG_NOTICE.
     143 *      =PAM_LOG_ERROR:
     144 *              Messages relating to serious errors.  These messages are
     145 *              logged with a =syslog priority of =LOG_ERR.
     146 *
     147 * The remaining arguments are a =printf format string and the
     148 * corresponding arguments.
     149 */
Note: See TracChangeset for help on using the changeset viewer.