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