Fix FSF address in license text according to
[platform/upstream/cryptsetup.git] / docs / examples / crypt_log_usage.c
1 /*
2  * An example of using logging through libcryptsetup API
3  *
4  * Copyright (C) 2011, 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 function that can be registered using crypt_set_log_callback API.
29  *
30  * Its prototype is void (*log)(int level, const char *msg, void *usrptr) as defined
31  * in crypt_set_log_callback
32  */
33 static void simple_syslog_wrapper(int level, const char *msg, void *usrptr)
34 {
35         const char *prefix = (const char *)usrptr;
36         int priority;
37
38         switch(level) {
39                 case CRYPT_LOG_NORMAL:  priority = LOG_NOTICE; break;
40                 case CRYPT_LOG_ERROR:   priority = LOG_ERR;    break;
41                 case CRYPT_LOG_VERBOSE: priority = LOG_INFO;   break;
42                 case CRYPT_LOG_DEBUG:   priority = LOG_DEBUG;  break;
43                 default:
44                         fprintf(stderr, "Unsupported log level requested!\n");
45                         return;
46         }
47
48         if (prefix)
49                 syslog(priority, "%s:%s", prefix, msg);
50         else
51                 syslog(priority, "%s", msg);
52 }
53
54 int main(void)
55 {
56         struct crypt_device *cd;
57         char usrprefix[] = "cslog_example";
58         int r;
59
60         if (geteuid()) {
61                 printf("Using of libcryptsetup requires super user privileges.\n");
62                 return 1;
63         }
64
65         openlog("cryptsetup", LOG_CONS | LOG_PID, LOG_USER);
66
67         /* Initialize empty crypt device context */
68         r = crypt_init(&cd, NULL);
69         if (r < 0) {
70                 printf("crypt_init() failed.\n");
71                 return 2;
72         }
73
74         /* crypt_set_log_callback() - register a log function for crypt context */
75         crypt_set_log_callback(cd, &simple_syslog_wrapper, (void *)usrprefix);
76
77         /* send messages ithrough the crypt_log() interface */
78         crypt_log(cd, CRYPT_LOG_NORMAL, "This is normal log message");
79         crypt_log(cd, CRYPT_LOG_ERROR, "This is error log message");
80         crypt_log(cd, CRYPT_LOG_VERBOSE, "This is verbose log message");
81         crypt_log(cd, CRYPT_LOG_DEBUG, "This is debug message");
82
83         /* release crypt context */
84         crypt_free(cd);
85
86         /* Initialize default (global) log function */
87         crypt_set_log_callback(NULL, &simple_syslog_wrapper, NULL);
88
89         crypt_log(NULL, CRYPT_LOG_NORMAL, "This is normal log message");
90         crypt_log(NULL, CRYPT_LOG_ERROR, "This is error log message");
91         crypt_log(NULL, CRYPT_LOG_VERBOSE, "This is verbose log message");
92         crypt_log(NULL, CRYPT_LOG_DEBUG, "This is debug message");
93
94         closelog();
95         return 0;
96 }