added and documented '-x' switch
[util-vserver.git] / util-vserver / src / lsxid.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "fstool.h"
24 #include "util.h"
25
26 #include <lib/vserver.h>
27 #include <lib/vserver-internal.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 struct option const
38 CMDLINE_OPTIONS[] = {
39   { "help",     no_argument,  0, CMD_HELP },
40   { "version",  no_argument,  0, CMD_VERSION },
41   { 0,0,0,0 }
42 };
43
44 char const              CMDLINE_OPTIONS_SHORT[] = "Radnx";
45
46 void
47 showHelp(int fd, char const *cmd, int res)
48 {
49   WRITE_MSG(fd, "Usage:  ");
50   WRITE_STR(fd, cmd);
51   WRITE_MSG(fd,
52             " [-Radnx] [--] <file>*\n\n"
53             " Options:\n"
54             "   -R  ...  recurse through directories\n"
55             "   -a  ...  display files starting with '.' also\n"
56             "   -d  ...  list directories like other files instead of\n"
57             "            listing their content\n"
58             "   -n  ...  do not try to do xid -> vserver-name mapping\n"
59             "   -x  ...  do not cross filesystems\n\n"
60             "Please report bugs to " PACKAGE_BUGREPORT "\n");
61   exit(res);
62 }
63
64 void
65 showVersion()
66 {
67   WRITE_MSG(1,
68             "lsxid " VERSION " -- shows the context which is associated to a file\n"
69             "This program is part of " PACKAGE_STRING "\n\n"
70             "Copyright (C) 2004 Enrico Scholz\n"
71             VERSION_COPYRIGHT_DISCLAIMER);
72   exit(0);
73 }
74
75 void
76 fixupParams(struct Arguments UNUSED * args, int UNUSED argc)
77 {
78 }
79
80 static xid_t
81 getFileContext(char const *name, struct stat const *exp_st)
82 {
83   int           fd = open(name, O_RDONLY);
84   xid_t         res;
85   
86   if (fd==-1) {
87     perror("open()");
88     return VC_NOCTX;
89   }
90
91   if (exp_st)
92     checkForRace(fd, name, exp_st);
93
94   res = vc_X_get_filecontext(fd);
95   if (res==VC_NOCTX)
96     perror("vc_X_get_filecontext()");
97   
98   close(fd);
99
100   return res;
101 }
102
103 bool
104 handleFile(char const *name, char const *display_name,
105            struct stat const *exp_st)
106 {
107   xid_t         ctx = 0;
108   char          buf[MAX(sizeof(ctx)*3+1, 20)];
109   bool          need_write = true;
110
111   memset(buf, ' ', sizeof buf);
112
113   if (S_ISLNK(exp_st->st_mode)) {
114     memcpy(buf, "-------", 7);
115     write(1, buf, sizeof buf);
116     need_write = false;
117   }
118   else {
119     ctx = getFileContext(name, exp_st);
120   
121     if (ctx==VC_NOCTX) {
122       memcpy(buf, "!!ERR!!", 7);
123       write(1, buf, sizeof buf);
124       need_write = false;
125     }
126     else if (global_args->do_mapping) {
127       char const *      vname = vc_getVserverByCtx(ctx, 0,0);
128       if (!vname) buf[0] = '\0';
129       else {
130         size_t          l = strlen(vname);
131         if (l<sizeof(buf)) write(1, buf, sizeof(buf)-l);
132         write(1, vname, l);
133
134         free((char *)vname);
135         need_write = false;
136       }
137     }
138   }
139
140   if (need_write) {
141     size_t      l = utilvserver_fmt_ulong(buf, ctx);
142     if (l<sizeof(buf)) write(1, buf+l, sizeof(buf)-l);
143     write(1, buf, l);
144   }
145
146   write(1, "  ", 2);
147   write(1, display_name, strlen(display_name));
148   write(1, "\n", 1);
149
150   return ctx!=VC_NOCTX;
151 }