1 /*
2  *  Copyright (c) 2017, Peter Haag
3  *  Copyright (c) 2014, Peter Haag
4  *  Copyright (c) 2009, Peter Haag
5  *  Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
6  *  All rights reserved.
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions are met:
10  *
11  *   * Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *   * Redistributions in binary form must reproduce the above copyright notice,
14  *     this list of conditions and the following disclaimer in the documentation
15  *     and/or other materials provided with the distribution.
16  *   * Neither the name of the author nor the names of its contributors may be
17  *     used to endorse or promote products derived from this software without
18  *     specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  *  POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef _BOOKKEEPER_H
35 #define _BOOKKEEPER_H 1
36 
37 #include "config.h"
38 
39 #include <sys/types.h>
40 #ifdef HAVE_STDINT_H
41 #include <stdint.h>
42 #endif
43 #include <time.h>
44 #include <unistd.h>
45 
46 enum { BOOKKEEPER_OK = 0, ERR_FAILED, ERR_NOTEXISTS, ERR_PATHACCESS, ERR_EXISTS };
47 
48 #define DETACH_ONLY	0
49 #define DESTROY_BOOKKEEPER 1
50 
51 typedef struct bookkeeper_s {
52 	// collector infos
53 	pid_t		nfcapd_pid;
54 	pid_t		launcher_pid;
55 
56 	// track info
57 	uint64_t	sequence;
58 
59 	// file infos
60 	time_t		first;
61 	time_t		last;
62 	uint64_t	numfiles;
63 	uint64_t	filesize;
64 	uint64_t	max_filesize;
65 	uint64_t	max_lifetime;
66 
67 
68 } bookkeeper_t;
69 
70 // All bookkeepers are put into a linked list, to have all the shm_id,sem_id
71 typedef struct bookkeeper_list_s {
72 	struct bookkeeper_list_s	*next;
73 
74 	bookkeeper_t	*bookkeeper;
75 
76 	// shared parameters
77 	int			sem_id;
78 	int			shm_id;
79 
80 } bookkeeper_list_t;
81 
82 /* function prototypes */
83 int InitBookkeeper(bookkeeper_t **bookkeeper, char *path, pid_t nfcapd_pid, pid_t launcher_pid);
84 
85 int AccessBookkeeper(bookkeeper_t **bookkeeper, char *path);
86 
87 void ReleaseBookkeeper(bookkeeper_t *bookkeeper, int destroy);
88 
89 void ClearBooks(bookkeeper_t *bookkeeper, bookkeeper_t *tmp_books);
90 
91 int  LookBooks(bookkeeper_t *bookkeeper);
92 
93 int  UnlookBooks(bookkeeper_t *bookkeeper);
94 
95 uint64_t BookSequence(bookkeeper_t *bookkeeper);
96 
97 void UpdateBooks(bookkeeper_t *bookkeeper, time_t when, uint64_t size);
98 
99 void UpdateBooksParam(bookkeeper_t *bookkeeper, time_t lifetime, uint64_t maxsize);
100 
101 void PrintBooks(bookkeeper_t *bookkeeper);
102 
103 #endif //_BOOKKEEPER_H
104