initial checkin
[util-vserver.git] / util-vserver / vserver-start / defaulttty.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 "vserver-start.h"
24 #include <pathconfig.h>
25
26 #include <lib_internal/string.h>
27
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 #define ENSC_WRAPPERS_UNISTD    1
33 #define ENSC_WRAPPERS_FCNTL     1
34 #include <wrappers.h>
35
36
37 inline static bool
38 checkTTY(char const /*@null@*/ *p)
39 {
40   return p!=0 && access(p, R_OK|W_OK)==0;
41 }
42
43 void
44 setDefaultTTY(PathInfo const *cfgdir, char const *dflt)
45 {
46   PathInfo      subpath = ENSC_STRING_FIXED("/apps/init/tty");
47   char          buf[ENSC_PI_APPSZ(*cfgdir, subpath)];
48   char const *  new_tty = 0;
49
50   do {
51     PathInfo    ttypath = *cfgdir;
52     
53     PathInfo_append(&ttypath, &subpath, buf);
54     new_tty = String_c_str(&ttypath, buf);
55     if (checkTTY(new_tty)) break;
56
57     new_tty = CONFDIR "/.defaults/apps/init/tty";
58     if (checkTTY(new_tty)) break;
59
60     new_tty = dflt;
61     if (checkTTY(new_tty)) break;
62
63     new_tty = "/dev/null";
64   } while (false);
65
66   int           fd_in  = Eopen(new_tty, O_RDONLY, 0);
67   if (fd_in!=0) {
68     Edup2(fd_in,  0);
69     Eclose(fd_in);
70   }
71   
72   int           fd_out = Eopen(new_tty, O_WRONLY, 0600);
73   if (fd_out!=1) {
74     Edup2(fd_out, 1);
75     Eclose(fd_out);
76   }
77   
78   Edup2(1, 2);
79 }