Initial debian packaging
[osspd.git] / ossp-util.h
1 /*
2  * ossp-util - OSS Proxy: Common utilities
3  *
4  * Copyright (C) 2008-2010  SUSE Linux Products GmbH
5  * Copyright (C) 2008-2010  Tejun Heo <tj@kernel.org>
6  *
7  * This file is released under the GPLv2.
8  */
9
10 #ifndef _OSSP_UTIL_H
11 #define _OSSP_UTIL_H
12
13 #include <assert.h>
14 #include <stddef.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include "ossp.h"
20
21 #define OSSP_LOG_NAME_LEN       128
22
23 enum {
24         OSSP_LOG_CRIT   = 1,
25         OSSP_LOG_ERR,
26         OSSP_LOG_WARN,
27         OSSP_LOG_INFO,
28         OSSP_LOG_DFL    = OSSP_LOG_INFO, /* default log level */
29         OSSP_LOG_DBG0,
30         OSSP_LOG_DBG1,
31         OSSP_LOG_MAX    = OSSP_LOG_DBG1,
32 };
33
34 extern char ossp_log_name[OSSP_LOG_NAME_LEN];
35 extern int ossp_log_level;
36 extern int ossp_log_timestamp;
37
38 #define BITS_PER_BYTE           8
39 #define BITS_PER_LONG           (BITS_PER_BYTE * sizeof(long))
40 #define DIV_ROUND_UP(n,d)       (((n) + (d) - 1) / (d))
41 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
42
43 /* ARRAY_SIZE and min/max macros stolen from linux/kernel.h */
44 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
45
46 #define min(x, y) ({                            \
47         typeof(x) _min1 = (x);                  \
48         typeof(y) _min2 = (y);                  \
49         (void) (&_min1 == &_min2);              \
50         _min1 < _min2 ? _min1 : _min2; })
51
52 #define max(x, y) ({                            \
53         typeof(x) _max1 = (x);                  \
54         typeof(y) _max2 = (y);                  \
55         (void) (&_max1 == &_max2);              \
56         _max1 > _max2 ? _max1 : _max2; })
57
58 #define min_t(type, x, y) ({                    \
59         type __min1 = (x);                      \
60         type __min2 = (y);                      \
61         __min1 < __min2 ? __min1: __min2; })
62
63 #define max_t(type, x, y) ({                    \
64         type __max1 = (x);                      \
65         type __max2 = (y);                      \
66         __max1 > __max2 ? __max1: __max2; })
67
68 void log_msg(int severity, const char *fmt, ...)
69         __attribute__ ((format (printf, 2, 3)));
70
71 #define fatal(fmt, args...) do {                                        \
72         log_msg(OSSP_LOG_CRIT, fmt , ##args);                           \
73         _exit(1);                                                       \
74 } while (0)
75 #define err(fmt, args...)               log_msg(OSSP_LOG_ERR, fmt , ##args)
76 #define warn(fmt, args...)              log_msg(OSSP_LOG_WARN, fmt , ##args)
77 #define info(fmt, args...)              log_msg(OSSP_LOG_INFO, fmt , ##args)
78 #define dbg0(fmt, args...)              log_msg(OSSP_LOG_DBG0, fmt , ##args)
79 #define dbg1(fmt, args...)              log_msg(OSSP_LOG_DBG1, fmt , ##args)
80
81 #define fatal_e(e, fmt, args...)        \
82         fatal(fmt" (%s)" , ##args, strerror(-(e)))
83 #define err_e(e, fmt, args...)  \
84         err(fmt" (%s)" , ##args, strerror(-(e)))
85 #define warn_e(e, fmt, args...) \
86         warn(fmt" (%s)" , ##args, strerror(-(e)))
87 #define info_e(e, fmt, args...) \
88         info(fmt" (%s)" , ##args, strerror(-(e)))
89 #define dbg0_e(e, fmt, args...) \
90         dbg0(fmt" (%s)" , ##args, strerror(-(e)))
91 #define dbg1_e(e, fmt, args...) \
92         dbg1(fmt" (%s)" , ##args, strerror(-(e)))
93
94 struct ring_buf {
95         char            *buf;
96         size_t          size;
97         size_t          head;
98         size_t          bytes;
99 };
100
101 static inline size_t ring_size(struct ring_buf *ring)
102 {
103         return ring->size;
104 }
105
106 static inline size_t ring_bytes(struct ring_buf *ring)
107 {
108         return ring->bytes;
109 }
110
111 static inline size_t ring_space(struct ring_buf *ring)
112 {
113         return ring->size - ring->bytes;
114 }
115
116 static inline void ring_consume(struct ring_buf *ring, size_t size)
117 {
118         assert(ring->bytes >= size);
119         ring->bytes -= size;
120 }
121
122 static inline void ring_manual_init(struct ring_buf *ring, void *buf,
123                                     size_t size, size_t head, size_t bytes)
124 {
125         ring->buf = buf;
126         ring->size = size;
127         ring->head = head;
128         ring->bytes = bytes;
129 }
130
131 void ring_fill(struct ring_buf *ring, const void *buf, size_t size);
132 void *ring_data(struct ring_buf *ring, size_t *sizep);
133 int ring_resize(struct ring_buf *ring, size_t new_size);
134
135 struct sized_buf {
136         char            *buf;
137         size_t          size;
138 };
139
140 int ensure_sbuf_size(struct sized_buf *sbuf, size_t size);
141
142 int read_fill(int fd, void *buf, size_t size);
143 int write_fill(int fd, const void *buf, size_t size);
144
145 /*
146  * Bitops lifted from linux asm-generic implementation.
147  */
148 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned
149                                  long size, unsigned long offset);
150 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
151 extern void __set_bit(int nr, volatile unsigned long *addr);
152 extern void __clear_bit(int nr, volatile unsigned long *addr);
153
154 typedef ssize_t (*ossp_action_fn_t)(enum ossp_opcode opcode,
155                                     void *carg, void *din, size_t din_sz,
156                                     void *rarg, void *dout, size_t *dout_szp,
157                                     int fd);
158
159 int get_proc_self_info(pid_t tid, pid_t *pgrp,
160                        char *cmd_buf, size_t cmd_buf_sz);
161
162 /*
163  * Doubly linked list handling code shamelessly stolen from the Linux
164  * kernel 2.6.26 include/linux/list.h.
165  */
166
167 /**
168  * container_of - cast a member of a structure out to the containing structure
169  * @ptr:        the pointer to the member.
170  * @type:       the type of the container struct this is embedded in.
171  * @member:     the name of the member within the struct.
172  *
173  */
174 #define container_of(ptr, type, member) ({                      \
175         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
176         (type *)( (char *)__mptr - offsetof(type,member) );})
177
178 #define LIST_POISON1  ((void *) 0x00100100)
179 #define LIST_POISON2  ((void *) 0x00200200)
180
181 /*
182  * Simple doubly linked list implementation.
183  *
184  * Some of the internal functions ("__xxx") are useful when
185  * manipulating whole lists rather than single entries, as
186  * sometimes we already know the next/prev entries and we can
187  * generate better code by using them directly rather than
188  * using the generic single-entry routines.
189  */
190
191 struct list_head {
192         struct list_head *next, *prev;
193 };
194
195 #define LIST_HEAD_INIT(name) { &(name), &(name) }
196
197 #define LIST_HEAD(name) \
198         struct list_head name = LIST_HEAD_INIT(name)
199
200 static inline void INIT_LIST_HEAD(struct list_head *list)
201 {
202         list->next = list;
203         list->prev = list;
204 }
205
206 /*
207  * Insert a new entry between two known consecutive entries.
208  *
209  * This is only for internal list manipulation where we know
210  * the prev/next entries already!
211  */
212 static inline void __list_add(struct list_head *new,
213                               struct list_head *prev,
214                               struct list_head *next)
215 {
216         next->prev = new;
217         new->next = next;
218         new->prev = prev;
219         prev->next = new;
220 }
221
222 /**
223  * list_add - add a new entry
224  * @new: new entry to be added
225  * @head: list head to add it after
226  *
227  * Insert a new entry after the specified head.
228  * This is good for implementing stacks.
229  */
230 static inline void list_add(struct list_head *new, struct list_head *head)
231 {
232         __list_add(new, head, head->next);
233 }
234
235 /**
236  * list_add_tail - add a new entry
237  * @new: new entry to be added
238  * @head: list head to add it before
239  *
240  * Insert a new entry before the specified head.
241  * This is useful for implementing queues.
242  */
243 static inline void list_add_tail(struct list_head *new, struct list_head *head)
244 {
245         __list_add(new, head->prev, head);
246 }
247
248 /*
249  * Delete a list entry by making the prev/next entries
250  * point to each other.
251  *
252  * This is only for internal list manipulation where we know
253  * the prev/next entries already!
254  */
255 static inline void __list_del(struct list_head * prev, struct list_head * next)
256 {
257         next->prev = prev;
258         prev->next = next;
259 }
260
261 /**
262  * list_del - deletes entry from list.
263  * @entry: the element to delete from the list.
264  * Note: list_empty() on entry does not return true after this, the entry is
265  * in an undefined state.
266  */
267 static inline void list_del(struct list_head *entry)
268 {
269         __list_del(entry->prev, entry->next);
270         entry->next = LIST_POISON1;
271         entry->prev = LIST_POISON2;
272 }
273
274 /**
275  * list_replace - replace old entry by new one
276  * @old : the element to be replaced
277  * @new : the new element to insert
278  *
279  * If @old was empty, it will be overwritten.
280  */
281 static inline void list_replace(struct list_head *old,
282                                 struct list_head *new)
283 {
284         new->next = old->next;
285         new->next->prev = new;
286         new->prev = old->prev;
287         new->prev->next = new;
288 }
289
290 static inline void list_replace_init(struct list_head *old,
291                                         struct list_head *new)
292 {
293         list_replace(old, new);
294         INIT_LIST_HEAD(old);
295 }
296
297 /**
298  * list_del_init - deletes entry from list and reinitialize it.
299  * @entry: the element to delete from the list.
300  */
301 static inline void list_del_init(struct list_head *entry)
302 {
303         __list_del(entry->prev, entry->next);
304         INIT_LIST_HEAD(entry);
305 }
306
307 /**
308  * list_move - delete from one list and add as another's head
309  * @list: the entry to move
310  * @head: the head that will precede our entry
311  */
312 static inline void list_move(struct list_head *list, struct list_head *head)
313 {
314         __list_del(list->prev, list->next);
315         list_add(list, head);
316 }
317
318 /**
319  * list_move_tail - delete from one list and add as another's tail
320  * @list: the entry to move
321  * @head: the head that will follow our entry
322  */
323 static inline void list_move_tail(struct list_head *list,
324                                   struct list_head *head)
325 {
326         __list_del(list->prev, list->next);
327         list_add_tail(list, head);
328 }
329
330 /**
331  * list_is_last - tests whether @list is the last entry in list @head
332  * @list: the entry to test
333  * @head: the head of the list
334  */
335 static inline int list_is_last(const struct list_head *list,
336                                 const struct list_head *head)
337 {
338         return list->next == head;
339 }
340
341 /**
342  * list_empty - tests whether a list is empty
343  * @head: the list to test.
344  */
345 static inline int list_empty(const struct list_head *head)
346 {
347         return head->next == head;
348 }
349
350 /**
351  * list_empty_careful - tests whether a list is empty and not being modified
352  * @head: the list to test
353  *
354  * Description:
355  * tests whether a list is empty _and_ checks that no other CPU might be
356  * in the process of modifying either member (next or prev)
357  *
358  * NOTE: using list_empty_careful() without synchronization
359  * can only be safe if the only activity that can happen
360  * to the list entry is list_del_init(). Eg. it cannot be used
361  * if another CPU could re-list_add() it.
362  */
363 static inline int list_empty_careful(const struct list_head *head)
364 {
365         struct list_head *next = head->next;
366         return (next == head) && (next == head->prev);
367 }
368
369 /**
370  * list_is_singular - tests whether a list has just one entry.
371  * @head: the list to test.
372  */
373 static inline int list_is_singular(const struct list_head *head)
374 {
375         return !list_empty(head) && (head->next == head->prev);
376 }
377
378 static inline void __list_splice(const struct list_head *list,
379                                  struct list_head *head)
380 {
381         struct list_head *first = list->next;
382         struct list_head *last = list->prev;
383         struct list_head *at = head->next;
384
385         first->prev = head;
386         head->next = first;
387
388         last->next = at;
389         at->prev = last;
390 }
391
392 /**
393  * list_splice - join two lists
394  * @list: the new list to add.
395  * @head: the place to add it in the first list.
396  */
397 static inline void list_splice(const struct list_head *list,
398                                 struct list_head *head)
399 {
400         if (!list_empty(list))
401                 __list_splice(list, head);
402 }
403
404 /**
405  * list_splice_init - join two lists and reinitialise the emptied list.
406  * @list: the new list to add.
407  * @head: the place to add it in the first list.
408  *
409  * The list at @list is reinitialised
410  */
411 static inline void list_splice_init(struct list_head *list,
412                                     struct list_head *head)
413 {
414         if (!list_empty(list)) {
415                 __list_splice(list, head);
416                 INIT_LIST_HEAD(list);
417         }
418 }
419
420 /**
421  * list_entry - get the struct for this entry
422  * @ptr:        the &struct list_head pointer.
423  * @type:       the type of the struct this is embedded in.
424  * @member:     the name of the list_struct within the struct.
425  */
426 #define list_entry(ptr, type, member) \
427         container_of(ptr, type, member)
428
429 /**
430  * list_first_entry - get the first element from a list
431  * @ptr:        the list head to take the element from.
432  * @type:       the type of the struct this is embedded in.
433  * @member:     the name of the list_struct within the struct.
434  *
435  * Note, that list is expected to be not empty.
436  */
437 #define list_first_entry(ptr, type, member) \
438         list_entry((ptr)->next, type, member)
439
440 /**
441  * list_for_each        -       iterate over a list
442  * @pos:        the &struct list_head to use as a loop cursor.
443  * @head:       the head for your list.
444  */
445 #define list_for_each(pos, head) \
446         for (pos = (head)->next; pos != (head); pos = pos->next)
447
448 /**
449  * list_for_each_prev   -       iterate over a list backwards
450  * @pos:        the &struct list_head to use as a loop cursor.
451  * @head:       the head for your list.
452  */
453 #define list_for_each_prev(pos, head) \
454         for (pos = (head)->prev; pos != (head); pos = pos->prev)
455
456 /**
457  * list_for_each_safe - iterate over a list safe against removal of list entry
458  * @pos:        the &struct list_head to use as a loop cursor.
459  * @n:          another &struct list_head to use as temporary storage
460  * @head:       the head for your list.
461  */
462 #define list_for_each_safe(pos, n, head) \
463         for (pos = (head)->next, n = pos->next; pos != (head); \
464                 pos = n, n = pos->next)
465
466 /**
467  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
468  * @pos:        the &struct list_head to use as a loop cursor.
469  * @n:          another &struct list_head to use as temporary storage
470  * @head:       the head for your list.
471  */
472 #define list_for_each_prev_safe(pos, n, head) \
473         for (pos = (head)->prev, n = pos->prev; \
474              pos != (head); pos = n, n = pos->prev)
475
476 /**
477  * list_for_each_entry  -       iterate over list of given type
478  * @pos:        the type * to use as a loop cursor.
479  * @head:       the head for your list.
480  * @member:     the name of the list_struct within the struct.
481  */
482 #define list_for_each_entry(pos, head, member)                          \
483         for (pos = list_entry((head)->next, typeof(*pos), member);      \
484              &pos->member != (head);                                    \
485              pos = list_entry(pos->member.next, typeof(*pos), member))
486
487 /**
488  * list_for_each_entry_reverse - iterate backwards over list of given type.
489  * @pos:        the type * to use as a loop cursor.
490  * @head:       the head for your list.
491  * @member:     the name of the list_struct within the struct.
492  */
493 #define list_for_each_entry_reverse(pos, head, member)                  \
494         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
495              &pos->member != (head);                                    \
496              pos = list_entry(pos->member.prev, typeof(*pos), member))
497
498 /**
499  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
500  * @pos:        the type * to use as a start point
501  * @head:       the head of the list
502  * @member:     the name of the list_struct within the struct.
503  *
504  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
505  */
506 #define list_prepare_entry(pos, head, member) \
507         ((pos) ? : list_entry(head, typeof(*pos), member))
508
509 /**
510  * list_for_each_entry_continue - continue iteration over list of given type
511  * @pos:        the type * to use as a loop cursor.
512  * @head:       the head for your list.
513  * @member:     the name of the list_struct within the struct.
514  *
515  * Continue to iterate over list of given type, continuing after
516  * the current position.
517  */
518 #define list_for_each_entry_continue(pos, head, member)                 \
519         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
520              &pos->member != (head);                                    \
521              pos = list_entry(pos->member.next, typeof(*pos), member))
522
523 /**
524  * list_for_each_entry_continue_reverse - iterate backwards from the given point
525  * @pos:        the type * to use as a loop cursor.
526  * @head:       the head for your list.
527  * @member:     the name of the list_struct within the struct.
528  *
529  * Start to iterate over list of given type backwards, continuing after
530  * the current position.
531  */
532 #define list_for_each_entry_continue_reverse(pos, head, member)         \
533         for (pos = list_entry(pos->member.prev, typeof(*pos), member);  \
534              &pos->member != (head);                                    \
535              pos = list_entry(pos->member.prev, typeof(*pos), member))
536
537 /**
538  * list_for_each_entry_from - iterate over list of given type from the current point
539  * @pos:        the type * to use as a loop cursor.
540  * @head:       the head for your list.
541  * @member:     the name of the list_struct within the struct.
542  *
543  * Iterate over list of given type, continuing from current position.
544  */
545 #define list_for_each_entry_from(pos, head, member)                     \
546         for (; &pos->member != (head);                                  \
547              pos = list_entry(pos->member.next, typeof(*pos), member))
548
549 /**
550  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
551  * @pos:        the type * to use as a loop cursor.
552  * @n:          another type * to use as temporary storage
553  * @head:       the head for your list.
554  * @member:     the name of the list_struct within the struct.
555  */
556 #define list_for_each_entry_safe(pos, n, head, member)                  \
557         for (pos = list_entry((head)->next, typeof(*pos), member),      \
558                 n = list_entry(pos->member.next, typeof(*pos), member); \
559              &pos->member != (head);                                    \
560              pos = n, n = list_entry(n->member.next, typeof(*n), member))
561
562 /**
563  * list_for_each_entry_safe_continue
564  * @pos:        the type * to use as a loop cursor.
565  * @n:          another type * to use as temporary storage
566  * @head:       the head for your list.
567  * @member:     the name of the list_struct within the struct.
568  *
569  * Iterate over list of given type, continuing after current point,
570  * safe against removal of list entry.
571  */
572 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
573         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
574                 n = list_entry(pos->member.next, typeof(*pos), member);         \
575              &pos->member != (head);                                            \
576              pos = n, n = list_entry(n->member.next, typeof(*n), member))
577
578 /**
579  * list_for_each_entry_safe_from
580  * @pos:        the type * to use as a loop cursor.
581  * @n:          another type * to use as temporary storage
582  * @head:       the head for your list.
583  * @member:     the name of the list_struct within the struct.
584  *
585  * Iterate over list of given type from current point, safe against
586  * removal of list entry.
587  */
588 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
589         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
590              &pos->member != (head);                                            \
591              pos = n, n = list_entry(n->member.next, typeof(*n), member))
592
593 /**
594  * list_for_each_entry_safe_reverse
595  * @pos:        the type * to use as a loop cursor.
596  * @n:          another type * to use as temporary storage
597  * @head:       the head for your list.
598  * @member:     the name of the list_struct within the struct.
599  *
600  * Iterate backwards over list of given type, safe against removal
601  * of list entry.
602  */
603 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
604         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
605                 n = list_entry(pos->member.prev, typeof(*pos), member); \
606              &pos->member != (head);                                    \
607              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
608
609 #endif /*_OSSP_UTIL_H*/