Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / docs / examples / crypt_log_usage.c
index 778b0cb..d8364af 100644 (file)
@@ -1,98 +1,94 @@
+/*
+ * libcryptsetup API log example
+ *
+ * Copyright (C) 2011-2020 Red Hat, Inc. All rights reserved.
+ *
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <syslog.h>
 #include <unistd.h>
 #include <libcryptsetup.h>
 
-#include "crypt_examples.h"
-
-#define LOG_PREFIX_CD  "cslog_example_prefix"
-
-int log_ready = 0;
-
 /*
- * This is an example of function that can be registered using crypt_set_log_callback API.
+ * This is an example of crypt_set_log_callback API callback.
  *
- * Its prototype is void (*log)(int level, const char *msg, void *usrptr) as defined
- * in crypt_set_log_callback
- *
- * NOTE: that some syslog message levels may not be visible with respect to your
- *      syslog setings
- *
- *      If your syslog daemon is turned off, messages should be printed to stderr
  */
 static void simple_syslog_wrapper(int level, const char *msg, void *usrptr)
 {
-       if(!log_ready) {
-               openlog((char *)usrptr, LOG_CONS | LOG_PID, LOG_USER);
-               log_ready = 1;
-       }
+       const char *prefix = (const char *)usrptr;
+       int priority;
+
        switch(level) {
-               case CRYPT_LOG_NORMAL:
-                       syslog(LOG_NOTICE, msg);
-                       break;
-               case CRYPT_LOG_ERROR:
-                       syslog(LOG_ERR, msg);
-                       break;
-               case CRYPT_LOG_VERBOSE:
-                       syslog(LOG_INFO, msg);
-                       break;
-               case CRYPT_LOG_DEBUG:
-                       syslog(LOG_DEBUG, msg);
-                       break;
+               case CRYPT_LOG_NORMAL:  priority = LOG_NOTICE; break;
+               case CRYPT_LOG_ERROR:   priority = LOG_ERR;    break;
+               case CRYPT_LOG_VERBOSE: priority = LOG_INFO;   break;
+               case CRYPT_LOG_DEBUG:   priority = LOG_DEBUG;  break;
                default:
                        fprintf(stderr, "Unsupported log level requested!\n");
+                       return;
        }
+
+       if (prefix)
+               syslog(priority, "%s:%s", prefix, msg);
+       else
+               syslog(priority, "%s", msg);
 }
 
 int main(void)
 {
-       int step = 0, r = 0;
        struct crypt_device *cd;
+       char usrprefix[] = "cslog_example";
+       int r;
 
-       if (geteuid())
-               fprintf(stderr, "WARN: Process doesn't have super user privileges. "
-                               "Most of examples will fail because of that.\n");
+       if (geteuid()) {
+               printf("Using of libcryptsetup requires super user privileges.\n");
+               return 1;
+       }
+
+       openlog("cryptsetup", LOG_CONS | LOG_PID, LOG_USER);
 
-       EX_STEP(++step, "crypt_init() to get an empty device context");
-       if ((r = crypt_init(&cd, NULL))) {
-               EX_FAIL("crypt_init() failed.");
-               return r;
+       /* Initialize empty crypt device context */
+       r = crypt_init(&cd, NULL);
+       if (r < 0) {
+               printf("crypt_init() failed.\n");
+               return 2;
        }
-       EX_SUCCESS("crypt_init() successfull");
 
-       EX_STEP(++step, "crypt_set_log_callback() to register a log function tied with context");
-       crypt_set_log_callback(cd, &simple_syslog_wrapper, LOG_PREFIX_CD);
-       EX_SUCCESS("");
-       EX_DELIM;
+       /* crypt_set_log_callback() - register a log callback for crypt context */
+       crypt_set_log_callback(cd, &simple_syslog_wrapper, (void *)usrprefix);
 
-       EX_STEP(++step, "multiple crypt_log() to send messages into the context set log function. "
-                       "The messages should be prefixed with '" LOG_PREFIX_CD "'");
+       /* send messages ithrough the crypt_log() interface */
        crypt_log(cd, CRYPT_LOG_NORMAL, "This is normal log message");
        crypt_log(cd, CRYPT_LOG_ERROR, "This is error log message");
        crypt_log(cd, CRYPT_LOG_VERBOSE, "This is verbose log message");
        crypt_log(cd, CRYPT_LOG_DEBUG, "This is debug message");
-       EX_SUCCESS("");
-       EX_DELIM;
 
+       /* release crypt context */
        crypt_free(cd);
 
-       if (log_ready)
-               closelog();
-
-       log_ready = 0;
-
-       EX_STEP(++step, "crypt_set_log_callback() to register a default (global) log function");
+       /* Initialize default (global) log callback */
        crypt_set_log_callback(NULL, &simple_syslog_wrapper, NULL);
-       EX_SUCCESS("");
-       EX_DELIM;
 
-       EX_STEP(++step, "multiple crypt_log() to send messages into default log");
        crypt_log(NULL, CRYPT_LOG_NORMAL, "This is normal log message");
        crypt_log(NULL, CRYPT_LOG_ERROR, "This is error log message");
        crypt_log(NULL, CRYPT_LOG_VERBOSE, "This is verbose log message");
        crypt_log(NULL, CRYPT_LOG_DEBUG, "This is debug message");
-       EX_SUCCESS("");
 
-       return r;
+       closelog();
+       return 0;
 }