Path: blob/main/contrib/bmake/filemon/filemon_dev.c
39535 views
/* $NetBSD: filemon_dev.c,v 1.9 2022/03/04 23:17:16 sjg Exp $ */12/*3* Copyright (c) 2020 The NetBSD Foundation, Inc.4* All rights reserved.5*6* This code is derived from software contributed to The NetBSD Foundation7* by Taylor R. Campbell.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS19* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED20* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS22* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/3031#include "filemon.h"3233#include <sys/ioctl.h>3435#include <errno.h>36#include <fcntl.h>37#include <stdlib.h>38#include <unistd.h>3940#ifdef HAVE_FILEMON_H41# include <filemon.h>42#endif4344#ifndef _PATH_FILEMON45#define _PATH_FILEMON "/dev/filemon"46#endif4748#ifndef MAKE_ATTR_UNUSED49#define MAKE_ATTR_UNUSED __attribute__((__unused__))50#endif5152struct filemon {53int fd;54};5556const char *57filemon_path(void)58{5960return _PATH_FILEMON;61}6263struct filemon *64filemon_open(void)65{66struct filemon *F;67unsigned i;68int error;6970/* Allocate and zero a struct filemon object. */71F = calloc(1, sizeof *F);72if (F == NULL)73return NULL;7475/* Try opening /dev/filemon, up to six times (cargo cult!). */76for (i = 0; (F->fd = open(_PATH_FILEMON, O_RDWR|O_CLOEXEC)) == -1; i++) {77if (i == 5) {78error = errno;79goto fail0;80}81}8283/* Success! */84return F;8586fail0: free(F);87errno = error;88return NULL;89}9091int92filemon_setfd(struct filemon *F, int fd)93{9495/* Point the kernel at this file descriptor. */96if (ioctl(F->fd, FILEMON_SET_FD, &fd) == -1)97return -1;9899/* No need for it in userland any more; close it. */100(void)close(fd);101102/* Success! */103return 0;104}105106void107filemon_setpid_parent(struct filemon *F MAKE_ATTR_UNUSED, pid_t pid MAKE_ATTR_UNUSED)108{109/* Nothing to do! */110}111112int113filemon_setpid_child(const struct filemon *F, pid_t pid)114{115116/* Just pass it on to the kernel. */117return ioctl(F->fd, FILEMON_SET_PID, &pid);118}119120int121filemon_close(struct filemon *F)122{123int error = 0;124125/* Close the filemon device fd. */126if (close(F->fd) == -1 && error == 0)127error = errno;128129/* Free the filemon descriptor. */130free(F);131132/* Set errno and return -1 if anything went wrong. */133if (error != 0) {134errno = error;135return -1;136}137138/* Success! */139return 0;140}141142int143filemon_readfd(const struct filemon *F MAKE_ATTR_UNUSED)144{145146return -1;147}148149int150filemon_process(struct filemon *F MAKE_ATTR_UNUSED)151{152153return 0;154}155156157