"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/docs/examples/crypt_log_usage.c" (13 Jan 2022, 2865 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


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.

    1 /*
    2  * libcryptsetup API log example
    3  *
    4  * Copyright (C) 2011-2021 Red Hat, Inc. All rights reserved.
    5  *
    6  * This file is free software; you can redistribute it and/or
    7  * modify it under the terms of the GNU Lesser General Public
    8  * License as published by the Free Software Foundation; either
    9  * version 2.1 of the License, or (at your option) any later version.
   10  *
   11  * This file is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14  * Lesser General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU Lesser General Public
   17  * License along with this file; if not, write to the Free Software
   18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   19  */
   20 
   21 #include <stdio.h>
   22 #include <sys/types.h>
   23 #include <syslog.h>
   24 #include <unistd.h>
   25 #include <libcryptsetup.h>
   26 
   27 /*
   28  * This is an example of crypt_set_log_callback API callback.
   29  *
   30  */
   31 static void simple_syslog_wrapper(int level, const char *msg, void *usrptr)
   32 {
   33     const char *prefix = (const char *)usrptr;
   34     int priority;
   35 
   36     switch(level) {
   37         case CRYPT_LOG_NORMAL:  priority = LOG_NOTICE; break;
   38         case CRYPT_LOG_ERROR:   priority = LOG_ERR;    break;
   39         case CRYPT_LOG_VERBOSE: priority = LOG_INFO;   break;
   40         case CRYPT_LOG_DEBUG:   priority = LOG_DEBUG;  break;
   41         default:
   42             fprintf(stderr, "Unsupported log level requested!\n");
   43             return;
   44     }
   45 
   46     if (prefix)
   47         syslog(priority, "%s:%s", prefix, msg);
   48     else
   49         syslog(priority, "%s", msg);
   50 }
   51 
   52 int main(void)
   53 {
   54     struct crypt_device *cd;
   55     char usrprefix[] = "cslog_example";
   56     int r;
   57 
   58     if (geteuid()) {
   59         printf("Using of libcryptsetup requires super user privileges.\n");
   60         return 1;
   61     }
   62 
   63     openlog("cryptsetup", LOG_CONS | LOG_PID, LOG_USER);
   64 
   65     /* Initialize empty crypt device context */
   66     r = crypt_init(&cd, NULL);
   67     if (r < 0) {
   68         printf("crypt_init() failed.\n");
   69         return 2;
   70     }
   71 
   72     /* crypt_set_log_callback() - register a log callback for crypt context */
   73     crypt_set_log_callback(cd, &simple_syslog_wrapper, (void *)usrprefix);
   74 
   75     /* send messages ithrough the crypt_log() interface */
   76     crypt_log(cd, CRYPT_LOG_NORMAL, "This is normal log message");
   77     crypt_log(cd, CRYPT_LOG_ERROR, "This is error log message");
   78     crypt_log(cd, CRYPT_LOG_VERBOSE, "This is verbose log message");
   79     crypt_log(cd, CRYPT_LOG_DEBUG, "This is debug message");
   80 
   81     /* release crypt context */
   82     crypt_free(cd);
   83 
   84     /* Initialize default (global) log callback */
   85     crypt_set_log_callback(NULL, &simple_syslog_wrapper, NULL);
   86 
   87     crypt_log(NULL, CRYPT_LOG_NORMAL, "This is normal log message");
   88     crypt_log(NULL, CRYPT_LOG_ERROR, "This is error log message");
   89     crypt_log(NULL, CRYPT_LOG_VERBOSE, "This is verbose log message");
   90     crypt_log(NULL, CRYPT_LOG_DEBUG, "This is debug message");
   91 
   92     closelog();
   93     return 0;
   94 }