From: Ian Kent <raven@themaw.net>
To: autofs mailing list <autofs@vger.kernel.org>
Cc: Gordon Lack <gordon.m.lack@gsk.com>,
"Lan Yixun (dlan)" <dennis.yxun@gmail.com>,
Leonardo Chiquitto <leonardo.lists@gmail.com>,
Dustin Polke <DuPol@gmx.de>
Subject: [PATCH 19/25] autofs-5.0.7 - teach dumpmaps to output simple key value pairs
Date: Mon, 19 Aug 2013 09:14:02 +0800 [thread overview]
Message-ID: <20130819011401.6472.10623.stgit@perseus.fritz.box> (raw)
In-Reply-To: <20130819010909.6472.32512.stgit@perseus.fritz.box>
The dumpmaps option doesn't allow maps to be output in <key, value>
pairs suitable for use as a file map.
This could be useful to save current maps as a backup for emergency
use.
If the dumpmaps option is given and is followed by two parameters,
"<map type> <map name>" then simple <key, value> pairs that would
be read in by a map read are printed to stdout if the given map type
and map name are found in the map configuration.
If the map is an LDAP map and there is more than one map of same name
in different base dns only the first map encountered by autofs will
be listed.
If the map type is an old style multi-map and any one of the map
names in the multi-map entry matches the given map name the entries
that would be used by autofs for the whole multi-map will be listed.
---
CHANGELOG | 1
daemon/automount.c | 51 +++++++++++---
include/master.h | 1
lib/master.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++
man/automount.8 | 18 ++++-
5 files changed, 243 insertions(+), 15 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
index 1583c76..07f9d6d 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -70,6 +70,7 @@
- fix compilation of lookup_ldap.c without sasl.
- fix dumpmaps multi output.
- try and cleanup after dumpmaps.
+- teach dumpmaps to output simple key value pairs.
25/07/2012 autofs-5.0.7
=======================
diff --git a/daemon/automount.c b/daemon/automount.c
index 6849272..2ce49bf 100644
--- a/daemon/automount.c
+++ b/daemon/automount.c
@@ -1725,7 +1725,8 @@ static void usage(void)
" -f --foreground do not fork into background\n"
" -r --random-multimount-selection\n"
" use ramdom replicated server selection\n"
- " -m --dumpmaps dump automounter maps and exit\n"
+ " -m --dumpmaps [<map type> <map name]"
+ " dump automounter maps and exit\n"
" -n --negative-timeout n\n"
" set the timeout for failed key lookups.\n"
" -O --global-options\n"
@@ -2125,22 +2126,33 @@ int main(int argc, char *argv[])
program);
#endif
- if (argc == 0)
- master_list = master_new(NULL, timeout, ghost);
- else
- master_list = master_new(argv[0], timeout, ghost);
-
- if (!master_list) {
- printf("%s: can't create master map %s", program, argv[0]);
- exit(1);
- }
-
if (dumpmaps) {
struct master_mapent *entry;
struct list_head *head, *p;
struct mapent_cache *nc;
+ const char *type = NULL;
+ const char *name = NULL;
+ const char *master = NULL;
+
+ if (argc > 0) {
+ if (argc >= 2) {
+ type = argv[0];
+ name = argv[1];
+ }
+ if (argc == 3)
+ master = argv[2];
+ }
+
+ if (master)
+ master_list = master_new(NULL, timeout, ghost);
+ else
+ master_list = master_new(master, timeout, ghost);
+ if (!master_list) {
+ printf("%s: can't create master map", program);
+ exit(1);
+ }
- open_log();
+ log_to_stderr();
master_init_scan();
@@ -2153,7 +2165,10 @@ int main(int argc, char *argv[])
master_list->nc = nc;
lookup_nss_read_master(master_list, 0);
- master_show_mounts(master_list);
+ if (type)
+ dump_map(master_list, type, name);
+ else
+ master_show_mounts(master_list);
head = &master_list->mounts;
p = head->next;
@@ -2168,6 +2183,16 @@ int main(int argc, char *argv[])
exit(0);
}
+ if (argc == 0)
+ master_list = master_new(NULL, timeout, ghost);
+ else
+ master_list = master_new(argv[0], timeout, ghost);
+
+ if (!master_list) {
+ printf("%s: can't create master map %s", program, argv[0]);
+ exit(1);
+ }
+
become_daemon(foreground, daemon_check);
if (pthread_attr_init(&th_attr)) {
diff --git a/include/master.h b/include/master.h
index 824707c..e77d5ba 100644
--- a/include/master.h
+++ b/include/master.h
@@ -112,6 +112,7 @@ int master_submount_list_empty(struct autofs_point *ap);
int master_notify_submount(struct autofs_point *, const char *path, enum states);
void master_notify_state_change(struct master *, int);
int master_mount_mounts(struct master *, time_t, int);
+int dump_map(struct master *, const char *, const char *);
int master_show_mounts(struct master *);
extern inline unsigned int master_get_logopt(void);
int master_list_empty(struct master *);
diff --git a/lib/master.c b/lib/master.c
index 734485e..964b547 100644
--- a/lib/master.c
+++ b/lib/master.c
@@ -1329,6 +1329,193 @@ static void print_map_info(struct map_source *source)
return;
}
+static int match_type(const char *source, const char *type)
+{
+ if (!strcmp(source, type))
+ return 1;
+ /* Sources file and files are synonymous */
+ if (!strncmp(source, type, 4) && (strlen(source) <= 5))
+ return 1;
+ return 0;
+}
+
+static char *get_map_name(char *string)
+{
+ char *name, *tmp;
+ char *start, *end, *base;
+
+ tmp = strdup(string);
+ if (!tmp) {
+ printf("error: allocation failure: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ base = basename(tmp);
+ end = strchr(base, ',');
+ if (end)
+ *end = '\0';
+ start = strchr(tmp, '=');
+ if (start)
+ start++;
+ else {
+ char *colon = strrchr(base, ':');
+ if (colon)
+ start = ++colon;
+ else
+ start = base;
+ }
+
+ name = strdup(start);
+ if (!name)
+ printf("error: allocation failure: %s\n", strerror(errno));
+ free(tmp);
+
+ return name;
+}
+
+static int match_name(struct map_source *source, const char *name)
+{
+ int argc = source->argc;
+ int ret = 0;
+ int i;
+
+ /*
+ * This can't work for old style "multi" type sources since
+ * there's no way to know from which map the cache entry came
+ * from and duplicate entries are ignored at map read time.
+ * All we can really do is list all the entries for the given
+ * multi map if one of its map names matches.
+ */
+ for (i = 0; i < argc; i++) {
+ if (i == 0 || !strcmp(source->argv[i], "--")) {
+ if (i != 0) {
+ i++;
+ if (i >= argc)
+ break;
+ }
+
+ if (source->argv[i] && *source->argv[i] != '-') {
+ char *map = get_map_name(source->argv[i]);
+ if (!map)
+ break;
+ if (!strcmp(map, name)) {
+ ret = 1;
+ free(map);
+ break;
+ }
+ free(map);
+ }
+ }
+ }
+
+ return ret;
+}
+
+int dump_map(struct master *master, const char *type, const char *name)
+{
+ struct list_head *p, *head;
+
+ if (list_empty(&master->mounts)) {
+ printf("no master map entries found\n");
+ return 1;
+ }
+
+ head = &master->mounts;
+ p = head->next;
+ while (p != head) {
+ struct map_source *source;
+ struct master_mapent *this;
+ struct autofs_point *ap;
+ time_t now = time(NULL);
+
+ this = list_entry(p, struct master_mapent, list);
+ p = p->next;
+
+ ap = this->ap;
+
+ /*
+ * Ensure we actually read indirect map entries so we can
+ * list them. The map reads won't read any indirect map
+ * entries (other than those in a file map) unless the
+ * browse option is set.
+ */
+ if (ap->type == LKP_INDIRECT)
+ ap->flags |= MOUNT_FLAG_GHOST;
+
+ /* Read the map content into the cache */
+ if (lookup_nss_read_map(ap, NULL, now))
+ lookup_prune_cache(ap, now);
+ else {
+ printf("failed to read map\n");
+ lookup_close_lookup(ap);
+ continue;
+ }
+
+ if (!this->maps) {
+ printf("no map sources found for %s\n", ap->path);
+ lookup_close_lookup(ap);
+ continue;
+ }
+
+ source = this->maps;
+ while (source) {
+ struct map_source *instance;
+ struct mapent *me;
+
+ instance = NULL;
+ if (source->type) {
+ if (!match_type(source->type, type)) {
+ source = source->next;
+ continue;
+ }
+ if (!match_name(source, name)) {
+ source = source->next;
+ continue;
+ }
+ instance = source;
+ } else {
+ struct map_source *map;
+
+ map = source->instance;
+ while (map) {
+ if (!match_type(map->type, type)) {
+ map = map->next;
+ continue;
+ }
+ if (!match_name(map, name)) {
+ map = map->next;
+ continue;
+ }
+ instance = map;
+ break;
+ }
+ }
+
+ if (!instance) {
+ source = source->next;
+ lookup_close_lookup(ap);
+ continue;
+ }
+
+ me = cache_lookup_first(source->mc);
+ if (!me)
+ printf("no keys found in map\n");
+ else {
+ do {
+ if (me->source == instance)
+ printf("%s\t%s\n", me->key, me->mapent);
+ } while ((me = cache_lookup_next(source->mc, me)));
+ }
+
+ lookup_close_lookup(ap);
+ return 1;
+ }
+ lookup_close_lookup(ap);
+ }
+
+ return 0;
+}
+
int master_show_mounts(struct master *master)
{
struct list_head *p, *head;
diff --git a/man/automount.8 b/man/automount.8
index 844b876..48f5c5a 100644
--- a/man/automount.8
+++ b/man/automount.8
@@ -57,8 +57,22 @@ Run the daemon in the foreground and log to stderr instead of syslog."
Enables the use of ramdom selection when choosing a host from a
list of replicated servers.
.TP
-.I "\-m, \-\-dumpmaps"
-Dump configured automounter maps, then exit.
+.I "\-m, \-\-dumpmaps [<map type> <map name]"
+With no parameters, list information about the configured automounter
+maps, then exit.
+
+If the dumpmaps option is given and is followed by two parameters,
+"<map type> <map name>" then simple "<key, value>" pairs that would
+be read in by a map read are printed to stdout if the given map type
+and map name are found in the map configuration.
+
+If the map is an LDAP map and there is more than one map of same name
+in different base dns only the first map encountered by autofs will
+be listed.
+
+If the map type is an old style multi-map and any one of the map
+names in the multi-map entry matches the given map name the entries
+that would be used by autofs for the whole multi-map will be listed.
.TP
.I "\-O, \-\-global-options"
Allows the specification of global mount options used for all master
next prev parent reply other threads:[~2013-08-19 1:14 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-19 1:11 [PATCH 00/25] Current autofs patch queue Ian Kent
2013-08-19 1:11 ` [PATCH 01/25] autofs-5.0.7 - don't override LDFLAGS in make rules Ian Kent
2013-08-19 1:12 ` [PATCH 02/25] autofs-5.0.7 - fix a couple of compiler warnings Ian Kent
2013-08-19 1:12 ` [PATCH 03/25] autofs-5.0.7 - add after sssd dependency to unit file Ian Kent
2013-08-19 1:12 ` [PATCH 04/25] autofs-5.0.7 - dont start readmap unless ready Ian Kent
2013-08-19 1:12 ` [PATCH 05/25] autofs-5.0.7 - fix crash due to thread unsafe use of libldap Ian Kent
2013-08-19 1:12 ` [PATCH 06/25] autofs-5.0.7 - fix compile error with heimdal support enabled Ian Kent
2013-08-20 3:36 ` Dennis Lan (dlan)
2013-08-20 7:34 ` Ian Kent
2013-08-19 1:12 ` [PATCH 07/25] autofs-5.0.7 - fix typo forced-shutdown should be force-shutdown Ian Kent
2013-08-19 1:12 ` [PATCH 08/25] autofs-5.0.7 - fix hesiod check error and use correct $(LIBS) setting Ian Kent
2013-08-19 1:12 ` [PATCH 09/25] autofs-5.0.7 - fix dead LDAP symbolic link when LDAP support is disabled Ian Kent
2013-08-19 1:13 ` [PATCH 10/25] autofs-5.0.7 - add missing libtirpc lib to mount_nfs.so when TIRPC enabled Ian Kent
2013-08-19 1:13 ` [PATCH 11/25] autofs-5.0.7 - use compiler determined by configure instead of hard-coded ones Ian Kent
2013-08-19 1:13 ` [PATCH 12/25] autofs-5.0.7 - remove hard-coded STRIP variable Ian Kent
2013-08-19 1:13 ` [PATCH 13/25] autofs-5.0.7 - use LIBS for link libraries Ian Kent
2013-08-19 1:13 ` [PATCH 14/25] autofs-5.0.7 - unbundle NOTSTRIP from DEBUG so they dont depend on each other Ian Kent
2013-08-19 1:13 ` [PATCH 15/25] autofs-5.0.7 - fix occasional build error when enable parallel compiling Ian Kent
2013-08-19 1:13 ` [PATCH 16/25] autofs-5.0.7 - fix compilation of lookup_ldap.c without sasl Ian Kent
2013-08-19 1:13 ` [PATCH 17/25] autofs-5.0.7 - fix dumpmaps multi output Ian Kent
2013-08-19 1:13 ` [PATCH 18/25] autofs-5.0.7 - try and cleanup after dumpmaps Ian Kent
2013-08-19 1:14 ` Ian Kent [this message]
2013-08-19 1:14 ` [PATCH 20/25] autofs-5.0.7 - fix syncronize handle_mounts() shutdown Ian Kent
2013-08-19 1:14 ` [PATCH 21/25] autofs-5.0.7 - fix fix wildcard multi map regression Ian Kent
2013-08-19 1:14 ` [PATCH 22/25] autofs-5.0.7 - improve timeout option description Ian Kent
2013-08-19 1:14 ` [PATCH 23/25] autofs-5.0.7 - only probe specific nfs version when requested Ian Kent
2013-08-19 1:14 ` [PATCH 24/25] autofs-5.0.7 - fix bad mkdir permission on create Ian Kent
2013-08-19 2:13 ` Ian Kent
2013-08-19 1:14 ` [PATCH 25/25] autofs-5.0.7 - setup program map env from macro table Ian Kent
2013-08-19 5:30 ` [PATCH 00/25] Current autofs patch queue Dennis Lan (dlan)
2013-08-20 2:55 ` Ian Kent
2013-08-20 4:52 ` Dennis Lan (dlan)
2013-09-02 10:34 ` Martin Wilck
2013-09-02 10:41 ` Gordon Lack
2013-09-02 11:04 ` Martin Wilck
2013-09-02 11:13 ` Gordon Lack
2013-09-02 12:17 ` Martin Wilck
2013-09-02 12:55 ` Gordon Lack
2013-09-02 13:15 ` Martin Wilck
2013-09-02 13:41 ` Gordon Lack
2013-09-02 14:11 ` Martin Wilck
2013-09-02 14:20 ` Gordon Lack
2013-09-02 14:49 ` Martin Wilck
2013-09-02 15:08 ` Gordon Lack
2013-09-02 15:23 ` Martin Wilck
2013-09-02 15:36 ` Gordon Lack
2013-09-06 8:11 ` Ian Kent
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130819011401.6472.10623.stgit@perseus.fritz.box \
--to=raven@themaw.net \
--cc=DuPol@gmx.de \
--cc=autofs@vger.kernel.org \
--cc=dennis.yxun@gmail.com \
--cc=gordon.m.lack@gsk.com \
--cc=leonardo.lists@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox