New argument in the init() function of the plugins, to get the global options.

Initial
Stephane Bortzmeyer 20 years ago
parent 98db4f370f
commit 5a99238de8

@ -16,11 +16,12 @@ cooked one by returning a port name or NULL from the init() function.
You will have to provide three functions: You will have to provide three functions:
char * init (const int argc, const char **argv) Accepts remaining char * init (const int argc, const char **argv, const echoping_options options))
arguments (you have to use popt to parse them, or do it by hand, Accepts remaining arguments (you have to use popt to parse them, or do
getopt does not allow you to resume the parsing) and returns a it by hand, getopt does not allow you to resume the parsing) and
string identifying the port name (cooked interface) or NULL (raw returns a string identifying the port name (cooked interface) or NULL
interface). (raw interface). The options struct will give you the global options
(see echoping.h).
For the cooked interface: For the cooked interface:

@ -88,7 +88,6 @@ main (argc, argv)
char *ext; char *ext;
void *plugin; void *plugin;
int plugin_result; int plugin_result;
char *dl_result;
void to_alarm (); /* our alarm() signal handler */ void to_alarm (); /* our alarm() signal handler */
void interrupted (); void interrupted ();
@ -141,6 +140,7 @@ main (argc, argv)
int tos; int tos;
int tos_requested = 0; int tos_requested = 0;
char *p; char *p;
echoping_options global_options;
/* popt variables */ /* popt variables */
const struct poptOption options[] = { const struct poptOption options[] = {
@ -174,6 +174,9 @@ main (argc, argv)
}; };
poptContext poptcon; poptContext poptcon;
global_options.udp = 0;
global_options.verbose = 0;
null_timeval.tv_sec = 0; null_timeval.tv_sec = 0;
null_timeval.tv_usec = 0; null_timeval.tv_usec = 0;
max_timeval.tv_sec = 1000000000; max_timeval.tv_sec = 1000000000;
@ -457,7 +460,10 @@ main (argc, argv)
{ {
err_sys ("Cannot find init in %s: %s", plugin_name, dlerror ()); err_sys ("Cannot find init in %s: %s", plugin_name, dlerror ());
} }
plugin_port_name = plugin_init (remaining, (const char **) leftover); global_options.udp = udp;
global_options.verbose = verbose;
plugin_port_name =
plugin_init (remaining, (const char **) leftover, global_options);
if (plugin_port_name != NULL) if (plugin_port_name != NULL)
{ {
strcpy (port_name, plugin_port_name); strcpy (port_name, plugin_port_name);
@ -889,7 +895,7 @@ main (argc, argv)
{ {
plugin_result = plugin_execute (); plugin_result = plugin_execute ();
if (plugin_result == -2) if (plugin_result == -2)
err_quit(""); err_quit ("");
} }
else else
{ {

@ -35,7 +35,7 @@
#include <math.h> #include <math.h>
#include <dlfcn.h> #include <dlfcn.h>
/* popt library TODO: what if missing? */ /* popt library */
#include <popt.h> #include <popt.h>
#ifdef OPENSSL #ifdef OPENSSL
@ -119,11 +119,17 @@ struct result
}; };
unsigned short timeout_flag; unsigned short timeout_flag;
struct echoping_struct
{
unsigned short udp; /* Use the UDP protocol (TCP is the default) */
unsigned short verbose;
};
typedef struct echoping_struct echoping_options;
#ifndef IN_PLUGIN #ifndef IN_PLUGIN
/* The functions we will find in the plugin */ /* The functions we will find in the plugin */
/* Initializes the plugin with its arguments. Returns the port name or number. */ /* Initializes the plugin with its arguments. Returns the port name or number or NULL if the plugin wants to use the raw interface. */
typedef char * (*init_f) (const int argc, const char **argv); typedef char *(*init_f) (const int argc, const char **argv,
const echoping_options global_options);
init_f plugin_init; init_f plugin_init;
typedef void (*start_f) (struct addrinfo *); typedef void (*start_f) (struct addrinfo *);
start_f plugin_start; start_f plugin_start;

@ -2,6 +2,7 @@ if PGSQLBUILD
pkglib_LTLIBRARIES = postgresql.la pkglib_LTLIBRARIES = postgresql.la
postgresql_la_SOURCES = postgresql.c postgresql_la_SOURCES = postgresql.c
postgresql_la_LDFLAGS = -module postgresql_la_LDFLAGS = -module
man_MANS = echoping_postgresql.1
endif endif
# $Id$ # $Id$

@ -55,7 +55,7 @@ am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \
mkinstalldirs = $(SHELL) $(top_srcdir)/../../mkinstalldirs mkinstalldirs = $(SHELL) $(top_srcdir)/../../mkinstalldirs
CONFIG_HEADER = config.h CONFIG_HEADER = config.h
CONFIG_CLEAN_FILES = CONFIG_CLEAN_FILES =
am__installdirs = "$(DESTDIR)$(pkglibdir)" am__installdirs = "$(DESTDIR)$(pkglibdir)" "$(DESTDIR)$(man1dir)"
pkglibLTLIBRARIES_INSTALL = $(INSTALL) pkglibLTLIBRARIES_INSTALL = $(INSTALL)
LTLIBRARIES = $(pkglib_LTLIBRARIES) LTLIBRARIES = $(pkglib_LTLIBRARIES)
postgresql_la_LIBADD = postgresql_la_LIBADD =
@ -76,6 +76,9 @@ LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
$(AM_LDFLAGS) $(LDFLAGS) -o $@ $(AM_LDFLAGS) $(LDFLAGS) -o $@
SOURCES = $(postgresql_la_SOURCES) SOURCES = $(postgresql_la_SOURCES)
DIST_SOURCES = $(am__postgresql_la_SOURCES_DIST) DIST_SOURCES = $(am__postgresql_la_SOURCES_DIST)
man1dir = $(mandir)/man1
NROFF = nroff
MANS = $(man_MANS)
ETAGS = etags ETAGS = etags
CTAGS = ctags CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@ -188,6 +191,7 @@ target_alias = @target_alias@
@PGSQLBUILD_TRUE@pkglib_LTLIBRARIES = postgresql.la @PGSQLBUILD_TRUE@pkglib_LTLIBRARIES = postgresql.la
@PGSQLBUILD_TRUE@postgresql_la_SOURCES = postgresql.c @PGSQLBUILD_TRUE@postgresql_la_SOURCES = postgresql.c
@PGSQLBUILD_TRUE@postgresql_la_LDFLAGS = -module @PGSQLBUILD_TRUE@postgresql_la_LDFLAGS = -module
@PGSQLBUILD_TRUE@man_MANS = echoping_postgresql.1
all: config.h all: config.h
$(MAKE) $(AM_MAKEFLAGS) all-am $(MAKE) $(AM_MAKEFLAGS) all-am
@ -314,6 +318,51 @@ clean-libtool:
distclean-libtool: distclean-libtool:
-rm -f libtool -rm -f libtool
uninstall-info-am: uninstall-info-am:
install-man1: $(man1_MANS) $(man_MANS)
@$(NORMAL_INSTALL)
test -z "$(man1dir)" || $(mkdir_p) "$(DESTDIR)$(man1dir)"
@list='$(man1_MANS) $(dist_man1_MANS) $(nodist_man1_MANS)'; \
l2='$(man_MANS) $(dist_man_MANS) $(nodist_man_MANS)'; \
for i in $$l2; do \
case "$$i" in \
*.1*) list="$$list $$i" ;; \
esac; \
done; \
for i in $$list; do \
if test -f $(srcdir)/$$i; then file=$(srcdir)/$$i; \
else file=$$i; fi; \
ext=`echo $$i | sed -e 's/^.*\\.//'`; \
case "$$ext" in \
1*) ;; \
*) ext='1' ;; \
esac; \
inst=`echo $$i | sed -e 's/\\.[0-9a-z]*$$//'`; \
inst=`echo $$inst | sed -e 's/^.*\///'`; \
inst=`echo $$inst | sed '$(transform)'`.$$ext; \
echo " $(INSTALL_DATA) '$$file' '$(DESTDIR)$(man1dir)/$$inst'"; \
$(INSTALL_DATA) "$$file" "$(DESTDIR)$(man1dir)/$$inst"; \
done
uninstall-man1:
@$(NORMAL_UNINSTALL)
@list='$(man1_MANS) $(dist_man1_MANS) $(nodist_man1_MANS)'; \
l2='$(man_MANS) $(dist_man_MANS) $(nodist_man_MANS)'; \
for i in $$l2; do \
case "$$i" in \
*.1*) list="$$list $$i" ;; \
esac; \
done; \
for i in $$list; do \
ext=`echo $$i | sed -e 's/^.*\\.//'`; \
case "$$ext" in \
1*) ;; \
*) ext='1' ;; \
esac; \
inst=`echo $$i | sed -e 's/\\.[0-9a-z]*$$//'`; \
inst=`echo $$inst | sed -e 's/^.*\///'`; \
inst=`echo $$inst | sed '$(transform)'`.$$ext; \
echo " rm -f '$(DESTDIR)$(man1dir)/$$inst'"; \
rm -f "$(DESTDIR)$(man1dir)/$$inst"; \
done
ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
@ -492,9 +541,9 @@ distcleancheck: distclean
exit 1; } >&2 exit 1; } >&2
check-am: all-am check-am: all-am
check: check-am check: check-am
all-am: Makefile $(LTLIBRARIES) config.h all-am: Makefile $(LTLIBRARIES) $(MANS) config.h
installdirs: installdirs:
for dir in "$(DESTDIR)$(pkglibdir)"; do \ for dir in "$(DESTDIR)$(pkglibdir)" "$(DESTDIR)$(man1dir)"; do \
test -z "$$dir" || $(mkdir_p) "$$dir"; \ test -z "$$dir" || $(mkdir_p) "$$dir"; \
done done
install: install-am install: install-am
@ -543,13 +592,13 @@ info: info-am
info-am: info-am:
install-data-am: install-data-am: install-man
install-exec-am: install-pkglibLTLIBRARIES install-exec-am: install-pkglibLTLIBRARIES
install-info: install-info-am install-info: install-info-am
install-man: install-man: install-man1
installcheck-am: installcheck-am:
@ -573,7 +622,10 @@ ps: ps-am
ps-am: ps-am:
uninstall-am: uninstall-info-am uninstall-pkglibLTLIBRARIES uninstall-am: uninstall-info-am uninstall-man \
uninstall-pkglibLTLIBRARIES
uninstall-man: uninstall-man1
.PHONY: CTAGS GTAGS all all-am am--refresh check check-am clean \ .PHONY: CTAGS GTAGS all all-am am--refresh check check-am clean \
clean-generic clean-libtool clean-pkglibLTLIBRARIES ctags dist \ clean-generic clean-libtool clean-pkglibLTLIBRARIES ctags dist \
@ -583,11 +635,12 @@ uninstall-am: uninstall-info-am uninstall-pkglibLTLIBRARIES
distdir distuninstallcheck dvi dvi-am html html-am info \ distdir distuninstallcheck dvi dvi-am html html-am info \
info-am install install-am install-data install-data-am \ info-am install install-am install-data install-data-am \
install-exec install-exec-am install-info install-info-am \ install-exec install-exec-am install-info install-info-am \
install-man install-pkglibLTLIBRARIES install-strip \ install-man install-man1 install-pkglibLTLIBRARIES \
installcheck installcheck-am installdirs maintainer-clean \ install-strip installcheck installcheck-am installdirs \
maintainer-clean-generic mostlyclean mostlyclean-compile \ maintainer-clean maintainer-clean-generic mostlyclean \
mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ mostlyclean-compile mostlyclean-generic mostlyclean-libtool \
tags uninstall uninstall-am uninstall-info-am \ pdf pdf-am ps ps-am tags uninstall uninstall-am \
uninstall-info-am uninstall-man uninstall-man1 \
uninstall-pkglibLTLIBRARIES uninstall-pkglibLTLIBRARIES

@ -0,0 +1,48 @@
.\" Hey, EMACS: -*- nroff -*-
.\" First parameter, NAME, should be all caps
.\" Second parameter, SECTION, should be 1-8, maybe w/ subsection
.\" other parameters are allowed: see man(7), man(1)
.TH ECHOPING_POSTGRESQL 1 "May 26, 2004"
.\" Please adjust this date whenever revising the manpage.
.\"
.\" Some roff macros, for reference:
.\" .nh disable hyphenation
.\" .hy enable hyphenation
.\" .ad l left justify
.\" .ad b justify to both left and right margins
.\" .nf disable filling
.\" .fi enable filling
.\" .br insert line break
.\" .sp <n> insert n+1 empty lines
.\" for manpage-specific macros, see man(7)
.SH NAME
echoping_postgresql \- echoping plugin which connects to a PostgreSQL RDBMS server
.SH SYNOPSIS
.B echoping
.RI [echoping\ options]
.RI -m\ postgresql
.B hostname
.RI [-c conninfo]
.RI [request]
.SH DESCRIPTION
.PP
.\" TeX users may be more comfortable with the \fB<whatever>\fP and
.\" \fI<whatever>\fP escape sequences to invode bold face and italics,
.\" respectively.
postgresql is an echoping plugin to connect to PostgreSQL RDBMS servers.
.SH ARGUMENTS
.B [request]
The request to the PostgreSQL server (in SQL). Only SELECT are
supported. The default is 'SELECT now()'.
.TP
.SH OPTIONS
.TP
.B \-c, \-\-conninfo
Connection information for the Postgresql server. Something like 'host=foo dbname=bar'
.TP
.B \-r, \-\-readall
Read all the data sent by the Postgresql server
.SH SEE ALSO
.BR echoping (1),
.SH AUTHOR
Stephane Bortzmeyer <bortz@users.sourceforge.net>

@ -15,6 +15,7 @@ poptContext postgresql_poptcon;
PGconn *conn; PGconn *conn;
PGresult *res; PGresult *res;
char *conninfo; char *conninfo;
echoping_options global_options;
void void
postgresql_usage (const char *msg) postgresql_usage (const char *msg)
@ -28,23 +29,25 @@ postgresql_usage (const char *msg)
} }
char * char *
init (const int argc, const char **argv) init (const int argc, const char **argv,
const echoping_options global_external_options)
{ {
int value; int value;
char *msg = malloc (256); char *msg = malloc (256);
char *hostname;
/* popt variables */ /* popt variables */
struct poptOption options[] = { struct poptOption options[] = {
{"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0, {"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0,
"Connection information for the Postgresql server. Something like 'host=foo dbname=bar''", "Connection information for the Postgresql server. Something like 'host=foo dbname=bar''",
"request"}, ""},
{"request", 'r', POPT_ARG_STRING, &request, 0,
"Request/query (in SQL) to send to the Postgresql server. Only SELECT are supported.",
"request"},
{"readall", 'a', POPT_ARG_NONE, &readall, 0, {"readall", 'a', POPT_ARG_NONE, &readall, 0,
"Read all the data sent by the Postgresql server", "Read all the data sent by the Postgresql server",
""}, ""},
POPT_AUTOHELP POPT_TABLEEND POPT_AUTOHELP POPT_TABLEEND
}; };
global_options = global_external_options;
if (global_options.udp)
err_quit ("UDP makes no sense for a PostgreSQL connection");
postgresql_poptcon = poptGetContext (NULL, argc, postgresql_poptcon = poptGetContext (NULL, argc,
argv, options, argv, options,
POPT_CONTEXT_KEEP_FIRST); POPT_CONTEXT_KEEP_FIRST);
@ -58,10 +61,12 @@ init (const int argc, const char **argv)
postgresql_usage (msg); postgresql_usage (msg);
} }
} }
if (request == NULL) /* TODO: a default like SELECT now()? */ hostname = poptGetArg (postgresql_poptcon); /* Not used */
postgresql_usage ("Mandatory request missing"); request = poptGetArg (postgresql_poptcon);
if (request == NULL)
request = "SELECT now()";
if (conninfo == NULL) if (conninfo == NULL)
postgresql_usage ("Mandatory connection information missing"); conninfo = "";
return NULL; /* We only use the conninfo, echoping does not see our hostname or port */ return NULL; /* We only use the conninfo, echoping does not see our hostname or port */
} }
@ -89,6 +94,8 @@ execute ()
printf ("Cannot run \"%s\": %s\n", request, PQresultErrorMessage (res)); printf ("Cannot run \"%s\": %s\n", request, PQresultErrorMessage (res));
return -1; return -1;
} }
if (global_options.verbose)
printf ("%d tuples returned\n", PQntuples (res));
if (readall) if (readall)
{ {
for (row = 0; row++; row < PQntuples (res)) for (row = 0; row++; row < PQntuples (res))

@ -29,7 +29,7 @@ whois_usage (const char *msg)
} }
char * char *
init (const int argc, const char **argv) init (const int argc, const char **argv, echoping_options global_options)
{ {
int value; int value;
char *msg = malloc (256); char *msg = malloc (256);
@ -43,6 +43,9 @@ init (const int argc, const char **argv)
""}, ""},
POPT_AUTOHELP POPT_TABLEEND POPT_AUTOHELP POPT_TABLEEND
}; };
if (global_options.udp)
err_quit ("UDP is incompatible with this whois plugin");
/* Will probably be cached before because /etc/services have no entry for UDP port 43 */
whois_poptcon = poptGetContext (NULL, argc, whois_poptcon = poptGetContext (NULL, argc,
argv, options, POPT_CONTEXT_KEEP_FIRST); argv, options, POPT_CONTEXT_KEEP_FIRST);
while ((value = poptGetNextOpt (whois_poptcon)) > 0) while ((value = poptGetNextOpt (whois_poptcon)) > 0)
@ -105,5 +108,7 @@ execute ()
return 1; return 1;
} }
void terminate() { void
terminate ()
{
} }

Loading…
Cancel
Save