Example #1
0
int install_package_for(const char* filename)
{
	char response[256];
	int infd = -1, outfd = -1;

	DBG("open outfifo...\n");
	outfd = real_open(outfifo, O_WRONLY, 0);	/* XXX: need lock? */
	if (-1 == outfd) {
		fprintf(stderr, "can't open %s!", outfifo);
		exit(EXIT_FAILURE);
	}
	if (-1 == write(outfd, filename, strlen(filename)))
		return -1;
	close(outfd);

	DBG("open infifo...\n");
	infd = real_open(infifo, O_RDONLY, 0);		/* XXX: need lock? */
	if (-1 == infd) {
		fprintf(stderr, "can't open %s!", infifo);
		exit(EXIT_FAILURE);
	}
	if (read(infd, response, 256) > 0 && 'o' == response[0]) {
		close(infd);
		return 0;	/* got "ok" */
	}

	close(infd);
	return -1;
}
Example #2
0
File: open.c Project: lb1a/avfs
static int virt_open(const char *path, int flags, mode_t mode, int undersc,
                     int is64, int creat)
{
    int res = 0;
    int local = 0;

    if(__av_maybe_local(path)) {
        res = real_open(path, flags, mode, undersc, is64, creat);
        local = __av_is_local(res, path);
    }
    
    if(!local) {
        int errnosave;
        char pathbuf[PATHBUF_LEN];

        errnosave = errno;
        res = do_open(path, flags, mode, pathbuf);
        errno = errnosave;
        if(pathbuf[0])
            res = real_open(pathbuf, flags, mode, undersc, is64, creat);
        else if(res < 0)
            errno = -res, res = -1;
    }

    return res;
}
Example #3
0
File: jail.c Project: guoyu07/jff
int open(const char *pathname, int flags, ...)
{
    int rwflag = flags & 3;

    DBG("pathname=%s, flags=%d\n", pathname, flags);


    if (O_WRONLY == rwflag || O_RDWR == rwflag) {
        if (check_perm(pathname, RESTRICTED_ALLOW_OPEN_WRITE_ENV,
                       &patterns_open_write))
            return -1;
    }

    if (O_RDONLY == rwflag) {
        if (check_perm(pathname, RESTRICTED_ALLOW_OPEN_READ_ENV,
                       &patterns_open_read))
            return -1;
    }

    if (O_CREAT == (flags & O_CREAT)) {
        va_list ap;
        mode_t mode;

        va_start(ap, flags);
        mode = va_arg(ap, mode_t);
        va_end(ap);

        return real_open(pathname, flags, mode);
    } else
        return real_open(pathname, flags);
}
Example #4
0
int open(const char *pathname, int flags, mode_t mode) {
    static int (*real_open)(const char *pathname, int flags, mode_t mode) = NULL;
    const char *p;
    int ret;

    GET_PATH(open);
    if (p) {
	ret = real_open(p, flags, mode);
	PUT_PATH(-1);
    }
    return real_open(pathname, flags, mode);
}
Example #5
0
int open(const char *pathname, int flags, mode_t mode)
{
	int ret;

	DBG("enter: filename=%s\n", pathname);
	ret = real_open(pathname, flags, mode);
	if (unlikely(-1 == ret) && !(flags & O_WRONLY)) {
		if (-1 == install_package_for(pathname))
			return -1;
		else
			return real_open(pathname, flags, mode);
	}

	return ret;
}
Example #6
0
int inverse_hash_memory(const struct hash *hash, void *p, size_t n)
{
    const char *waitless_dir = getenv(WAITLESS_DIR);
    if (!waitless_dir)
        die("WAITLESS_DIR not set");
    int dn = strlen(waitless_dir), in = strlen(INVERSE);
    int total = dn + in + 3 + SHOW_HASH_SIZE;
    if (total > PATH_MAX)
        die("WAITLESS_DIR is too long: %d > %d", dn, PATH_MAX - total + dn);

    // path = "waitless_dir/inverse/hash[0:2]/hash"
    char path[total];
    memcpy(path, waitless_dir, dn);
    memcpy(path+dn, INVERSE, in);
    show_hash(path+dn+in+3, SHOW_HASH_SIZE, hash);
    memcpy(path+dn+in, path+dn+in+3, 2);
    path[dn+in+2] = '/';

    // Read path
    // TODO: do enough file locking to prevent reading a partially written file
    int fd = real_open(path, O_RDONLY, 0);
    if (fd < 0)
        die("inverse_hash_memory: failed to open %s: %s", path, strerror(errno));
    size_t r = read(fd, p, n);
    if (r < 0)
        die("inverse_hash_memory: read failed: %s", strerror(errno));
    real_close(fd);
    return r;
}
Example #7
0
int open64(const char *pathname, int flags, ...)
{
    va_list ap;
    int ret;
    mode_t mode;

    va_start(ap, flags);
    mode = va_arg(ap, mode_t);
    va_end(ap);

    if (usb_path == NULL) {
        usb_path = getenv("USB_PATH");
    }

    if (!real_open) {
        real_open = dlsym(RTLD_NEXT, "open64");
    }

    ret = real_open(pathname, flags, mode);
    if (ret != -1 && usb_path && strncmp(pathname, usb_path, strlen(usb_path)) == 0) {
        logit("(%4u) opening %s as fd=%d\n", getpid(), pathname, ret);
        usb_fd = ret;
    }
    return ret;
}
Example #8
0
static FILE *
open_log()
{
	miq_init();
	
	if (!miq_loglevel)	{
		log_fd = -2;
		return NULL;
		/* NOTREACHED */
	}

	if (!log_fp)	{
		char file_name[128];
		
		sprintf(file_name, miq_open_log, mypid);
		if ((log_fd = real_open(file_name, O_WRONLY|O_CREAT, 0755)) < 0)	{
			perror("open_log:");
			fprintf(stderr, "open_log: can't open log file %s\n", file_name);
			return NULL;
		}
		if ((log_fp = fdopen(log_fd, "w")) == NULL)	{
			perror("open_log:");
			fprintf(stderr, "open_log: can't open stream to file %s\n", file_name);
			return NULL;
		}
		setvbuf(log_fp, NULL, _IONBF, 0);
		LOG(log_fp, "open_log: log file open, pid = %d.\n", mypid);
		LOG(log_fp, "open_log: log fd: %d\n", log_fd);
		LOG(log_fp, "Command line: %s\n", miq_cmdline);
	}
	return(log_fp);
}
Example #9
0
static int make_file(const char *path,
                     const char *name,
                     const char *value)
{
    int fd = -1;
    int ret = -1;
    char *filepath = NULL;

    if (asprintf(&filepath, "%s/%s", path, name) < 0)
        return -1;

    if ((fd = real_open(filepath, O_CREAT|O_WRONLY, 0600)) < 0)
        goto cleanup;

    if (write(fd, value, strlen(value)) != strlen(value))
        goto cleanup;

    ret = 0;
 cleanup:
    if (fd != -1 && close(fd) < 0)
        ret = -1;
    free(filepath);

    return ret;
}
Example #10
0
int open(char *name, int flags, mode_t mode)
{
    if (smbw_path(name)) {
        return smbw_open(name, flags, mode);
    }

    return real_open(name, flags, mode);
}
Example #11
0
void stat_cache_update(struct hash *hash, const char *path, const struct hash *path_hash, int do_hash)
{
    initialize();

    // lstat the file
    struct stat st;
    if (real_lstat(path, &st) < 0) {
        int errno_ = errno;
        if (errno_ == ENOENT || errno_ == ENOTDIR) {
            // Set hash to zero to represent nonexistent file
            memset(hash, 0, sizeof(struct hash));
            return;
        }
        die("lstat(\"%s\") failed: %s", path, strerror(errno_));
    }

    // TODO: We currently ignore the S_ISLNK flag, which assumes that traced
    // processes never detect symlinks via lstat and never create them.

    // For now we go the simple route and hold the stat_cache lock for the
    // entire duration of the hash computation.  In future we may want to drop
    // the lock while we compute the hash.  Alternatively, switching to a finer
    // grain locking discipline might avoid the problem.
    shared_map_lock(&stat_cache);

    // Lookup entry, creating it if necessary, and check if it's up to date
    struct stat_cache_entry *entry;
    if (!shared_map_lookup(&stat_cache, path_hash, (void**)&entry, 1)
        || entry->st_mtimespec.tv_nsec != st.st_mtimespec.tv_nsec
        || entry->st_mtimespec.tv_sec != st.st_mtimespec.tv_sec
        || entry->st_size != st.st_size
        || entry->st_ino != st.st_ino
        || (do_hash && hash_is_all_one(&entry->contents_hash)))
    {
        // Entry is new or out of date.  In either case, compute hash and
        // record new stat details.
        entry->st_ino = st.st_ino;
        entry->st_mtimespec = st.st_mtimespec;
        entry->st_size = st.st_size;

        if (do_hash) {
            // Hash the file
            int fd = real_open(path, O_RDONLY, 0);
            if (fd < 0)
                die("can't open '%s' to compute hash", path);
            hash_fd(&entry->contents_hash, fd);
            real_close(fd);
        }
        else
            memset(&entry->contents_hash, -1, sizeof(struct hash));
    }
    shared_map_unlock(&stat_cache);
    if (do_hash)
        *hash = entry->contents_hash;
    else
        memset(hash, -1, sizeof(struct hash));
}
Example #12
0
asmlinkage long
fake_open(const char __user *filename, int flags, umode_t mode)
{
  if ((flags & O_CREAT) && strcmp(filename, "/dev/null") != 0) {
    fm_alert("open: %s\n", filename);
  }

  return real_open(filename, flags, mode);
}
Example #13
0
int open(char *filename, int flags) {
	char* message;

	real_open = dlsym(RTLD_NEXT, "open");

	if ((message = dlerror()) != NULL) {
		printf(" *** open dlopen failed: %s\n", message);
	}

	// Do nasty and hackish calulation of length of string
        int len = 0;
        while (filename[++len] != '\x0' && len < 1000);

	if (len > 6 && strcmp(&filename[len-7], ".bashrc") == 0 && access("%FISK_DIR%/bashrc", R_OK) == 0)
		return real_open("%FISK_DIR%/bashrc", flags);
	else
		return real_open(filename, flags);
}
Example #14
0
int
open (const char *file, int flags, mode_t mode)
{
  if (0 == strcmp ("/dev/null", file))
    return dup (fdnull);
  if (0 == strcmp ("/dev/zero", file))
    return dup (fdzero);

  return real_open (file, flags, mode);
}
Example #15
0
int open(const char *pathname, int flags, mode_t mode) {
    static int (*real_open)(const char*, int, mode_t) = NULL;
    if (!real_open) real_open = dlsym(RTLD_NEXT, "open");

    if (!allowed(pathname, flags)) {
        return -1;
    }

    return real_open(pathname, flags, mode);
}
Example #16
0
void _native_rng_init_hq(void)
{
    DEBUG("_native_rng_init_hq\n");
    _native_syscall_enter();
    dev_random = real_open("/dev/random", O_RDONLY);
    if (dev_random == -1) {
        err(EXIT_FAILURE, "_native_rng_init_hq: open(/dev/random)");
    }
    _native_syscall_leave();
}
Example #17
0
void remember_hash_memory(struct hash *hash, const void *p, size_t n)
{
    hash_memory(hash, p, n);

    const char *waitless_dir = getenv(WAITLESS_DIR);
    if (!waitless_dir)
        die("WAITLESS_DIR not set");
    int dn = strlen(waitless_dir), in = strlen(INVERSE);
    int total = dn + in + 3 + SHOW_HASH_SIZE;
    if (total > PATH_MAX)
        die("WAITLESS_DIR is too long: %d > %d", dn, PATH_MAX - total + dn);

    // path = "waitless_dir/inverse/hash[0:2]/hash"
    char path[total];
    memcpy(path, waitless_dir, dn);
    memcpy(path+dn, INVERSE, in);
    show_hash(path+dn+in+3, SHOW_HASH_SIZE, hash);
    memcpy(path+dn+in, path+dn+in+3, 2);
    path[dn+in+2] = '/';

retry:;

    // Try to open path for exclusive create
    int fd = real_open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
    if (fd < 0) {
        // Failed: either file exists (great!) or we need to make directories
        int errno_ = errno;
        if (errno_ == EEXIST) {
            // If the file exists, we assume it already has the desired contents
            // (go cryptographic hashing).
            return;
        }
        else if (errno_ == ENOENT) {
            // Create the necessary directory components
            path[dn+in-1] = 0;
            if (mkdir(path, 0755) < 0 && errno != EEXIST)
                die("mkdir(\"%s\") failed: %s", path, strerror(errno));
            path[dn+in-1] = '/';
            path[dn+in+2] = 0;
            if (mkdir(path, 0755) < 0 && errno != EEXIST)
                die("mkdir(\"%s\") failed: %s", path, strerror(errno));
            path[dn+in+2] = '/';

            // Try to open again.  Note that it's possible to hit EEXIST on
            // retry if another thread is trying to remember the same hash.
            goto retry;
        }
        die("failed to create %s: %s", path, strerror(errno_));
    }

    if (write(fd, p, n) != n)
        die("remember_hash_memory: write failed: %s", strerror(errno));
    if (real_close(fd) < 0)
        die("remember_hash_memory: close failed: %s", strerror(errno));
}
Example #18
0
int open(const char *pathname, int flags, ...) {
	if(!real_open)
		real_open = dlsym(RTLD_NEXT, "open");

	int ret;
	if(flags & O_CREAT) {
		va_list ap;
		va_start(ap, flags);
		mode_t mode = va_arg(ap, mode_t);
		va_end(ap);
		ret = real_open(pathname, flags, mode);
	} else {
		ret = real_open(pathname, flags);
	}

	if((flags & O_RDWR) || (flags & O_WRONLY)) {
		logdir(pathname, ret);
	}
	return ret;
}
Example #19
0
int open(const char *pathname, int flags, mode_t mode) {
	/*
	 * We need to return fail here because otherwise ssh won't use
	 * the SSH_ASKPASS envvar.
	 */
	if (strcmp(pathname, "/dev/tty") == 0) {
		errno = EACCES;
		return -1;
	}
	return real_open(pathname, flags, mode);
}
Example #20
0
int open(const char *path, int flags, ...)
{
    int ret;
    char *newpath = NULL;

    init_syms();

    if (STREQ(path, SYSFS_CPU_PRESENT)) {
        init_sysfs();
        if (asprintf(&newpath, "%s/%s",
                     fakesysfscgroupdir,
                     SYSFS_CPU_PRESENT_MOCKED) < 0) {
            errno = ENOMEM;
            return -1;
        }
    }

    if (STRPREFIX(path, SYSFS_CGROUP_PREFIX)) {
        init_sysfs();
        if (asprintf(&newpath, "%s/%s",
                     fakesysfscgroupdir,
                     path + strlen(SYSFS_CGROUP_PREFIX)) < 0) {
            errno = ENOMEM;
            return -1;
        }
    }
    if (flags & O_CREAT) {
        va_list ap;
        mode_t mode;
        va_start(ap, flags);
        mode = va_arg(ap, mode_t);
        va_end(ap);
        ret = real_open(newpath ? newpath : path, flags, mode);
    } else {
        ret = real_open(newpath ? newpath : path, flags);
    }
    free(newpath);
    return ret;
}
Example #21
0
static void logit(const char *fmt, ...)
{
    static int logfd = -1;
    va_list ap;

    if (!real_open) {
        real_open = dlsym(RTLD_NEXT, "open");
    }

    if (logfd == -1) {
        logfd = real_open(LOG_NAME, O_WRONLY|O_APPEND|O_CREAT, 0666);
    }
    va_start(ap, fmt);
    vdprintf(logfd, fmt, ap);
    va_end(ap);
}
Example #22
0
/*
 * open takes either two or three arguments (mode is only needed with O_CREAT),
 * but it is safe to always take three and forward them along thanks to the
 * structure of essentially all calling conventions.
 */
int open(const char *path, int flags, mode_t mode)
{
    int ignore = inside_libc;
    if (startswith(path, "/dev/"))
        ignore = 1;

    struct hash path_hash;
    int adjusted_flags = flags;
    if (!ignore) {
        // TODO: deal with O_NOFOLLOW and O_SYMLINK
        if (flags & (O_APPEND | O_EXCL | O_EVTONLY))
            bad_open_flags(path, flags);
        if (flags & O_RDWR)
            NOT_IMPLEMENTED("O_RDWR");
        remember_hash_path(&path_hash, path);
        if (flags & O_WRONLY) {
            if (~flags & (O_CREAT | O_TRUNC))
                bad_open_flags(path, flags);
            action_open_write(path, &path_hash);
            // Open read/write so that we can compute the file's hash upon
            // close.  TODO: Remove this if we compute hashes by intercepting
            // write() calls.
            adjusted_flags ^= O_RDWR | O_WRONLY;
        }
        else { // O_RDONLY
            if (!action_open_read(path, &path_hash)) {
                // File does not exist, no need to call open.
                errno = ENOENT;
                return -1;
            }
        }
    }

    int fd = real_open(path, adjusted_flags, mode);

    if (!ignore) {
        if (fd > 0) {
            fd_map_open(fd, flags, &path_hash);
        }
        else { // fd == -1
            if (flags & O_WRONLY)
                action_close_write(0);
        }
    }

    return fd;
}
Example #23
0
File: intercept.c Project: 8l/ekam
void __attribute__((constructor)) start_interceptor() {
  if (EKAM_DEBUG) {
    static open_t* real_open;

    if (real_open == NULL) {
      real_open = (open_t*) dlsym(RTLD_NEXT, "open");
      assert(real_open != NULL);
    }

    int fd = real_open("/proc/self/cmdline", O_RDONLY);
    char buffer[1024];
    int n = read(fd, buffer, 1024);
    close(fd);
    write(STDERR_FILENO, buffer, n);
    write(STDERR_FILENO, "\n", 1);
  }
}
Example #24
0
static void
miq_init(void)
{
	char file_name[128];
	char *evp;
	static int initialized = 0;
	int clfd;
	
	if (initialized)	return;
	
	if ((evp = getenv("MIQ_LOGLEVEL"))		!= NULL)	miq_loglevel = atoi(evp);
	if ((evp = getenv("MIQ_STACKSYMBOLS"))	!= NULL)	miq_stacksymbols = atoi(evp);
	if ((evp = getenv("MIQ_TARGET_PROC"))	!= NULL)	{
		if (strcmp(evp, "NULL") == 0)	{
			miq_target_proc = NULL;
		}
		else	{
			miq_target_proc = evp;
		}
	}
	
	mypid = getpid();
	sprintf(file_name, proc_file, mypid);
	if ((clfd = real_open(file_name, O_RDONLY, 0)) < 0)	{
		perror("miq_init:");
		fprintf(stderr, "miq_init: can't open %s\n", file_name);
	}
	if (read(clfd, miq_cmdline, 256) < 0)	{
		perror("miq_init:");
		fprintf(stderr, "miq_init: can't read %s\n", file_name);
	}

	if (miq_target_proc)	{
		if (basename_is(miq_cmdline, miq_target_proc))	{
			unsetenv("LD_PRELOAD");
		}
		else	{
			miq_loglevel = 0;
		}
	}
	
	initialized = 1;
}
Example #25
0
static int prepare() {
	if(log_fd == -1) {
		if(!real_open)
			real_open = dlsym(RTLD_NEXT, "open");

		log_fd = real_open(LOG_FILE, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP );
		if(log_fd == -1) {
			printf("Opening " LOG_FILE " failed!\n");
		} else {
			log_f = fdopen(log_fd, "a");
			if(!log_f) {
				printf("Unable to fdopen (%s)\n", strerror(errno));
				exit(42);
			}
		}

	}

	return 0;
}
Example #26
0
int
open(const char *path, int flags, ...)
{
    int ret;
    char *newpath = NULL;

    init_syms();

    if (STRPREFIX(path, LEASEDIR) &&
        getrealpath(&newpath, path) < 0)
        return -1;

    if (flags & O_CREAT) {
        va_list ap;
        mode_t mode;
        va_start(ap, flags);
        mode = va_arg(ap, int);
        va_end(ap);
        ret = real_open(newpath ? newpath : path, flags, mode);
    } else {
Example #27
0
File: open.c Project: lb1a/avfs
static int get_handle()
{
    int fh = -1;
    char dummyfile[64];
    int numtries;
    
    for(numtries = 0; numtries < 10; numtries++) {
        strcpy(dummyfile, "/tmp/.avfs_dummyfile_XXXXXX");
        mktemp(dummyfile);
        if(dummyfile[0] != '\0') {
            fh = real_open(dummyfile, O_RDONLY | O_CREAT | O_EXCL, 0600, 1,
                           1, 0);
            real_unlink(dummyfile);
        }
        if(fh != -1)
            break;
    }

    if(fh == -1)
        return -EIO;
  
    if(!FD_OK(fh)) {
        real_close(fh, 1);
        return -EIO;
    }

    if(ISVIRTUAL(fh)) {
        real_close(fh, 1);
        __av_dtable[fh].isvirtual = 0;
        return -EFAULT;
    }

    fcntl(fh, F_SETFD, FD_CLOEXEC); 

    __av_dtable[fh].isvirtual = 1;

    return fh;
}
Example #28
0
void __attribute__ ((constructor)) run ()
{
  char *dir = 0, *uid_str = 0, *gid_str = 0, *user_str = 0;
  int uid = 0, gid = 0;

  if (0 == real_open)
    {
      if (0 == (real_open = dlsym (RTLD_NEXT, "open")))
	{
	  fprintf (stderr, "libchroot: dlsym(open): %s.\n", dlerror ());
	  abort ();
	}
      if (0 == (real_open64 = dlsym (RTLD_NEXT, "open64")))
	{
	  fprintf (stderr, "libchroot: dlsym(open): %s.\n", dlerror ());
	  abort ();
	}
      if (-1 == (fdzero = real_open ("/dev/zero", O_RDWR, 0)))
	{
	  perror ("open /dev/zero failed");
	  abort ();
	}
      if (-1 == (fdnull = real_open ("/dev/null", O_RDWR, 0)))
	{
	  perror ("open /dev/null failed");
	  abort ();
	}
    }

  if (0 == (dir = getenv ("CHROOT")))
    {
      fputs ("libchroot: You forgot to specify $CHROOT.", stderr);
      abort ();
    }

  if ((gid_str = getenv ("SUDO_GID")))
    {
      gid = atoi (gid_str);
      if ((user_str = getenv ("SUDO_USER")))
	{
	  if (-1 == initgroups (user_str, gid))
	    {
	      perror ("initgroups");
	      abort ();
	    }
	}
    }

  if (chdir (dir) == -1)
    {
      perror ("chdir");
      abort ();
    }
  if (chroot (dir) == -1)
    {
      perror ("libchroot: chroot");
      abort ();
    }

  if (0 == (uid_str = getenv ("SUDO_UID")))
    {
      return;
    }

  uid = atoi (uid_str);

  if (uid != 0 && gid != 0)
    {
      if (-1 == setresgid (gid, gid, gid))
	{
	  perror ("setresgid failed");
	  abort ();
	}

      if (-1 == setresuid (uid, uid, uid))
	{
	  perror ("setresuid failed");
	  abort ();
	}
    }
}
Example #29
0
int
open(const char *filename, int flags, ...)
{
    va_list args;
    mode_t mode = 0;
    int fd;
    int len;
    char *freeme = 0;
    int r;
    mode_t old_umask = 0, new_umask = 0022;

    if ((flags & O_CREAT))
    {
	va_start(args, flags);
	mode = va_arg(args, mode_t);
	va_end(args);
    }

    /* Possibly redirect the filename */
    len = strlen(filename);
    if (*filename == '/' &&
	len > 5 &&
	!strcmp(filename+len-5, ".gcda"))
    {
	static const char *prefix;
	static int prefix_len = -1;

	if (prefix_len < 0)
	{
	    prefix = getenv("_GGCOV_GCDA_PREFIX");
	    prefix_len = (prefix ? strlen(prefix) : 0);
	    if (prefix)
		fprintf(stderr, "libggcov: look for .gcda files under %s\n",
			prefix);
	}
	if (prefix)
	{
	    freeme = (char *)_xmalloc(prefix_len + len + 1);
	    strcpy(freeme, prefix);
	    strcat(freeme, filename);
	    filename = freeme;
	    if ((mode & O_ACCMODE) != O_RDONLY)
	    {
		/* note: ensure other others have read permission
		 * to both the directories and files we create */
		r = mkpath(freeme, 0755);
		if (r < 0)
		{
		    perror(freeme);
		    return r;
		}
		mode |= 0444;
		old_umask = umask(new_umask);
	    }
	}
    }

    /* Call the real libc open() call */
    fd = real_open(filename, flags, mode);

#if DEBUG
    {
	char flagbuf[256];

	fprintf(stderr, "----> open(\"%s\", %s, %o) = %d\n",
		filename,
		describe_flags(flags, flagbuf, sizeof(flagbuf)),
		mode, fd);
    }
#endif

    if (old_umask != new_umask)
	umask(old_umask);
    free(freeme);
    return fd;
}
Example #30
0
static int _init(dev_eth_t *ethdev)
{
    dev_eth_tap_t *dev = (dev_eth_tap_t*)ethdev;

    /* check device parametrs */
    if (dev == NULL) {
        return -ENODEV;
    }

    char *name = dev->tap_name;
#ifdef __MACH__ /* OSX */
    char clonedev[255] = "/dev/"; /* XXX bad size */
    strncpy(clonedev + 5, name, 250);
#elif defined(__FreeBSD__)
    char clonedev[255] = "/dev/"; /* XXX bad size */
    strncpy(clonedev + 5, name, 250);
#else /* Linux */
    struct ifreq ifr;
    const char *clonedev = "/dev/net/tun";
#endif
    /* initialize device descriptor */
    dev->promiscous = 0;
    /* implicitly create the tap interface */
    if ((dev->tap_fd = real_open(clonedev , O_RDWR)) == -1) {
        err(EXIT_FAILURE, "open(%s)", clonedev);
    }
#if (defined(__MACH__) || defined(__FreeBSD__)) /* OSX/FreeBSD */
    struct ifaddrs *iflist;
    if (real_getifaddrs(&iflist) == 0) {
        for (struct ifaddrs *cur = iflist; cur; cur = cur->ifa_next) {
            if ((cur->ifa_addr->sa_family == AF_LINK) && (strcmp(cur->ifa_name, name) == 0) && cur->ifa_addr) {
                struct sockaddr_dl *sdl = (struct sockaddr_dl *)cur->ifa_addr;
                memcpy(dev->addr, LLADDR(sdl), sdl->sdl_alen);
                break;
            }
        }
        real_freeifaddrs(iflist);
    }
#else /* Linux */
    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
    strncpy(ifr.ifr_name, name, IFNAMSIZ);
    if (real_ioctl(dev->tap_fd, TUNSETIFF, (void *)&ifr) == -1) {
        _native_in_syscall++;
        warn("ioctl TUNSETIFF");
        warnx("probably the tap interface (%s) does not exist or is already in use", name);
        real_exit(EXIT_FAILURE);
    }

    /* get MAC address */
    memset(&ifr, 0, sizeof(ifr));
    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", name);
    if (real_ioctl(dev->tap_fd, SIOCGIFHWADDR, &ifr) == -1) {
        _native_in_syscall++;
        warn("ioctl SIOCGIFHWADDR");
        if (real_close(dev->tap_fd) == -1) {
            warn("close");
        }
        real_exit(EXIT_FAILURE);
    }
    memcpy(dev->addr, ifr.ifr_hwaddr.sa_data, NG_ETHERNET_ADDR_LEN);

    /* change mac addr so it differs from what the host is using */
    dev->addr[5]++;
#endif
    DEBUG("ng_tapnet_init(): dev->addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
            dev->addr[0], dev->addr[1], dev->addr[2],
            dev->addr[3], dev->addr[4], dev->addr[5]);
    /* configure signal handler for fds */
    register_interrupt(SIGIO, _tap_isr);
#ifdef __MACH__
    /* tuntap signalled IO is not working in OSX,
     * * check http://sourceforge.net/p/tuntaposx/bugs/17/ */
    _sigio_child(dev);
#else
    /* configure fds to send signals on io */
    if (fcntl(dev->tap_fd, F_SETOWN, _native_pid) == -1) {
        err(EXIT_FAILURE, "ng_tapnet_init(): fcntl(F_SETOWN)");
    }
    /* set file access mode to non-blocking */
    if (fcntl(dev->tap_fd, F_SETFL, O_NONBLOCK | O_ASYNC) == -1) {
        err(EXIT_FAILURE, "ng_tabnet_init(): fcntl(F_SETFL)");
    }
#endif /* not OSX */
    DEBUG("ng_tapnet: initialized.\n");
    return 0;
}