3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on fakerunlevel.cc by Jacques Gelinas
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)
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.
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.
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.
38 #include <sys/types.h>
43 showHelp(int fd, int exit_code)
46 "Usage: fakerunlevel <runlevel> <utmp_file>\n\n"
47 "Put a runlevel record in file <utmp_file>\n\n"
48 "Please report bugs to " PACKAGE_BUGREPORT "\n");
57 "fakerunlevel " VERSION "\n"
58 "This program is part of " PACKAGE_STRING "\n\n"
59 "Copyright (C) 2003 Enrico Scholz\n"
60 VERSION_COPYRIGHT_DISCLAIMER);
64 int main (int argc, char *argv[])
66 if (argc==1) showHelp(2,1);
67 if (strcmp(argv[1], "--help")==0) showHelp(1,0);
68 if (strcmp(argv[1], "--version")==0) showVersion();
69 if (argc!=3) showHelp(2,1);
72 int const runlevel = atoi(argv[1]);
73 char const * const fname = argv[2];
78 char *gid_str = getenv("UTMP_GID");
80 if (runlevel<0 || runlevel>6) showHelp(2,1);
82 if (chroot(".")==-1 ||
84 perror("chroot()/chdir()");
88 // Real NSS is too expensive/insecure in this state; therefore, use the
89 // value detected at ./configure stage or overridden by $UTMP_GID
91 gid = gid_str ? atoi(gid_str) : UTMP_GID;
92 if (setgroups(1,&gid)==-1 ||
95 perror("setgid()/getgid()");
100 fd = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0664);
113 memset (&ut,0,sizeof(ut));
114 ut.ut_type = RUN_LVL;
115 ut.ut_pid = ('#' << 8) + runlevel+'0';