046f828c68c74e10586ce2303b74bf6d4545d8f4
[util-vserver.git] / util-vserver / lib / getvservername.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2003 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.h"
24 #include "pathconfig.h"
25
26 #include <string.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <libgen.h>
30 #include <unistd.h>
31 #include <limits.h>
32
33 static char *
34 getRecentName(char *start, char *end)
35 {
36   char          *res = 0;
37   int           fd;
38   char          buf[PATH_MAX];
39   
40   strcpy(end, "/name");
41   fd = open(start, O_RDONLY);
42   if (fd!=-1) {
43     off_t       len;
44
45     if ((len=lseek(fd, 0, SEEK_END))!=-1 &&
46         (len<VC_LIMIT_VSERVER_NAME_LEN) &&
47         (lseek(fd, 0, SEEK_SET)!=-1)) {
48       char      buf[len+1];
49
50       if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))==len) {
51         while (len>0 && buf[len-1]=='\n') --len;
52         buf[len] = '\0';
53         if (len>0) res = buf;
54       }
55
56       close(fd);
57       return strdup(res);
58     }
59
60     close(fd);
61   }
62
63   if (res==0) {
64     int         old_fd = open(".", O_RDONLY);
65
66     *end = '\0';
67     
68     if (old_fd!=-1 &&
69         chdir(start)!=-1 &&
70         getcwd(buf, sizeof buf)==buf)
71       start = buf;
72
73     res = basename(start);
74
75     if (old_fd!=-1) {
76       fchdir(old_fd);
77       close(old_fd);
78     }
79   }
80
81   return strdup(res);
82 }
83
84 char *
85 vc_getVserverName(char const *id, vcCfgStyle style)
86 {
87   size_t                l1  = strlen(id);
88
89   if (style==vcCFG_NONE || style==vcCFG_AUTO)
90     style = vc_getVserverCfgStyle(id);
91
92   switch (style) {
93     case vcCFG_NONE             :  return 0;
94     case vcCFG_LEGACY           :  return strdup(id);
95     case vcCFG_RECENT_SHORT     :
96     {
97       char              buf[sizeof(CONFDIR "/") + l1 + sizeof("/name") - 1];
98
99       strcpy(buf,                         CONFDIR "/");
100       strcpy(buf+sizeof(CONFDIR "/") - 1, id);
101       
102       return getRecentName(buf, buf+sizeof(CONFDIR "/")+l1 - 1);
103     }
104     case vcCFG_RECENT_FULL      :
105     {
106       char              buf[l1 + sizeof("/name")];
107       strcpy(buf, id);
108
109       return getRecentName(buf, buf+l1);
110     }
111     default                     :  return 0;
112   }
113 }