1224987Sjonathan/*- 2224987Sjonathan * Copyright (c) 2009 Robert N. M. Watson 3224987Sjonathan * All rights reserved. 4224987Sjonathan * 5224987Sjonathan * This software was developed at the University of Cambridge Computer 6224987Sjonathan * Laboratory with support from a grant from Google, Inc. 7224987Sjonathan * 8224987Sjonathan * Redistribution and use in source and binary forms, with or without 9224987Sjonathan * modification, are permitted provided that the following conditions 10224987Sjonathan * are met: 11224987Sjonathan * 1. Redistributions of source code must retain the above copyright 12224987Sjonathan * notice, this list of conditions and the following disclaimer. 13224987Sjonathan * 2. Redistributions in binary form must reproduce the above copyright 14224987Sjonathan * notice, this list of conditions and the following disclaimer in the 15224987Sjonathan * documentation and/or other materials provided with the distribution. 16224987Sjonathan * 17224987Sjonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18224987Sjonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19224987Sjonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20224987Sjonathan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21224987Sjonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22224987Sjonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23224987Sjonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24224987Sjonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25224987Sjonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26224987Sjonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27224987Sjonathan * SUCH DAMAGE. 28224987Sjonathan * 29224987Sjonathan * $FreeBSD$ 30224987Sjonathan */ 31224987Sjonathan 32224987Sjonathan#ifndef _SYS_PROCDESC_H_ 33224987Sjonathan#define _SYS_PROCDESC_H_ 34224987Sjonathan 35224987Sjonathan#ifdef _KERNEL 36224987Sjonathan#include <sys/selinfo.h> /* struct selinfo */ 37224987Sjonathan#include <sys/_lock.h> 38224987Sjonathan#include <sys/_mutex.h> 39224987Sjonathan 40224987Sjonathan/*- 41224987Sjonathan * struct procdesc describes a process descriptor, and essentially consists 42224987Sjonathan * of two pointers -- one to the file descriptor, and one to the process. 43224987Sjonathan * When both become NULL, the process descriptor will be freed. An important 44224987Sjonathan * invariant is that there is only ever one process descriptor for a process, 45224987Sjonathan * so a single file pointer will suffice. 46224987Sjonathan * 47224987Sjonathan * Locking key: 48224987Sjonathan * (c) - Constant after initial setup. 49224987Sjonathan * (p) - Protected by the process descriptor mutex. 50269619Semaste * (r) - Atomic reference count. 51224987Sjonathan * (s) - Protected by selinfo. 52224987Sjonathan * (t) - Protected by the proctree_lock 53224987Sjonathan */ 54224987Sjonathanstruct proc; 55224987Sjonathanstruct sigio; 56224987Sjonathanstruct procdesc { 57224987Sjonathan /* 58224987Sjonathan * Basic process descriptor state: the process, a cache of its pid to 59224987Sjonathan * satisfy queries after the process exits, and process descriptor 60224987Sjonathan * refcount. 61224987Sjonathan */ 62224987Sjonathan struct proc *pd_proc; /* (t) Process. */ 63224987Sjonathan pid_t pd_pid; /* (c) Cached pid. */ 64224987Sjonathan u_int pd_refcount; /* (r) Reference count. */ 65224987Sjonathan 66224987Sjonathan /* 67224987Sjonathan * In-flight data and notification of events. 68224987Sjonathan */ 69224987Sjonathan int pd_flags; /* (p) PD_ flags. */ 70224987Sjonathan struct selinfo pd_selinfo; /* (p) Event notification. */ 71224987Sjonathan struct mtx pd_lock; /* Protect data + events. */ 72224987Sjonathan}; 73224987Sjonathan 74224987Sjonathan/* 75224987Sjonathan * Locking macros for the procdesc itself. 76224987Sjonathan */ 77224987Sjonathan#define PROCDESC_LOCK_DESTROY(pd) mtx_destroy(&(pd)->pd_lock) 78224987Sjonathan#define PROCDESC_LOCK_INIT(pd) mtx_init(&(pd)->pd_lock, "procdesc", NULL, \ 79224987Sjonathan MTX_DEF) 80224987Sjonathan#define PROCDESC_LOCK(pd) mtx_lock(&(pd)->pd_lock) 81224987Sjonathan#define PROCDESC_UNLOCK(pd) mtx_unlock(&(pd)->pd_lock) 82224987Sjonathan 83224987Sjonathan/* 84224987Sjonathan * Flags for the pd_flags field. 85224987Sjonathan */ 86224987Sjonathan#define PDF_CLOSED 0x00000001 /* Descriptor has closed. */ 87224987Sjonathan#define PDF_SELECTED 0x00000002 /* Issue selwakeup(). */ 88224987Sjonathan#define PDF_EXITED 0x00000004 /* Process exited. */ 89224987Sjonathan#define PDF_DAEMON 0x00000008 /* Don't exit when procdesc closes. */ 90224987Sjonathan 91224987Sjonathan/* 92224987Sjonathan * In-kernel interfaces to process descriptors. 93224987Sjonathan */ 94224987Sjonathanint procdesc_exit(struct proc *); 95255219Spjdint procdesc_find(struct thread *, int fd, cap_rights_t *, struct proc **); 96255219Spjdint kern_pdgetpid(struct thread *, int fd, cap_rights_t *, pid_t *pidp); 97224987Sjonathanvoid procdesc_new(struct proc *, int); 98224987Sjonathanvoid procdesc_finit(struct procdesc *, struct file *); 99224987Sjonathanpid_t procdesc_pid(struct file *); 100224987Sjonathanvoid procdesc_reap(struct proc *); 101224987Sjonathan 102224987Sjonathan#else /* !_KERNEL */ 103224987Sjonathan 104224987Sjonathan/* 105224987Sjonathan * Process descriptor system calls. 106224987Sjonathan */ 107224987Sjonathanstruct rusage; 108224987Sjonathanint pdfork(int *, int); 109224987Sjonathanint pdkill(int, int); 110224987Sjonathanint pdgetpid(int, pid_t *); 111224987Sjonathan 112224987Sjonathan#endif /* _KERNEL */ 113224987Sjonathan 114224987Sjonathan/* 115224987Sjonathan * Flags which can be passed to pdfork(2). 116224987Sjonathan */ 117224987Sjonathan#define PD_DAEMON 0x00000001 /* Don't exit when procdesc closes. */ 118224987Sjonathan 119224987Sjonathan#endif /* !_SYS_PROCDESC_H_ */ 120