1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-22 17:35:59 +03:00

test.c->dmevent.c

This commit is contained in:
Alasdair Kergon 2005-04-28 14:49:41 +00:00
parent e4fe47bc19
commit 42712811e3
3 changed files with 142 additions and 80 deletions

View File

@ -16,18 +16,18 @@ srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@
TARGETS = test dmeventd
TARGETS = dmevent dmeventd
INSTALL_TYPE = install_dynamic
SOURCES = test.c dmeventd.c
CLEAN_TARGETS = test
SOURCES = dmevent.c dmeventd.c
CLEAN_TARGETS = dmevent
LDFLAGS += -ldl -ldevmapper -lpthread
include ../make.tmpl
test: $(OBJECTS) $(interfacedir)/libdevmapper.$(LIB_SUFFIX) $(top_srcdir)/lib/event/libdmevent.$(LIB_SUFFIX)
$(CC) -o $@ test.o $(LDFLAGS) \
dmevent: $(OBJECTS) $(interfacedir)/libdevmapper.$(LIB_SUFFIX) $(top_srcdir)/lib/event/libdmevent.$(LIB_SUFFIX)
$(CC) -o $@ dmevent.o $(LDFLAGS) \
-L$(interfacedir) -L$(DESTDIR)/lib -L$(top_srcdir)/lib/event -ldevmapper -ldmevent $(LIBS)
dmeventd: $(OBJECTS) $(interfacedir)/libdevmapper.$(LIB_SUFFIX) $(top_srcdir)/lib/event/libdmevent.$(LIB_SUFFIX)

137
daemons/dmeventd/dmevent.c Normal file
View File

@ -0,0 +1,137 @@
/*
* Copyright (C) 2005 Red Hat, Inc. All rights reserved.
*
* This file is part of the device-mapper userspace tools.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "libdevmapper.h"
#include "libdm-event.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
static enum event_type events = ALL_ERRORS; /* All until we can distinguish. */
static char *default_dso_name = "noop"; /* default DSO is noop */
static int default_reg = 1; /* default action is register */
/* Display help. */
static void print_usage(char *name)
{
char *cmd = strrchr(name, '/');
cmd = cmd ? cmd + 1 : name;
printf("Usage::\n"
"%s [options] <device>\n"
"\n"
"Options:\n"
" -d <dso> Specify the DSO to use.\n"
" -h Print this usage.\n"
" -l List registered devices.\n"
" -r Register for event (default).\n"
" -u Unregister for event.\n"
"\n", cmd);
}
/* Parse command line arguments. */
static int parse_argv(int argc, char **argv,
char **dso_name, char **device, int *reg, int *list)
{
int c;
const char *options = "d:hlru";
while ((c = getopt(argc, argv, options)) != -1) {
switch (c) {
case 'd':
*dso_name = optarg;
break;
case 'h':
print_usage(argv[0]);
exit(EXIT_SUCCESS);
case 'l':
*list = 1;
break;
case 'r':
*reg = 1;
break;
case 'u':
*reg = 0;
break;
default:
fprintf(stderr, "Unknown option '%c'.\n"
"Try '-h' for help.\n", c);
return 0;
}
}
if (!*list) {
if (optind >= argc) {
fprintf(stderr, "You need to specify a device.\n");
return 0;
}
*device = argv[optind];
}
return 1;
}
int main(int argc, char **argv)
{
int list = 0, ret, reg = default_reg;
char *device, *dso_name = default_dso_name;
if (!parse_argv(argc, argv, &dso_name, &device, &reg, &list))
exit(EXIT_FAILURE);
if (list) {
do {
ret = dm_get_next_registered_device(&dso_name, &device, &events);
if (!ret) {
printf("%s %s 0x%x\n", dso_name, device, events);
free(dso_name);
free(device);
}
} while (ret);
exit(EXIT_SUCCESS);
}
if ((ret = reg ? dm_register_for_event(dso_name, device, events) :
dm_unregister_for_event(dso_name, device, events))) {
fprintf(stderr, "Failed to %sregister %s: %s\n",
reg ? "": "un", device, strerror(-ret));
exit(EXIT_FAILURE);
}
printf("%s %sregistered successfully.\n", device, reg ? "" : "un");
exit(EXIT_SUCCESS);
}
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/

View File

@ -1,75 +0,0 @@
/*
* Copyright (C) 2005 Red Hat, Inc. All rights reserved.
*
* This file is part of the device-mapper userspace tools.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "libdevmapper.h"
#include "libdm-event.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
static char *dso_name = "kickme";
static char *device = "/dev/mapper/test";
static char *device1 = "/dev/mapper/test1";
enum event_type events = ALL_ERRORS;
static void log_return(int ret, char *what, char *device)
{
printf("%s returned \"%s\"\n", what, strerror(-ret));
if (ret < 0)
printf("%s %s FAILED :-(((\n", what, device);
else
printf("%s %s succeded :-)\n", what, device);
}
int main(int argc, char **argv)
{
int reg = 0;
if (argc > 1 && *argv[1] == 'r')
reg = 1;
if (reg) {
log_return(dm_register_for_event(dso_name, device, events),
"register", device);
log_return(dm_register_for_event(dso_name, device1, events),
"register", device1);
} else {
log_return(dm_unregister_for_event(dso_name, device, events),
"unregister", device);
log_return(dm_unregister_for_event(dso_name, device1, events),
"unregister", device1);
}
return 0;
}
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/