summaryrefslogtreecommitdiff
blob: e2aab3f6ec8dda680e3d58ccb1e4fc8471b7453b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (C) 2002 Brad House <brad@mainstreetsoftworks.com>
 * Distributed under the terms of the GNU General Public License, v2 or later 
 * Author: Brad House <brad@mainstreetsoftworks.com>
 *
 * $Header: /var/cvsroot/gentoo-src/portage/src/sandbox-dev/Attic/sandbox_futils.c,v 1.3 2002/12/04 18:11:32 azarah Exp $
 * 
 */

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdarg.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>

#include "sandbox.h"


char *get_sandbox_path(char *argv0)
{
  char path[255];
  char *cwd = NULL;

  /* ARGV[0] specifies full path */
  if (argv0[0] == '/') {
    strncpy(path, argv0, 254);

  /* ARGV[0] specifies relative path */
  } else {
      getcwd(cwd, 253);
      sprintf(path, "%s/%s", cwd, argv0);
      if (cwd) free(cwd);
      cwd = NULL;
  }

  /* Return just directory */
  return(sb_dirname(path));
}

char *get_sandbox_lib(char *sb_path)
{
  char path[255];

  snprintf(path, 254, "/lib/%s", LIB_NAME);
  if (file_exist(path, 0) <= 0) {
    snprintf(path, 254, "%s%s", sb_path, LIB_NAME);
  }
  return(strdup(path));
}

char *get_sandbox_rc(char *sb_path)
{
  char path[255];

  snprintf(path, 254, "/usr/lib/portage/lib/%s", BASHRC_NAME);
  if (file_exist(path, 0) <= 0) {
    snprintf(path, 254, "%s%s", sb_path, BASHRC_NAME);
  }
  return(strdup(path));
}

char *get_sandbox_log()
{
  char path[255];
  char pid_string[20];
  char *sandbox_log_env = NULL;

  sprintf(pid_string, "%d", getpid());

  strcpy(path, LOG_FILE_PREFIX);
  sandbox_log_env = getenv(ENV_SANDBOX_LOG);
  if (sandbox_log_env) {
    strcat(path, sandbox_log_env);
    strcat(path, "-");
  }
  strcat(path, pid_string);
  strcat(path, LOG_FILE_EXT);
  return(strdup(path));
}

/* Obtain base directory name. Do not allow trailing / */
char *sb_dirname(const char *path)
{
  char *ret = NULL;
  char *ptr = NULL;
  int loc = 0, i;
  int cut_len = -1;

  /* don't think NULL will ever be passed, but just in case */
  if (NULL == path) return(strdup("."));

  /* Grab pointer to last slash */
  ptr = strrchr(path, '/');
  if (NULL == ptr) {
    return(strdup("."));
  }

  /* decimal location of pointer */
  loc = ptr - path;

  /* Remove any trailing slash */
  for (i = loc-1; i >= 0; i--) {
    if (path[i] != '/') {
      cut_len = i + 1;  /* make cut_len the length of the string to keep */
      break;
    }
  }
  
  /* It could have been just a plain /, return a 1byte 0 filled string */
  if (-1 == cut_len) return(strdup(""));

  /* Allocate memory, and return the directory */
  ret = (char *)malloc((cut_len + 1) * sizeof(char));
  memcpy(ret, path, cut_len);
  ret[cut_len] = 0;

  return(ret);
}

/*
char* dirname(const char* path)
{
  char* base = NULL;
  unsigned int length = 0;

  base = strrchr(path, '/');
  if (NULL == base)
  {
    return strdup(".");
  }
  while (base > path && *base == '/')
  {
    base--;
  }
  length = (unsigned int) 1 + base - path;

  base = malloc(sizeof(char)*(length+1));
  memmove(base, path, length);
  base[length] = 0;

  return base;
}*/

/* Convert text (string) modes to integer values */
int file_getmode(char *mode)
{
  int mde = 0;
  if (0 == strcasecmp(mode, "r+")) {
    mde = O_RDWR | O_CREAT;
  } else if (0 == strcasecmp(mode, "w+")) {
    mde = O_RDWR | O_CREAT | O_TRUNC;
  } else if (0 == strcasecmp(mode, "a+")) {
    mde = O_RDWR | O_CREAT | O_APPEND;
  } else if (0 == strcasecmp(mode, "r")) {
    mde = O_RDONLY;
  } else if (0 == strcasecmp(mode, "w")) {
    mde = O_WRONLY | O_CREAT | O_TRUNC;
  } else if (0 == strcasecmp(mode, "a")) {
    mde = O_WRONLY | O_APPEND | O_CREAT;
  } else {
    mde = O_RDONLY;
  }
  return(mde);
}

/* Get current position in file */
long file_tell(int fp)
{
  return(lseek(fp, 0L, SEEK_CUR));
}

/* lock the file, preferrably the POSIX way */
int file_lock(int fd, int lock, char *filename)
{
  int err;
#ifdef USE_FLOCK
  if (flock(fd, lock) < 0) {
    err = errno;
    fprintf(stderr, ">>> %s flock file lock: %s\n", filename, strerror(err));
    return 0;
  }
#else
  struct flock fl;
  fl.l_type = lock;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0L;
  fl.l_len = 0L;
  fl.l_pid = getpid();
  if (fcntl(fd, F_SETLKW, &fl) < 0) {
    err = errno;
    fprintf(stderr,  ">>> %s fcntl file lock: %s\n", filename, strerror(err));
    return 0;
  }
#endif
  return 1;
}

/* unlock the file, preferrably the POSIX way */
int file_unlock(int fd)
{
#ifdef USE_FLOCK
  if (flock(fd, LOCK_UN) < 0) {
    perror(">>> flock file unlock");
    return 0;
  }
#else
  struct flock fl;
  fl.l_type = F_UNLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0L;
  fl.l_len = 0L;
  fl.l_pid = getpid();
  if (fcntl(fd, F_SETLKW, &fl) < 0) {
    perror(">>> fcntl file unlock");
    return 0;
  }
#endif
  return 1;
}

/* Auto-determine from how the file was opened, what kind of lock to lock
 * the file with
 */
int file_locktype(char *mode)
{
#ifdef USE_FLOCK
  if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+')) || (NULL != strchr(mode, 'a')))
    return(LOCK_EX);
  return(LOCK_SH);
#else
  if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+')) || (NULL != strchr(mode, 'a')))
    return(F_WRLCK);
  return(F_RDLCK);
#endif
}

/* Use standard fopen style modes to open the specified file.  Also auto-determines and
 * locks the file either in shared or exclusive mode depending on opening mode
 */
int file_open(char *filename, char *mode, int perm_specified, ...)
{
  int fd;
  char error[250];
  va_list ap;
  int perm;

  if (perm_specified) {
    va_start(ap, perm_specified);
    perm = va_arg(ap, int);
    va_end(ap);
  }
  if (perm_specified) {
    fd = open(filename, file_getmode(mode), perm);
  } else {
    fd = open(filename, file_getmode(mode));
  }
  if (-1 == fd) {
    snprintf(error, 249, ">>> %s file mode: %s open", filename, mode);
    perror(error);
    return(fd);
  }
  /* Only lock the file if opening succeeded */
  if (-1 != fd) {
    if (0 == file_lock(fd, file_locktype(mode), filename)) {
      close(fd);
      return -1;
    }
  } else {
    snprintf(error, 249, ">>> %s file mode:%s open", filename, mode);
    perror(error);
  }
  return(fd);
}

/* Close and unlock file */
void file_close(int fd)
{
  if (-1 != fd) {
    file_unlock(fd);
    close(fd);
  }
}

/* Return length of file */
long file_length(int fd)
{
  long pos, len;
  pos = file_tell(fd);
  len = lseek(fd, 0L, SEEK_END);
  lseek(fd, pos, SEEK_SET);
  return(len);
}

/* Zero out file */
int file_truncate(int fd)
{
  lseek(fd, 0L, SEEK_SET);
  if (ftruncate(fd, 0) < 0) {
    perror(">>> file truncate");
    return 0;
  }
  return 1;
}

/* Check to see if a file exists Return: 1 success, 0 file not found, -1 error */
int file_exist(char *filename, int checkmode)
{
  struct stat mystat;

  /* Verify file exists and is regular file (not sym link) */
  if (checkmode) {
    if (-1 == lstat(filename, &mystat)) {
      /* file doesn't exist */
      if (ENOENT == errno) {
        return 0;
      } else { /* permission denied or other error */
        perror(">>> stat file");
        return -1;
      }
    }
    if (!S_ISREG(mystat.st_mode))
      return -1;

  /* Just plain verify the file exists */
  } else {
    if (-1 == stat(filename, &mystat)) {
      /* file does not exist */
      if (ENOENT == errno) {
        return 0;
      } else { /* permission denied or other error */
        perror(">>> stat file");
        return -1;
      }
    }
  }

  return 1;
}


// vim:expandtab noai:cindent ai