1 /*
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
4  * Author: Angus Salkeld <asalkeld@redhat.com>
5  *
6  * This file is part of libqb.
7  *
8  * libqb is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * libqb is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 #ifndef _RINGBUFFER_H_
22 #define _RINGBUFFER_H_
23 
24 #include "os_base.h"
25 
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif /* HAVE_SYS_MMAN_H */
29 #ifdef HAVE_SYS_SEM_H
30 #include <sys/sem.h>
31 #endif
32 #ifdef HAVE_SYS_IPC_H
33 #include <sys/ipc.h>
34 #endif
35 #include "rpl_sem.h"
36 #include "util_int.h"
37 #include <qb/qbatomic.h>
38 #include <qb/qbutil.h>
39 #include <qb/qbrb.h>
40 
41 struct qb_ringbuffer_s;
42 
43 int32_t qb_rb_sem_create(struct qb_ringbuffer_s *rb, uint32_t flags);
44 
45 typedef int32_t(*qb_rb_notifier_post_fn_t) (void * instance, size_t msg_size);
46 typedef ssize_t(*qb_rb_notifier_q_len_fn_t) (void * instance);
47 typedef ssize_t(*qb_rb_notifier_used_fn_t) (void * instance);
48 typedef int32_t(*qb_rb_notifier_timedwait_fn_t) (void * instance,
49 					         int32_t ms_timeout);
50 typedef int32_t(*qb_rb_notifier_reclaim_fn_t) (void * instance, size_t msg_size);
51 typedef int32_t(*qb_rb_notifier_destroy_fn_t) (void * instance);
52 
53 struct qb_rb_notifier {
54 	qb_rb_notifier_post_fn_t post_fn;
55 	qb_rb_notifier_q_len_fn_t q_len_fn;
56 	qb_rb_notifier_used_fn_t space_used_fn;
57 	qb_rb_notifier_timedwait_fn_t timedwait_fn;
58 	qb_rb_notifier_reclaim_fn_t reclaim_fn;
59 	qb_rb_notifier_destroy_fn_t destroy_fn;
60 	void *instance;
61 };
62 
63 struct qb_ringbuffer_shared_s {
64 	volatile uint32_t write_pt;
65 	volatile uint32_t read_pt;
66 	uint32_t word_size;
67 	char hdr_path[PATH_MAX];
68 	char data_path[PATH_MAX];
69 	int32_t ref_count;
70 	rpl_sem_t posix_sem;
71 	char user_data[1];
72 } __attribute__ ((aligned(8)));
73 
74 struct qb_ringbuffer_s {
75 	uint32_t flags;
76 	int32_t sem_id;
77 	struct qb_ringbuffer_shared_s *shared_hdr;
78 	uint32_t *shared_data;
79 
80 	struct qb_rb_notifier notifier;
81 };
82 
83 void qb_rb_force_close(qb_ringbuffer_t * rb);
84 
85 /**
86  * Helper to munmap, and conditionally unlink the file or possibly truncate it.
87  * @param rb ringbuffer instance.
88  * @param unlink_it whether the underlying files should be unlinked.
89  * @param truncate_fallback whether to truncate the files when unlink fails.
90  * @return 0 (success) or -errno
91  */
92 int32_t qb_rb_close_helper(struct qb_ringbuffer_s * rb, int32_t unlink_it,
93 			   int32_t truncate_fallback);
94 
95 qb_ringbuffer_t *qb_rb_open_2(const char *name, size_t size, uint32_t flags,
96 			      size_t shared_user_data_size,
97 			      struct qb_rb_notifier *notifier);
98 
99 
100 #ifndef HAVE_SEMUN
101 union semun {
102 	int32_t val;
103 	struct semid_ds *buf;
104 	unsigned short int *array;
105 	struct seminfo *__buf;
106 };
107 #endif /* HAVE_SEMUN */
108 
109 
110 /* This function is to be used to "decorate" argument (with an extra
111    reference level added) to qb_rb_{force_,}_close() so as to avoid trivial
112    IPC API misuses such as recv-after-close rather than avoiding races in
113    multi-threaded applications (although it partially helps there, too);
114    it's debatable whether that should be fixed at higher level in ipc[cs].c */
115 static inline struct qb_ringbuffer_s *
qb_rb_lastref_and_ret(struct qb_ringbuffer_s ** rb)116 qb_rb_lastref_and_ret(struct qb_ringbuffer_s ** rb)
117 {
118 	struct qb_ringbuffer_s *rb_res = *rb;
119 
120 	if (rb_res == NULL) {
121 		return NULL;
122 	}
123 	*rb = NULL;
124 	/* qb_rb_close will get rid of this "last reference" */
125 	qb_atomic_int_set(&rb_res->shared_hdr->ref_count, 1);
126 
127 	return rb_res;
128 }
129 
130 #endif /* _RINGBUFFER_H_ */
131