minor/medium cleanups
[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[] = "Radn";
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             " [-Radn] [--] <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             "Please report bugs to " PACKAGE_BUGREPORT "\n");
60   exit(res);
61 }
62
63 void
64 showVersion()
65 {
66   WRITE_MSG(1,
67             "lsxid " VERSION " -- shows the context which is associated to a file\n"
68             "This program is part of " PACKAGE_STRING "\n\n"
69             "Copyright (C) 2004 Enrico Scholz\n"
70             VERSION_COPYRIGHT_DISCLAIMER);
71   exit(0);
72 }
73
74 void
75 fixupParams(struct Arguments UNUSED * args, int UNUSED argc)
76 {
77 }
78
79 static xid_t
80 getFileContext(char const *name, struct stat const *exp_st)
81 {
82   int           fd = open(name, O_RDONLY);
83   xid_t         res;
84   
85   if (fd==-1) {
86     perror("open()");
87     return VC_NOCTX;
88   }
89
90   if (exp_st)
91     checkForRace(fd, name, exp_st);
92
93   res = vc_X_get_filecontext(fd);
94   if (res==VC_NOCTX)
95     perror("vc_X_get_filecontext()");
96   
97   close(fd);
98
99   return res;
100 }
101
102 bool
103 handleFile(char const *name, char const *display_name,
104            struct stat const *exp_st)
105 {
106   xid_t         ctx = 0;
107   char          buf[MAX(sizeof(ctx)*3+1, 20)];
108   bool          need_write = true;
109
110   memset(buf, ' ', sizeof buf);
111
112   if (S_ISLNK(exp_st->st_mode)) {
113     memcpy(buf, "-------", 7);
114     write(1, buf, sizeof buf);
115     need_write = false;
116   }
117   else {
118     ctx = getFileContext(name, exp_st);
119   
120     if (ctx==VC_NOCTX) {
121       memcpy(buf, "!!ERR!!", 7);
122       write(1, buf, sizeof buf);
123       need_write = false;
124     }
125     else if (global_args->do_mapping) {
126       char const *      vname = vc_getVserverByCtx(ctx, 0,0);
127       if (!vname) buf[0] = '\0';
128       else {
129         size_t          l = strlen(vname);
130         if (l<sizeof(buf)) write(1, buf, sizeof(buf)-l);
131         write(1, vname, l);
132
133         free((char *)vname);
134         need_write = false;
135       }
136     }
137   }
138
139   if (need_write) {
140     size_t      l = utilvserver_fmt_ulong(buf, ctx);
141     if (l<sizeof(buf)) write(1, buf+l, sizeof(buf)-l);
142     write(1, buf, l);
143   }
144
145   write(1, "  ", 2);
146   write(1, display_name, strlen(display_name));
147   write(1, "\n", 1);
148
149   return ctx!=VC_NOCTX;
150 }