use 'exec-cd' instead of 'secure-umount'
[util-vserver.git] / util-vserver / src / fakerunlevel.c
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on fakerunlevel.cc by Jacques Gelinas
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 as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 /*
21         This program add a RUNLEVEL record in a utmp file.
22         This is used when a vserver lack a private init process
23         so runlevel properly report the fake runlevel.
24 */
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include "util.h"
30
31 #include <utmp.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40
41 static void
42 showHelp(int fd, int exit_code)
43 {
44   WRITE_MSG(fd,
45             "Usage: fakerunlevel <runlevel> <utmp_file>\n\n"
46             "Put a runlevel record in file <utmp_file>\n\n"
47             "Please report bugs to " PACKAGE_BUGREPORT "\n");
48
49   exit(exit_code);
50 }
51
52 static void
53 showVersion()
54 {
55   WRITE_MSG(1,
56             "fakerunlevel " VERSION "\n"
57             "This program is part of " PACKAGE_STRING "\n\n"
58             "Copyright (C) 2003 Enrico Scholz\n"
59             VERSION_COPYRIGHT_DISCLAIMER);
60   exit(0);
61 }
62
63 int main (int argc, char *argv[])
64 {
65   if (argc==1) showHelp(2,1);
66   if (strcmp(argv[1], "--help")==0)    showHelp(1,0);
67   if (strcmp(argv[1], "--version")==0) showVersion();
68   if (argc!=3) showHelp(2,1);
69
70   {
71     int  const          runlevel = atoi(argv[1]);
72     char const * const  fname    = argv[2];
73     int                 fd;
74     struct utmp         ut;
75     
76     gid_t               gid;
77     char                *gid_str = getenv("UTMP_GID");
78     
79     if (runlevel<0 || runlevel>6) showHelp(2,1);
80
81     if (chroot(".")==-1 ||
82         chdir("/")==-1) {
83       perror("chroot()/chdir()");
84       return EXIT_FAILURE;
85     }
86
87       // Real NSS is too expensive/insecure in this state; therefore, use the
88       // value detected at ./configure stage or overridden by $UTMP_GID
89       // env-variable
90     gid = gid_str ? atoi(gid_str) : UTMP_GID;
91     if (setgid(gid)==-1 ||
92         getgid()!=gid) {
93       perror("setgid()/getgid()");
94       return EXIT_FAILURE;
95     }
96
97     umask(002);
98     fd = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0664);
99     if (fd==-1) {
100       perror("open()");
101       return EXIT_FAILURE;
102     }
103
104     if (close(fd)==-1) {
105       perror("close()");
106       return EXIT_FAILURE;
107     }
108
109     utmpname (fname);
110     setutent();
111     memset (&ut,0,sizeof(ut));
112     ut.ut_type = RUN_LVL;
113     ut.ut_pid  = ('#' << 8) + runlevel+'0';
114     pututline (&ut);
115     endutent();
116   }
117
118   return EXIT_SUCCESS;
119 }