initial checkin
[util-vserver.git] / util-vserver / lib / personalitytype.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2005 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 "internal.h"
25 #include <lib_internal/util-dimof.h>
26 #include <linux/personality.h>
27
28 #include <string.h>
29 #include <strings.h>
30 #include <assert.h>
31
32 #define DECL(VAL) { #VAL, sizeof(#VAL)-1, (PER_ ## VAL) }
33
34 static struct Mapping_uint32 const VALUES[] = {
35   DECL(LINUX),
36   DECL(LINUX_32BIT),
37   DECL(SVR4),
38   DECL(SVR3),
39   DECL(SCOSVR3),
40   DECL(OSR5),
41   DECL(WYSEV386),
42   DECL(ISCR4),
43   DECL(BSD),
44   DECL(SUNOS),
45   DECL(XENIX),
46   DECL(LINUX32),
47   DECL(LINUX32_3GB),
48   DECL(IRIX32),
49   DECL(IRIXN32),
50   DECL(IRIX64),
51   DECL(RISCOS),
52   DECL(SOLARIS),
53   DECL(UW7),
54   DECL(HPUX),
55   DECL(OSF4)
56 };
57
58 static char const *
59 removePrefix(char const *str, size_t *len)
60 {
61   if ((len==0 || *len==0 || *len>4) &&
62       strncasecmp("per_", str, 4)==0) {
63     if (len && *len>4) *len -= 4;
64     return str+4;
65   }
66   else
67     return str;
68 }
69
70 uint_least32_t
71 vc_str2personalitytype(char const *str, size_t len)
72 {
73   char const    *tmp = removePrefix(str, &len);
74   ssize_t       idx  = utilvserver_value2text_uint32(tmp, len,
75                                                      VALUES, DIM_OF(VALUES));
76
77   if (idx==-1) return VC_BAD_PERSONALITY;
78   else         return VALUES[idx].val;
79 }