1 /*
2   Copyright Ⓒ 1997, 1998, 1999, 2000, 2001  joost witteveen
3   Copyright Ⓒ 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009  Clint Adams
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, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 */
15 
16 #ifndef FAKEROOT_COMMUNICATE_H
17 #define FAKEROOT_COMMUNICATE_H
18 
19 #include "config.h"
20 #include "fakerootconfig.h"
21 
22 #define LCHOWN_SUPPORT
23 
24 /* I've got a chicken-and-egg problem here. I want to have
25    stat64 support, only running on glibc2.1 or later. To
26    find out what glibc we've got installed, I need to
27    #include <features.h>.
28    But, before including that file, I have to define _LARGEFILE64_SOURCE
29    etc, cause otherwise features.h will not define it's internal defines.
30    As I assume that pre-2.1 libc's will just ignore those _LARGEFILE64_SOURCE
31    defines, I hope I can get away with this approach:
32 */
33 
34 /*First, unconditionally define these, so that glibc 2.1 features.h defines
35   the needed 64 bits defines*/
36 #ifndef _LARGEFILE64_SOURCE
37 # define _LARGEFILE64_SOURCE
38 #endif
39 #ifndef _LARGEFILE_SOURCE
40 # define _LARGEFILE_SOURCE
41 #endif
42 
43 /* Then include features.h, to find out what glibc we run */
44 #ifdef HAVE_FEATURES_H
45 # include <features.h>
46 #endif
47 
48 #ifdef HAVE_SYS_FEATURE_TESTS_H
49 # include <sys/feature_tests.h>
50 #endif
51 
52 #ifdef __APPLE__
53 # include <AvailabilityMacros.h>
54 # ifndef MAC_OS_X_VERSION_10_5 1050
55 #  define MAC_OS_X_VERSION_10_5 1050
56 # endif
57 # ifndef MAC_OS_X_VERSION_10_6 1060
58 #  define MAC_OS_X_VERSION_10_6 1060
59 # endif
60 # if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
61 #  define HAVE_APPLE_STAT64 1
62 # endif
63 #endif
64 
65 /* Then decide whether we do or do not use the stat64 support */
66 #if defined HAVE_APPLE_STAT64 \
67 	|| (defined(sun) && !defined(__SunOS_5_5_1) && !defined(_LP64)) \
68 	|| (!defined __UCLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))) \
69 	|| (defined __UCLIBC__ && defined __UCLIBC_HAS_LFS__)
70 # define STAT64_SUPPORT
71 #else
72 # ifndef __APPLE__
73 #  warning Not using stat64 support
74 # endif
75 /* if glibc is 2.0 or older, undefine these again */
76 # undef STAT64_SUPPORT
77 # undef _LARGEFILE64_SOURCE
78 # undef _LARGEFILE_SOURCE
79 #endif
80 
81 /* Sparc glibc 2.0.100 is broken, dlsym segfaults on --fxstat64..
82 #define STAT64_SUPPORT */
83 
84 #include <sys/types.h>
85 #include <sys/stat.h>
86 #include <sys/utsname.h>
87 #ifdef HAVE_STDINT_H
88 # include <stdint.h>
89 #endif
90 #ifdef HAVE_INTTYPES_H
91 # include <inttypes.h>
92 #endif
93 
94 #ifndef FAKEROOT_FAKENET
95 # define FAKEROOTKEY_ENV          "FAKEROOTKEY"
96 #endif /* ! FAKEROOT_FAKENET */
97 
98 #define FAKEROOTUID_ENV           "FAKEROOTUID"
99 #define FAKEROOTGID_ENV           "FAKEROOTGID"
100 #define FAKEROOTEUID_ENV          "FAKEROOTEUID"
101 #define FAKEROOTEGID_ENV          "FAKEROOTEGID"
102 #define FAKEROOTSUID_ENV          "FAKEROOTSUID"
103 #define FAKEROOTSGID_ENV          "FAKEROOTSGID"
104 #define FAKEROOTFUID_ENV          "FAKEROOTFUID"
105 #define FAKEROOTFGID_ENV          "FAKEROOTFGID"
106 #define FAKEROOTDONTTRYCHOWN_ENV  "FAKEROOTDONTTRYCHOWN"
107 
108 #define FAKELIBDIR                "/usr/lib/fakeroot"
109 #define FAKELIBNAME               "libfakeroot.so.0"
110 #ifdef FAKEROOT_FAKENET
111 # define FD_BASE_ENV              "FAKEROOT_FD_BASE"
112 #endif /* FAKEROOT_FAKENET */
113 #ifdef FAKEROOT_DB_PATH
114 # define DB_SEARCH_PATHS_ENV      "FAKEROOT_DB_SEARCH_PATHS"
115 #endif /* FAKEROOT_DB_PATH */
116 
117 #ifdef __GNUC__
118 # define UNUSED __attribute__((unused))
119 #else
120 # define UNUSED
121 #endif
122 
123 #ifndef S_ISTXT
124 # define S_ISTXT S_ISVTX
125 #endif
126 
127 #ifndef ALLPERMS
128 # define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)/* 07777 */
129 #endif
130 
131 /* Define big enough _constant size_ types for the various types of the
132    stat struct. I cannot (or rather, shouldn't) use struct stat itself
133    in the communication between the fake-daemon and the client (libfake),
134    as the sizes elements of struct stat may depend on the compiler or
135    compile time options of the C compiler, or the C library used. Thus,
136    the fake-daemon may have to communicate with two clients that have
137    different views of struct stat (this is the case for libc5 and
138    libc6 (glibc2) compiled programmes on Linux). This currently isn't
139    enabled any more, but used to be in libtricks.
140 */
141 
142 enum {chown_func = 0,
143                chmod_func = 1,
144                mknod_func = 2,
145                stat_func = 3,
146                unlink_func = 4,
147                debugdata_func = 5,
148                reqoptions_func = 6,
149                listxattr_func = 7,
150                getxattr_func = 8,
151                setxattr_func = 9,
152                removexattr_func = 10,
153                last_func};
154 
155 typedef struct {
156   int func;
157   const char *name;
158   char *value;
159   size_t size;
160   int flags;
161   int rc;
162 } xattr_args;
163 
164 #include "message.h"
165 
166 extern const char *env_var_set(const char *env);
167 #ifndef STUPID_ALPHA_HACK
168 extern void send_stat(const struct stat *st, func_id_t f);
169 #else
170 extern void send_stat(const struct stat *st, func_id_t f,int ver);
171 #endif
172 extern void send_fakem(const struct fake_msg *buf);
173 #ifndef STUPID_ALPHA_HACK
174 extern void send_get_stat(struct stat *buf);
175 #else
176 extern void send_get_stat(struct stat *buf,int ver);
177 #endif
178 #ifndef STUPID_ALPHA_HACK
179 extern void send_get_xattr(struct stat *st, xattr_args *xattr);
180 #else
181 extern void send_get_xattr(struct stat *st, xattr_args *xattr, int ver);
182 #endif
183 extern void cpyfakefake (struct fakestat *b1, const struct fakestat *b2);
184 #ifndef STUPID_ALPHA_HACK
185 extern void cpystatfakem(struct     stat *st, const struct fake_msg *buf);
186 #else
187 extern void cpystatfakem(struct     stat *st, const struct fake_msg *buf, int ver);
188 #endif
189 
190 #ifndef FAKEROOT_FAKENET
191 extern int init_get_msg();
192 extern key_t get_ipc_key(key_t new_key);
193 # ifndef STUPID_ALPHA_HACK
194 extern void cpyfakemstat(struct fake_msg *b1, const struct stat *st);
195 # else
196 extern void cpyfakemstat(struct fake_msg *b1, const struct stat *st, int ver);
197 # endif
198 #else /* FAKEROOT_FAKENET */
199 # ifdef FAKEROOT_LIBFAKEROOT
200 extern volatile int comm_sd;
201 extern void lock_comm_sd(void);
202 extern void unlock_comm_sd(void);
203 # endif
204 #endif /* FAKEROOT_FAKENET */
205 
206 #ifdef STAT64_SUPPORT
207 #ifndef STUPID_ALPHA_HACK
208 extern void send_stat64(const struct stat64 *st, func_id_t f);
209 extern void send_get_stat64(struct stat64 *buf);
210 extern void send_get_xattr64(struct stat64 *st, xattr_args *xattr);
211 #else
212 extern void send_stat64(const struct stat64 *st, func_id_t f, int ver);
213 extern void send_get_stat64(struct stat64 *buf, int ver);
214 extern void send_get_xattr64(struct stat64 *st, xattr_args *xattr, int ver);
215 #endif
216 extern void stat64from32(struct stat64 *s64, const struct stat *s32);
217 extern void stat32from64(struct stat *s32, const struct stat64 *s64);
218 #endif
219 
220 #ifndef FAKEROOT_FAKENET
221 extern int msg_snd;
222 extern int msg_get;
223 extern int sem_id;
224 #endif /* ! FAKEROOT_FAKENET */
225 
226 #endif
227