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