Valgrind fixes
[multipath-tools/.git] / libmultipath / log_pthread.c
1 /*
2  * Copyright (c) 2005 Christophe Varoqui
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <pthread.h>
8 #include <sys/mman.h>
9
10 #include <memory.h>
11
12 #include "log_pthread.h"
13 #include "log.h"
14 #include "lock.h"
15
16 void log_safe (int prio, const char * fmt, va_list ap)
17 {
18         sigset_t old;
19
20         block_signal(SIGUSR1, &old);
21         block_signal(SIGHUP, NULL);
22
23         pthread_mutex_lock(logq_lock);
24         log_enqueue(prio, fmt, ap);
25         pthread_mutex_unlock(logq_lock);
26
27         pthread_mutex_lock(logev_lock);
28         pthread_cond_signal(logev_cond);
29         pthread_mutex_unlock(logev_lock);
30
31         pthread_sigmask(SIG_SETMASK, &old, NULL);
32 }
33
34 static void flush_logqueue (void)
35 {
36         int empty;
37
38         do {
39                 pthread_mutex_lock(logq_lock);
40                 empty = log_dequeue(la->buff);
41                 pthread_mutex_unlock(logq_lock);
42                 if (!empty)
43                         log_syslog(la->buff);
44         } while (empty == 0);
45 }
46
47 static void * log_thread (void * et)
48 {
49         mlockall(MCL_CURRENT | MCL_FUTURE);
50         logdbg(stderr,"enter log_thread\n");
51
52         while (1) {
53                 pthread_mutex_lock(logev_lock);
54                 pthread_cond_wait(logev_cond, logev_lock);
55                 pthread_mutex_unlock(logev_lock);
56
57                 flush_logqueue();
58         }
59 }
60
61 void log_thread_start (pthread_attr_t *attr)
62 {
63         logdbg(stderr,"enter log_thread_start\n");
64
65         logq_lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
66         logev_lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
67         logev_cond = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
68
69         pthread_mutex_init(logq_lock, NULL);
70         pthread_mutex_init(logev_lock, NULL);
71         pthread_cond_init(logev_cond, NULL);
72
73         if (log_init("multipathd", 0)) {
74                 fprintf(stderr,"can't initialize log buffer\n");
75                 exit(1);
76         }
77         pthread_create(&log_thr, attr, log_thread, NULL);
78
79         return;
80 }
81
82 void log_thread_stop (void)
83 {
84         logdbg(stderr,"enter log_thread_stop\n");
85
86         pthread_mutex_lock(logq_lock);
87         pthread_cancel(log_thr);
88         pthread_mutex_unlock(logq_lock);
89
90         flush_logqueue();
91
92         pthread_mutex_destroy(logq_lock);
93         pthread_mutex_destroy(logev_lock);
94         pthread_cond_destroy(logev_cond);
95
96         free(logq_lock);
97         logq_lock = NULL;
98         free(logev_lock);
99         logev_lock = NULL;
100         free(logev_cond);
101         logev_cond = NULL;
102         free_logarea();
103 }