Exemple #1
0
static pid_t fork_with_pipes(int pipefd_in[2], int pipefd_out[2])
{
	pid_t child_pid;

	if (pipe(pipefd_in) < 0) {
		LOG_ERROR(nfqueue, "%s", errno_error(errno));
		return -1;
	}

	if (pipefd_out) {
		if (pipe(pipefd_out) < 0) {
			LOG_ERROR(nfqueue, "%s", errno_error(errno));
			close(pipefd_in[0]);
			close(pipefd_in[1]);
			return -1;
		}
	}

	child_pid = fork();
	if (child_pid < 0) {
		LOG_ERROR(nfqueue, "%s", errno_error(errno));
		close(pipefd_in[0]);
		close(pipefd_in[1]);

		if (pipefd_out) {
			close(pipefd_out[0]);
			close(pipefd_out[1]);
		}
		return -1;
	}

	return child_pid;
}
Exemple #2
0
static int open_send_socket(bool mark)
{
	int fd;
	int one = 1;

	fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
	if (fd < 0) {
		messagef(HAKA_LOG_ERROR, MODULE_NAME, L"cannot open send socket: %s", errno_error(errno));
		return -1;
	}

	if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0) {
		messagef(HAKA_LOG_ERROR, MODULE_NAME, L"cannot setup send socket: %s", errno_error(errno));
		return -1;
	}

	if (mark) {
		one = 0xffff;
		if (setsockopt(fd, SOL_SOCKET, SO_MARK, &one, sizeof(one)) < 0) {
			messagef(HAKA_LOG_ERROR, MODULE_NAME, L"cannot setup send socket: %s", errno_error(errno));
			return -1;
		}
	}

	return fd;
}
Exemple #3
0
bool mutex_init(mutex_t *mutex, bool recursive)
{
    int err;
    pthread_mutexattr_t attr;

    err = pthread_mutexattr_init(&attr);
    if (err) {
        error("mutex error: %s", errno_error(err));
        return false;
    }

    if (recursive)
        err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    else
        err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);

    if (err) {
        error("mutex error: %s", errno_error(err));
        return false;
    }

    err = pthread_mutex_init(mutex, &attr);
    pthread_mutexattr_destroy(&attr);
    if (err) {
        error("mutex error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #4
0
static pid_t fork_with_pipes(int pipefd_in[2], int pipefd_out[2])
{
	pid_t child_pid;

	if (pipe(pipefd_in) < 0) {
		messagef(HAKA_LOG_ERROR, MODULE_NAME, "%s", errno_error(errno));
		return -1;
	}

	if (pipefd_out) {
		if (pipe(pipefd_out) < 0) {
			messagef(HAKA_LOG_ERROR, MODULE_NAME, "%s", errno_error(errno));
			close(pipefd_in[0]);
			close(pipefd_in[1]);
			return -1;
		}
	}

	child_pid = fork();
	if (child_pid < 0) {
		messagef(HAKA_LOG_ERROR, MODULE_NAME, "%s", errno_error(errno));
		close(pipefd_in[0]);
		close(pipefd_in[1]);

		if (pipefd_out) {
			close(pipefd_out[0]);
			close(pipefd_out[1]);
		}
		return -1;
	}

	return child_pid;
}
status_t get_link_status(void) {
    struct stat sb;
    status_t status = STATUS_NONE, newstatus;
    int i;

    if (stat(NEWPATH, &sb) == -1) {
        if (errno != ENOENT)
            errno_error("Unable to stat %s", NEWPATH);
        if (mkdir(NEWPATH, 0755) == -1)
            errno_error("Unable to mkdir %s", NEWPATH);
        info("Directory %s created", NEWPATH);
    } else if (!S_ISDIR(sb.st_mode))
        error("%s exists but is not a directory", NEWPATH);


    for (i = 0; i < (int)(sizeof(filenames)/sizeof(filenames[0])); i++) {
        char srcpath[PATH_MAX], dstpath[PATH_MAX], linkdst[PATH_MAX];
        ssize_t ret;

        safe_snprintf(srcpath, sizeof(srcpath), "%s%s", NEWPATH, filenames[i]);
        safe_snprintf(dstpath, sizeof(dstpath), "%s%s", OLDPATH, filenames[i]);

        if ((ret = safe_readlink(srcpath, linkdst, sizeof(linkdst))) == -1) {
            if (errno == ENOENT) {
                if (status != STATUS_NONE) {
                    warn("Inconistent state in %s", NEWPATH);
                }
                return STATUS_UNKNOWN;
            } else
                error("Unable to readlink %s", srcpath);
        }

        newstatus = (strcmp(linkdst, dstpath) == 0) ? STATUS_ENABLED  :
                    (strcmp(linkdst, DEVNULL) == 0) ? STATUS_DISABLED :
                    STATUS_UNKNOWN;
        if (newstatus == STATUS_UNKNOWN)
            warn("%s points to unknown file %s", srcpath, linkdst);

        if (status != STATUS_NONE && status != newstatus) {
            warn("Inconistent state in %s", NEWPATH);
            return STATUS_UNKNOWN;
        }

        status = newstatus;
    }

    return (status == STATUS_NONE) ? STATUS_UNKNOWN : status;
}
Exemple #6
0
size_t file::write(const void* buffer, size_t n)
{
    ssize_t count = ::write(_M_fd, buffer, n);
    if(count == -1) throw errno_error();

    return count;
}
Exemple #7
0
storage::offset size(const std::string& name)
{
    struct stat x;
    if(::stat(name.data(), &x)) throw errno_error();

    return x.st_size;
}
Exemple #8
0
offset file::seek(storage::offset offset, storage::origin origin)
{
    storage::offset n = ::lseek(_M_fd, offset, static_cast<int>(origin));
    if(n == -1) throw errno_error();

    return n;
}
Exemple #9
0
int file::control(int request, void* buffer)
{
    int val = ::ioctl(_M_fd, request, buffer);
    if(val == -1) throw errno_error();

    return val;
}
Exemple #10
0
static void report_error(struct luadebug_remote_user *user, int err)
{
	if (!user->error) {
		messagef(HAKA_LOG_ERROR, MODULE, L"remote communication error: %s", errno_error(err));
		user->error = true;
	}
}
Exemple #11
0
static int packet_do_receive(struct packet_module_state *state, struct packet **pkt)
{
	const int rv = recv(state->fd, state->receive_buffer,
			sizeof(state->receive_buffer), 0);
	if (rv < 0) {
		messagef(HAKA_LOG_ERROR, MODULE_NAME, L"packet reception failed, %s", errno_error(errno));
		return 0;
	}

	if (nfq_handle_packet(state->handle, state->receive_buffer, rv) == 0) {
		if (state->current_packet) {
			state->current_packet->state = state;
			*pkt = (struct packet*)state->current_packet;

			if (pcap) {
				dump_pcap(&pcap->in, state->current_packet);
			}

			state->current_packet = NULL;
			return 0;
		}
		else {
			return state->error;
		}
	}
	else {
		message(HAKA_LOG_ERROR, MODULE_NAME, L"packet processing failed");
		return 0;
	}
}
Exemple #12
0
size_t file::read(void* buffer, size_t max, bool wait)
{
    ssize_t count = 0;
    if(wait || can_read(std::chrono::seconds(0))) count = ::read(_M_fd, buffer, max);

    if(count == -1) throw errno_error();
    return count;
}
Exemple #13
0
bool thread_cancel(thread_t thread)
{
    const int err = pthread_cancel(thread);
    if (err) {
        error("thread cancel error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #14
0
bool thread_join(thread_t thread, void **ret)
{
    const int err = pthread_join(thread, ret);
    if (err) {
        error("thread join error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #15
0
bool thread_create(thread_t *thread, void *(*main)(void*), void *param)
{
    const int err = pthread_create(thread, NULL, main, param);
    if (err) {
        error("thread creation error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #16
0
bool local_storage_set(local_storage_t *key, const void *value)
{
    const int err = pthread_setspecific(*key, value);
    if (err) {
        error("local storage error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #17
0
bool mutex_unlock(mutex_t *mutex)
{
    const int err = pthread_mutex_unlock(mutex);
    if (err) {
        error("mutex error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #18
0
bool barrier_destroy(barrier_t *barrier)
{
    const int err = pthread_barrier_destroy(barrier);
    if (err) {
        error("barrier error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #19
0
bool semaphore_post(semaphore_t *semaphore)
{
    const int err = sem_post(semaphore);
    if (err) {
        error("semaphore error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #20
0
bool semaphore_init(semaphore_t *semaphore, uint32 initial)
{
    const int err = sem_init(semaphore, 0, initial);
    if (err) {
        error("semaphore error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #21
0
bool rwlock_unlock(rwlock_t *rwlock)
{
    const int err = pthread_rwlock_unlock(rwlock);
    if (err) {
        error("rwlock error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #22
0
bool spinlock_unlock(spinlock_t *lock)
{
    const int err = pthread_spin_unlock(lock);
    if (err) {
        error("spinlock error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #23
0
bool spinlock_init(spinlock_t *lock)
{
    const int err = pthread_spin_init(lock, PTHREAD_PROCESS_PRIVATE);
    if (err) {
        error("spinlock error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #24
0
bool thread_signal(thread_t thread, int sig)
{
    const int err = pthread_kill(thread, sig);
    if (err) {
        error("thread sigmask error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #25
0
bool thread_sigmask(int how, sigset_t *set, sigset_t *oldset)
{
    const int err = pthread_sigmask(how, set, oldset);
    if (err) {
        error("thread sigmask error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #26
0
bool barrier_wait(barrier_t *barrier)
{
    const int err = pthread_barrier_wait(barrier);
    if (err && err != PTHREAD_BARRIER_SERIAL_THREAD) {
        error("barrier error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #27
0
bool local_storage_init(local_storage_t *key, void (*destructor)(void *))
{
    const int err = pthread_key_create(key, destructor);
    if (err) {
        error("local storage error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #28
0
bool local_storage_destroy(local_storage_t *key)
{
    const int err = pthread_key_delete(*key);
    if (err) {
        error("local storage error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #29
0
bool barrier_init(barrier_t *barrier, uint32 count)
{
    const int err = pthread_barrier_init(barrier, NULL, count);
    if (err) {
        error("barrier error: %s", errno_error(err));
        return false;
    }
    return true;
}
Exemple #30
0
bool thread_setcancelstate(bool enable)
{
    const int err = pthread_setcancelstate(enable ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, NULL);
    if (err) {
        error("thread set cancel state error: %s", errno_error(err));
        return false;
    }
    return true;
}