1 /* This file is part of GNU Rush.
2    Copyright (C) 2008-2019 Sergey Poznyakoff
3 
4    GNU Rush is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Rush is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Rush.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <gettext.h>
20 
21 #define N_(s) s
22 #define _(s) gettext(s)
23 
24 #ifndef RUSH_ARG_UNUSED
25 # define RUSH_ARG_UNUSED __attribute__ ((__unused__))
26 #endif
27 
28 #ifndef RUSH_PRINTFLIKE
29 # define RUSH_PRINTFLIKE(fmt,narg) __attribute__ ((__format__ (__printf__, fmt, narg)))
30 #endif
31 
32 #ifndef RUSH_NORETURN
33 # define RUSH_NORETURN __attribute__ ((noreturn))
34 #endif
35 
36 
37 extern const char *program_name;
38 
39 void rush_set_program_name(const char *argv0);
40 
41 
42 
43 struct rush_wtmp {
44 	size_t reclen;
45 	pid_t pid;
46 	struct timeval start;
47 	struct timeval stop;
48 	char *user;
49 	char *rule;
50 	char *command;
51 };
52 #define RUSH_WTMP_DATA_PTR(w) ((char*)&(w)->pid)
53 
54 #define RUSH_STATUS_AVAIL  0
55 #define RUSH_STATUS_INUSE  1
56 
57 #define RUSH_STATUS_MAP_ANY 0
58 #define RUSH_STATUS_MAP_BIT(stat) (1<<(stat))
59 #define RUSH_STATUS_MAP_ISSET(map, stat) ((map) & RUSH_STATUS_MAP_BIT(stat))
60 
61 struct rush_utmp {
62 	int status;
63 	off_t offset;
64 };
65 
66 #define RUSH_UTMP_NAME "utmp"
67 #define RUSH_WTMP_NAME "wtmp"
68 
69 enum rushdb_result {
70 	rushdb_result_ok,
71 	rushdb_result_eof,
72 	rushdb_result_fail
73 };
74 
75 extern mode_t rushdb_umask;
76 extern mode_t rushdb_dir_mode;
77 extern mode_t rushdb_file_mode;
78 
79 enum rush_wtmp_dir {
80 	rush_wtmp_forward,
81 	rush_wtmp_backward
82 };
83 
84 void rush_wtmp_set_dir(enum rush_wtmp_dir dir);
85 int rush_wtmp_rewind(void);
86 
87 int rush_wtmp_open(const char *name, int rw);
88 enum rushdb_result rush_wtmp_read(struct rush_wtmp **pwtmp);
89 int rush_wtmp_seek(off_t off);
90 off_t rush_wtmp_append(struct rush_wtmp *wtmp);
91 int rush_wtmp_close(void);
92 int rush_wtmp_update(struct timeval *tv);
93 
94 int rush_utmp_open(const char *name, int rw);
95 enum rushdb_result rush_utmp_read(int statmap, int *pstatus,
96 				     struct rush_wtmp **pwtmp);
97 int rush_utmp_chstatus(int status);
98 int rush_utmp_write(struct rush_wtmp *wtmp);
99 int rush_utmp_close();
100 
101 void rush_utmp_lock_all(int type);
102 void rush_utmp_unlock_all(void);
103 
104 enum rushdb_result rushdb_open(const char *base_name, int rw);
105 int rushdb_close(void);
106 int rushdb_start(struct rush_wtmp *wtmp);
107 int rushdb_stop(void);
108 void rushdb_backward_direction(void);
109 
110 #define RUSH_LOCK_READ  0
111 #define RUSH_LOCK_WRITE 1
112 
113 int rushdb_lock(int fd, size_t size, off_t offset, int whence, int type);
114 int rushdb_unlock(int fd, size_t size, off_t offset, int whence);
115 
116 
117 typedef struct slist *slist_t;
118 #define LIST_APPEND(elt, head, tail)		\
119 	do {					\
120 		if (tail)			\
121 			(tail)->next = elt;	\
122 		else				\
123 			head = elt;		\
124 		tail = elt;			\
125 	} while(0)
126 
127 void slist_append(slist_t slist, const char *p, size_t len);
128 char *slist_reduce(slist_t slist, char **pbuf, size_t *psize);
129 slist_t slist_create(void);
130 void slist_free(slist_t slist);
131 char *slist_alloc(slist_t slist, size_t len);
132 
133 
134 void version(const char *progname);
135 
136 
137 typedef struct rushdb_format *rushdb_format_t;
138 extern char *rushdb_date_format;
139 extern char *rushdb_error_string;
140 
141 rushdb_format_t rushdb_compile_format(char *fmt);
142 int rushdb_print(rushdb_format_t form, struct rush_wtmp *wtmp, int newline);
143 void rushdb_print_header(rushdb_format_t form);
144 
145 
146 char *rush_read_format(const char *name);
147 
148 
149 void rush_i18n_init(void);
150 const char *user_gettext(const char *locale, const char *domain,
151 			 const char *dir,
152 			 const char *msg);
153 
154 void argcv_free(int argc, char **argv);
155 char *argcv_string(int argc, char **argv);
156 
157 int wildmatch(char const *expr, char const *name, size_t len);
158