gdp / apps / gdp-find-xname.c @ master
History | View | Annotate | Download (3.42 KB)
1 | 815ee3e5 | Eric Allman | /* vim: set ai sw=4 sts=4 ts=4 : */
|
---|---|---|---|
2 | |||
3 | |||
4 | /*
|
||
5 | ** GDP-FIND-XNAME --- find the human name of a log
|
||
6 | **
|
||
7 | ** ... as long as it is listed in the metadata.
|
||
8 | **
|
||
9 | ** ----- BEGIN LICENSE BLOCK -----
|
||
10 | ** Applications for the Global Data Plane
|
||
11 | ** From the Ubiquitous Swarm Lab, 490 Cory Hall, U.C. Berkeley.
|
||
12 | **
|
||
13 | c87dd166 | Eric Allman | ** Copyright (c) 2015-2019, Regents of the University of California.
|
14 | 815ee3e5 | Eric Allman | ** All rights reserved.
|
15 | **
|
||
16 | ** Permission is hereby granted, without written agreement and without
|
||
17 | ** license or royalty fees, to use, copy, modify, and distribute this
|
||
18 | ** software and its documentation for any purpose, provided that the above
|
||
19 | ** copyright notice and the following two paragraphs appear in all copies
|
||
20 | ** of this software.
|
||
21 | **
|
||
22 | ** IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
|
||
23 | ** SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
|
||
24 | ** PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
|
||
25 | ** EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
26 | **
|
||
27 | ** REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
|
||
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||
29 | ** FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
|
||
30 | ** IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO
|
||
31 | ** OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
||
32 | ** OR MODIFICATIONS.
|
||
33 | ** ----- END LICENSE BLOCK -----
|
||
34 | */
|
||
35 | |||
36 | #include <gdp/gdp.h> |
||
37 | |||
38 | #include <ep/ep_app.h> |
||
39 | #include <ep/ep_dbg.h> |
||
40 | |||
41 | #include <sysexits.h> |
||
42 | |||
43 | db4fa341 | Eric Allman | /*
|
44 | ** Given an internal (base64-encoded) log name, check the metadata
|
||
45 | ** and print the external (human) name.
|
||
46 | */
|
||
47 | 815ee3e5 | Eric Allman | |
48 | void
|
||
49 | usage(void)
|
||
50 | { |
||
51 | fprintf(stderr, |
||
52 | "Usage: %s [-D dbg_spec] [-G router_addr] log-name\n"
|
||
53 | " -D set debugging flags\n"
|
||
54 | " -G IP host to contact for gdp_router\n",
|
||
55 | ep_app_getprogname()); |
||
56 | exit(EX_USAGE); |
||
57 | } |
||
58 | |||
59 | |||
60 | int
|
||
61 | main(int argc, char **argv) |
||
62 | { |
||
63 | int opt;
|
||
64 | bool show_usage = false; |
||
65 | EP_STAT estat; |
||
66 | eae1d3ec | Eric Allman | gdp_gin_t *gin; |
67 | 815ee3e5 | Eric Allman | char *gdpd_addr = NULL; |
68 | char buf[200]; |
||
69 | eae1d3ec | Eric Allman | gdp_name_t gdpiname; |
70 | 815ee3e5 | Eric Allman | |
71 | while ((opt = getopt(argc, argv, "D:G:")) > 0) |
||
72 | { |
||
73 | switch (opt)
|
||
74 | { |
||
75 | case 'D': |
||
76 | ep_dbg_set(optarg); |
||
77 | break;
|
||
78 | |||
79 | case 'G': |
||
80 | gdpd_addr = optarg; |
||
81 | break;
|
||
82 | |||
83 | default:
|
||
84 | show_usage = true;
|
||
85 | break;
|
||
86 | } |
||
87 | } |
||
88 | argc -= optind; |
||
89 | argv += optind; |
||
90 | |||
91 | if (show_usage || argc != 1) |
||
92 | usage(); |
||
93 | |||
94 | // initialize GDP library
|
||
95 | estat = gdp_init(gdpd_addr); |
||
96 | if (!EP_STAT_ISOK(estat))
|
||
97 | { |
||
98 | ep_app_error("GDP Initialization failed");
|
||
99 | goto fail0;
|
||
100 | } |
||
101 | |||
102 | // allow thread to settle to avoid interspersed debug output
|
||
103 | ep_time_nanosleep(INT64_C(100000000));
|
||
104 | |||
105 | eae1d3ec | Eric Allman | estat = gdp_parse_name(argv[0], gdpiname);
|
106 | 22f2276a | Eric Allman | if (EP_STAT_ISFAIL(estat))
|
107 | 815ee3e5 | Eric Allman | ep_app_fatal("Cannot parse log name %s", argv[0]); |
108 | eae1d3ec | Eric Allman | estat = gdp_gin_open(gdpiname, GDP_MODE_RO, NULL, &gin);
|
109 | 815ee3e5 | Eric Allman | EP_STAT_CHECK(estat, goto fail1);
|
110 | |||
111 | eae1d3ec | Eric Allman | gdp_md_t *md; |
112 | estat = gdp_gin_getmetadata(gin, &md); |
||
113 | 815ee3e5 | Eric Allman | EP_STAT_CHECK(estat, goto fail2);
|
114 | |||
115 | int indx;
|
||
116 | for (indx = 0; EP_STAT_ISOK(estat); indx++) |
||
117 | { |
||
118 | eae1d3ec | Eric Allman | gdp_md_id_t id; |
119 | 815ee3e5 | Eric Allman | size_t len; |
120 | const void *data; |
||
121 | |||
122 | eae1d3ec | Eric Allman | estat = gdp_md_get(md, indx, &id, &len, &data); |
123 | if (EP_STAT_ISOK(estat) && id == GDP_MD_XID)
|
||
124 | 815ee3e5 | Eric Allman | { |
125 | // found the name
|
||
126 | printf("%.*s\n", (int) len, (const char *) data); |
||
127 | break;
|
||
128 | } |
||
129 | } |
||
130 | |||
131 | if (!EP_STAT_ISOK(estat))
|
||
132 | printf("No name found\n");
|
||
133 | |||
134 | fail2:
|
||
135 | eae1d3ec | Eric Allman | // close logs / release resources
|
136 | 815ee3e5 | Eric Allman | //XXX someday
|
137 | |||
138 | if (false) |
||
139 | { |
||
140 | fail1:
|
||
141 | ep_app_error("Could not open log %s: %s",
|
||
142 | argv[0], ep_stat_tostr(estat, buf, sizeof buf)); |
||
143 | } |
||
144 | |||
145 | fail0:
|
||
146 | 9829d4ae | Eric Allman | ep_app_message(estat, "exiting with status");
|
147 | 815ee3e5 | Eric Allman | return EP_STAT_ISOK(estat) ? EX_OK : EX_UNAVAILABLE;
|
148 | } |