s3:configure: search fdatasync also in librt
[Samba/ekacnet.git] / source3 / utils / eventlogadm.c
blob73d851db6f1459819cc4b8dcd2eabc48a7a7bb3e
2 /*
3 * Samba Unix/Linux SMB client utility
4 * Write Eventlog records to a tdb, perform other eventlog related functions
7 * Copyright (C) Brian Moran 2005.
8 * Copyright (C) Guenther Deschner 2009.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "lib/eventlog/eventlog.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
32 extern int optind;
33 extern char *optarg;
35 int opt_debug = 0;
37 static void usage( char *s )
39 printf( "\nUsage: %s [OPTION]\n\n", s );
40 printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
41 printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
42 printf( " -o dump <Eventlog Name> <starting_record>\t\t\t\t\tDump stored eventlog entries on STDOUT\n" );
43 printf( "\nMiscellaneous options:\n" );
44 printf( " -s <filename>\t\t\t\t\t\t\tUse configuration file <filename>.\n");
45 printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
46 printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
49 static void display_eventlog_names( void )
51 const char **elogs;
52 int i;
54 elogs = lp_eventlog_list( );
55 printf( "Active eventlog names:\n" );
56 printf( "--------------------------------------\n" );
57 if ( elogs ) {
58 for ( i = 0; elogs[i]; i++ ) {
59 printf( "\t%s\n", elogs[i] );
62 else
63 printf( "\t<None specified>\n");
66 static int DoAddSourceCommand( int argc, char **argv, bool debugflag, char *exename )
69 if ( argc < 3 ) {
70 printf( "need more arguments:\n" );
71 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
72 return -1;
74 /* must open the registry before we access it */
75 if (!W_ERROR_IS_OK(regdb_init())) {
76 printf( "Can't open the registry.\n" );
77 return -1;
80 if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
81 return -2;
82 return 0;
85 static int DoWriteCommand( int argc, char **argv, bool debugflag, char *exename )
87 FILE *f1;
88 char *argfname;
89 ELOG_TDB *etdb;
90 NTSTATUS status;
92 /* fixed constants are bad bad bad */
93 char linein[1024];
94 bool is_eor;
95 struct eventlog_Record_tdb ee;
96 uint32_t record_number = 0;
97 TALLOC_CTX *mem_ctx = talloc_tos();
99 f1 = stdin;
100 if ( !f1 ) {
101 printf( "Can't open STDIN\n" );
102 return -1;
105 if ( debugflag ) {
106 printf( "Starting write for eventlog [%s]\n", argv[0] );
107 display_eventlog_names( );
110 argfname = argv[0];
112 if ( !( etdb = elog_open_tdb( argfname, False, False ) ) ) {
113 printf( "can't open the eventlog TDB (%s)\n", argfname );
114 return -1;
117 ZERO_STRUCT( ee ); /* MUST initialize between records */
119 while ( !feof( f1 ) ) {
120 if (fgets( linein, sizeof( linein ) - 1, f1 ) == NULL) {
121 break;
123 if ((strlen(linein) > 0)
124 && (linein[strlen(linein)-1] == '\n')) {
125 linein[strlen(linein)-1] = 0;
128 if ( debugflag )
129 printf( "Read line [%s]\n", linein );
131 is_eor = False;
134 parse_logentry( mem_ctx, ( char * ) &linein, &ee, &is_eor );
135 /* should we do something with the return code? */
137 if ( is_eor ) {
138 fixup_eventlog_record_tdb( &ee );
140 if ( opt_debug )
141 printf( "record number [%d], tg [%d] , tw [%d]\n",
142 ee.record_number, (int)ee.time_generated, (int)ee.time_written );
144 if ( ee.time_generated != 0 ) {
146 /* printf("Writing to the event log\n"); */
148 status = evlog_push_record_tdb( mem_ctx, ELOG_TDB_CTX(etdb),
149 &ee, &record_number );
150 if ( !NT_STATUS_IS_OK(status) ) {
151 printf( "Can't write to the event log: %s\n",
152 nt_errstr(status) );
153 } else {
154 if ( opt_debug )
155 printf( "Wrote record %d\n",
156 record_number );
158 } else {
159 if ( opt_debug )
160 printf( "<null record>\n" );
162 ZERO_STRUCT( ee ); /* MUST initialize between records */
166 elog_close_tdb( etdb , False );
168 return 0;
171 static int DoDumpCommand(int argc, char **argv, bool debugflag, char *exename)
173 ELOG_TDB *etdb;
174 TALLOC_CTX *mem_ctx = talloc_tos();
175 const char *tdb_filename;
176 uint32_t count = 1;
178 if (argc > 2) {
179 return -1;
182 tdb_filename = argv[0];
184 if (argc > 1) {
185 count = atoi(argv[1]);
188 etdb = elog_open_tdb(argv[0], false, true);
189 if (!etdb) {
190 printf("can't open the eventlog TDB (%s)\n", argv[0]);
191 return -1;
194 while (1) {
196 struct eventlog_Record_tdb *r;
197 char *s;
199 r = evlog_pull_record_tdb(mem_ctx, etdb->tdb, count);
200 if (!r) {
201 break;
204 printf("displaying record: %d\n", count);
206 s = NDR_PRINT_STRUCT_STRING(mem_ctx, eventlog_Record_tdb, r);
207 if (s) {
208 printf("%s\n", s);
209 talloc_free(s);
211 count++;
214 elog_close_tdb(etdb, false);
216 return 0;
219 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
221 int main( int argc, char *argv[] )
223 int opt, rc;
224 char *exename;
225 char *configfile = NULL;
226 TALLOC_CTX *frame = talloc_stackframe();
229 fstring opname;
231 load_case_tables();
233 opt_debug = 0; /* todo set this from getopts */
235 exename = argv[0];
237 /* default */
239 fstrcpy( opname, "write" ); /* the default */
241 #if 0 /* TESTING CODE */
242 eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
243 #endif
244 while ( ( opt = getopt( argc, argv, "dho:s:" ) ) != EOF ) {
245 switch ( opt ) {
247 case 'o':
248 fstrcpy( opname, optarg );
249 break;
251 case 'h':
252 usage( exename );
253 display_eventlog_names( );
254 exit( 0 );
255 break;
257 case 'd':
258 opt_debug = 1;
259 break;
260 case 's':
261 configfile = talloc_strdup(frame, optarg);
262 break;
267 argc -= optind;
268 argv += optind;
270 if ( argc < 1 ) {
271 printf( "\nNot enough arguments!\n" );
272 usage( exename );
273 exit( 1 );
276 if ( configfile == NULL ) {
277 lp_load(get_dyn_CONFIGFILE(), True, False, False, True);
278 } else if (!lp_load(configfile, True, False, False, True)) {
279 printf("Unable to parse configfile '%s'\n",configfile);
280 exit( 1 );
283 /* note that the separate command types should call usage if they need to... */
284 while ( 1 ) {
285 if ( !StrCaseCmp( opname, "addsource" ) ) {
286 rc = DoAddSourceCommand( argc, argv, opt_debug,
287 exename );
288 break;
290 if ( !StrCaseCmp( opname, "write" ) ) {
291 rc = DoWriteCommand( argc, argv, opt_debug, exename );
292 break;
294 if ( !StrCaseCmp( opname, "dump" ) ) {
295 rc = DoDumpCommand( argc, argv, opt_debug, exename );
296 break;
298 printf( "unknown command [%s]\n", opname );
299 usage( exename );
300 exit( 1 );
301 break;
303 TALLOC_FREE(frame);
304 return rc;