Whamcloud - gitweb
LU-4961 lustre: remove liblustre.h and obd.h from userspace
[fs/lustre-release.git] / lustre / utils / lshowmount.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/utils/lshowmount.h
35  *
36  * Author: Herb Wartens <wartens2@llnl.gov>
37  * Author: Jim Garlick <garlick@llnl.gov>
38  */
39
40 #ifndef _GNU_SOURCE
41 #define _GNU_SOURCE
42 #endif
43
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <getopt.h>
48 #include <dirent.h>
49 #include <string.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <arpa/inet.h>
53 #include <netdb.h>
54 #include <errno.h>
55 #include <libgen.h>
56
57 #include "nidlist.h"
58
59 #define PROC_DIRS {                     \
60         "/proc/fs/lustre/mgs",          \
61         "/proc/fs/lustre/mdt",          \
62         "/proc/fs/lustre/obdfilter",    \
63         NULL,                           \
64 }
65 #define PROC_EXPORTS_TMPL       "%s/%s/exports"
66 #define PROC_UUID_TMPL          "%s/%s/uuid"
67
68 static void print_nids(NIDList nidlist, int lookup, int enumerate, int indent);
69 static int lshowmount(int lookup, int enumerate, int verbose);
70 static void read_exports(char *exports, NIDList nidlist);
71
72 char *prog;
73
74 #define OPTIONS "ehlv"
75 static struct option long_options[] = {
76         {"enumerate", no_argument, 0, 'e'},
77         {"help",      no_argument, 0, 'h'},
78         {"lookup",    no_argument, 0, 'l'},
79         {"verbose",   no_argument, 0, 'v'},
80         {0, 0, 0, 0},
81 };
82
83 static void usage(void)
84 {
85         fprintf(stderr, "usage: %s [-e] [-h] [-l] [-v]\n", prog);
86         exit(1);
87 }
88
89 int main(int argc, char **argv)
90 {
91         int opt, optidx = 0;
92         int lopt = 0;
93         int vopt = 0;
94         int eopt = 0;
95
96         prog = basename(argv[0]);
97
98         while ((opt = getopt_long(argc, argv, OPTIONS, long_options, &optidx)) != -1) {
99                 switch (opt) {
100                         case 'e':       /* --enumerate */
101                                 eopt = 1;
102                                 break;
103                         case 'l':       /* --lookup */
104                                 lopt = 1;
105                                 break;
106                         case 'v':       /* --verbose */
107                                 vopt = 1;
108                                 break;
109                         case 'h':       /* --help */
110                         default:
111                                 usage();
112                 }
113         }
114
115         if (lshowmount(lopt, eopt, vopt) == 0) {
116                 fprintf(stderr, "%s: lustre server modules not loaded\n", prog);
117                 exit(1);
118         }
119         exit(0);
120 }
121
122 static void print_nids(NIDList nidlist, int lookup, int enumerate, int indent)
123 {
124         char *s, *sep = "\n", *pfx = "";
125
126         if (lookup)
127                 nl_lookup_ip(nidlist);
128         nl_sort(nidlist);
129         nl_uniq(nidlist);
130         if (nl_count(nidlist) > 0) {
131                 if (indent) {
132                         sep = "\n    ";
133                         pfx = "    ";
134                 }
135                 if (enumerate)
136                         s = nl_string(nidlist, sep);
137                 else
138                         s = nl_xstring(nidlist, sep);
139                 printf("%s%s\n", pfx, s);
140                 free(s);
141         }
142 }
143
144 static int lshowmount(int lookup, int enumerate, int verbose)
145 {
146         char *dirs[] = PROC_DIRS;
147         char exp[PATH_MAX + 1];
148         NIDList nidlist = NULL;
149         DIR *topdirp;
150         struct dirent *dp;
151         int i;
152         int opens = 0;
153
154         if (!verbose)
155                 nidlist = nl_create();
156         for (i = 0; dirs[i] != NULL; i++) {
157                 if ((topdirp = opendir(dirs[i])) == NULL)
158                         continue;
159                 while ((dp = readdir(topdirp))) {
160                         if (dp->d_type != DT_DIR)
161                                 continue;
162                         if (!strcmp(dp->d_name, "."))
163                                 continue;
164                         if (!strcmp(dp->d_name, ".."))
165                                 continue;
166                         sprintf(exp, PROC_EXPORTS_TMPL, dirs[i], dp->d_name);
167                         if (verbose) {
168                                 nidlist = nl_create();
169                                 read_exports(exp, nidlist);
170                                 printf("%s:\n", dp->d_name);
171                                 print_nids(nidlist, lookup, enumerate, 1);
172                                 nl_destroy(nidlist);
173                         } else
174                                 read_exports(exp, nidlist);
175                 }
176                 closedir(topdirp);
177                 opens++;
178         }
179         if (!verbose) {
180                 print_nids(nidlist, lookup, enumerate, 0);
181                 nl_destroy(nidlist);
182         }
183         return opens;
184 }
185
186 static int empty_proc_file(char *path)
187 {
188         int empty = 0;
189         char buf[36];
190         int fd;
191
192         if ((fd = open(path, O_RDONLY)) < 0 || read(fd, buf, sizeof(buf)) <= 0)
193                 empty = 1;
194         if (fd >= 0)
195                 close(fd);
196         return empty;
197 }
198
199 static void read_exports(char *exports, NIDList nidlist)
200 {
201         DIR *dirp;
202         struct dirent *dp;
203         char path[PATH_MAX + 1];
204
205         if ((dirp = opendir(exports))) {
206                 while ((dp = readdir(dirp))) {
207                         if (dp->d_type != DT_DIR)
208                                 continue;
209                         if (!strcmp(dp->d_name, "."))
210                                 continue;
211                         if (!strcmp(dp->d_name, ".."))
212                                 continue;
213                         if (strchr(dp->d_name, '@') == NULL)
214                                 continue;
215                         sprintf(path, PROC_UUID_TMPL, exports, dp->d_name);
216                         if (empty_proc_file(path))
217                                 continue;
218
219                         nl_add(nidlist, dp->d_name);
220                 }
221                 closedir(dirp);
222         }
223 }