s/S_ISLNK/!S_ISREG/ -> ignore any non-regular files
[util-vserver.git] / util-vserver / src / chxid.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
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33
34 struct option const
35 CMDLINE_OPTIONS[] = {
36   { "help",     no_argument,  0, CMD_HELP },
37   { "version",  no_argument,  0, CMD_VERSION },
38   { 0,0,0,0 }
39 };
40
41 char const              CMDLINE_OPTIONS_SHORT[] = "Rc:x";
42
43 void
44 showHelp(int fd, char const *cmd, int res)
45 {
46   WRITE_MSG(fd, "Usage:  ");
47   WRITE_STR(fd, cmd);
48   WRITE_MSG(fd,
49             " -c <ctx|vserver> [-Rx] [--] <file>+\n\n"
50             " Options:\n"
51             "   -R  ...  recurse through directories\n"
52             "   -c  ...  assign the given context/vserver to the file(s)\n"
53             "   -x  ...  do not cross filesystems\n\n"
54             "Please report bugs to " PACKAGE_BUGREPORT "\n");
55   exit(res);
56 }
57
58 void
59 showVersion()
60 {
61   WRITE_MSG(1,
62             "chxid " VERSION " -- assigns a context to a file\n"
63             "This program is part of " PACKAGE_STRING "\n\n"
64             "Copyright (C) 2004 Enrico Scholz\n"
65             VERSION_COPYRIGHT_DISCLAIMER);
66   exit(0);
67 }
68
69 static bool
70 setFile(char const *name, char const * display_name, struct stat const *exp_st)
71 {
72   bool          res = false;
73   int           fd = open(name, O_RDONLY);
74
75   if (fd==-1) {
76     perror("open()");
77     return false;
78   }
79
80   if (!exp_st ||
81       !checkForRace(fd, name, exp_st))
82     goto err;
83
84   if (vc_X_set_filecontext(fd, global_args->ctx)==-1) {
85     //perror("vc_X_set_filecontext()");
86     perror(display_name);
87     goto err;
88   }
89
90   res = true;
91
92   err:
93   close(fd);
94   return res;
95 }
96
97 bool
98 handleFile(char const *name, char const * display_name,
99            struct stat const *exp_st)
100 {
101   if (!S_ISREG(exp_st->st_mode)) return true;
102   
103   return setFile(name, display_name, exp_st);
104 }
105
106 void
107 fixupParams(struct Arguments UNUSED *args, int argc)
108 {
109   if (optind==argc) {
110     WRITE_MSG(2, "No filename given; use '--help' for more information\n");
111     exit(1);
112   }
113
114   if (args->ctx_str==0) {
115     WRITE_MSG(2, "No context given; use '--help' for more information\n");
116     exit(1);
117   }
118
119   args->ctx = resolveCtx(args->ctx_str);
120 }