#include <libkern/OSAtomic.h>
#include <sys/proc.h>
+#include <mach/resource_monitors.h> // command/proc_name_t
+
+
__BEGIN_DECLS
#include <kern/locks.h>
#if PSYNCH
* PL = Process Lock
* PGL = Process Group Lock
* PFDL = Process File Desc Lock
+ * PUCL = Process User Credentials Lock
* PSL = Process Spin Lock
* PPL = Parent Process Lock (planed for later usage)
* LL = List Lock
*/
struct label;
-/*
- * Added by SPARTA, Inc.
- */
-/*
- * Login context.
- */
-struct lctx {
- LIST_ENTRY(lctx) lc_list; /* List of all login contexts. */
- LIST_HEAD(, proc) lc_members; /* Pointer to lc members. */
- int lc_mc; /* Member Count. */
- pid_t lc_id; /* Login context ID. */
- lck_mtx_t lc_mtx; /* Mutex to protect members */
-
- struct label *lc_label; /* Login context MAC label. */
-};
-
/*
* One structure allocated per session.
*/
LIST_ENTRY(proc) p_hash; /* Hash chain. (LL)*/
TAILQ_HEAD( ,eventqelt) p_evlist; /* (PL) */
+#if CONFIG_PERSONAS
+ struct persona *p_persona;
+ LIST_ENTRY(proc) p_persona_list;
+#endif
+
lck_mtx_t p_fdmlock; /* proc lock to protect fdesc */
+ lck_mtx_t p_ucred_mlock; /* mutex lock to protect p_ucred */
/* substructures: */
- kauth_cred_t p_ucred; /* Process owner's identity. (PL) */
+ kauth_cred_t p_ucred; /* Process owner's identity. (PUCL) */
struct filedesc *p_fd; /* Ptr to open files structure. (PFDL) */
struct pstats *p_stats; /* Accounting/statistics (PL). */
struct plimit *p_limit; /* Process limits.(PL) */
int p_mac_enforce; /* MAC policy enforcement control */
#endif
- char p_comm[MAXCOMLEN+1];
- char p_name[(2*MAXCOMLEN)+1]; /* PL */
+ // types currently in sys/param.h
+ command_t p_comm;
+ proc_name_t p_name; /* can be changed by the process */
- struct pgrp *p_pgrp; /* Pointer to process group. (LL) */
+
+ struct pgrp *p_pgrp; /* Pointer to process group. (LL) */
uint32_t p_csflags; /* flags for codesign (PL) */
uint32_t p_pcaction; /* action for process control on starvation */
uint8_t p_uuid[16]; /* from LC_UUID load command */
u_short p_acflag; /* Accounting flags. */
volatile u_short p_vfs_iopolicy; /* VFS iopolicy flags. (atomic bit ops) */
- struct lctx *p_lctx; /* Pointer to login context. */
- LIST_ENTRY(proc) p_lclist; /* List of processes in lctx. */
user_addr_t p_threadstart; /* pthread start fn */
user_addr_t p_wqthread; /* pthread workqueue fn */
int p_pthsize; /* pthread size */
uint32_t p_pth_tsd_offset; /* offset from pthread_t to TSD for new threads */
- user_addr_t p_targconc; /* target concurrency ptr */
user_addr_t p_stack_addr_hint; /* stack allocation hint for wq threads */
void * p_wqptr; /* workq ptr */
- int p_wqsize; /* allocated size */
- boolean_t p_wqiniting; /* semaphore to serialze wq_open */
- lck_spin_t p_wqlock; /* lock to protect work queue */
+ struct kqueue * p_wqkqueue; /* private workq kqueue */
+
struct timeval p_start; /* starting time */
void * p_rcall;
int p_ractive;
uint32_t p_memstat_dirty; /* dirty state */
uint64_t p_memstat_userdata; /* user state */
uint64_t p_memstat_idledeadline; /* time at which process became clean */
-#if CONFIG_JETSAM
- int32_t p_memstat_memlimit; /* cached memory limit */
-#endif
+ uint64_t p_memstat_idle_start; /* abstime process transitions into the idle band */
+ uint64_t p_memstat_idle_delta; /* abstime delta spent in idle band */
+ int32_t p_memstat_memlimit; /* cached memory limit, toggles between active and inactive limits */
+ int32_t p_memstat_memlimit_active; /* memory limit enforced when process is in active jetsam state */
+ int32_t p_memstat_memlimit_inactive; /* memory limit enforced when process is in inactive jetsam state */
#if CONFIG_FREEZE
uint32_t p_memstat_suspendedfootprint; /* footprint at time of suspensions */
#endif /* CONFIG_FREEZE */
#endif /* CONFIG_MEMORYSTATUS */
+
+ /* cached proc-specific data required for corpse inspection */
+ pid_t p_responsible_pid; /* pid resonsible for this process */
+
+ struct os_reason *p_exit_reason;
};
#define PGRPID_DEAD 0xdeaddead
#define P_LIST_INPGRP 0x00020000 /* process is in pgrp */
#define P_LIST_PGRPTRANS 0x00040000 /* pgrp is getting replaced */
#define P_LIST_PGRPTRWAIT 0x00080000 /* wait for pgrp replacement */
-#define P_LIST_EXITCOUNT 0x00100000 /* counted for process exit */
+#define P_LIST_EXITCOUNT 0x00100000 /* counted for process exit */
+#define P_LIST_REFWAIT 0x00200000 /* wait to take a ref */
/* local flags */
#define P_LLIMWAIT 0x00040000
#define P_LWAITED 0x00080000
#define P_LINSIGNAL 0x00100000
-#define P_UNUSED 0x00200000 /* Unused */
#define P_LRAGE_VNODES 0x00400000
#define P_LREGISTER 0x00800000 /* thread start fns registered */
#define P_LVMRSRCOWNER 0x01000000 /* can handle the resource ownership of */
-/* old P_LPTERMINATE 0x02000000 */
#define P_LTERM_DECRYPTFAIL 0x04000000 /* process terminating due to key failure to decrypt */
#define P_LTERM_JETSAM 0x08000000 /* process is being jetsam'd */
#define P_JETSAM_VMPAGESHORTAGE 0x00000000 /* jetsam: lowest jetsam priority proc, killed due to vm page shortage */
#define PROC_SETACTION_STATE(p) (p->p_pcaction = (PROC_CONTROL_STATE(p) | (PROC_CONTROL_STATE(p) << 16)))
#define PROC_RESETACTION_STATE(p) (p->p_pcaction = PROC_CONTROL_STATE(p))
+/* Process exit reason macros */
+#define PROC_HAS_EXITREASON(p) (p->p_exit_reason != OS_REASON_NULL)
+#define PROC_EXITREASON_FLAGS(p) p->p_exit_reason->osr_flags
+
/* additional process flags */
#define P_LADVLOCK 0x01
+#define P_LXBKIDLEINPROG 0x02
/* p_vfs_iopolicy flags */
#define P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY 0x0001
-/* defns for proc_iterate */
-#define PROC_ALLPROCLIST 1 /* walk the allproc list (procs not exited yet) */
-#define PROC_ZOMBPROCLIST 2 /* walk the zombie list */
-#define PROC_NOWAITTRANS 4 /* do not wait for transitions (checkdirs only) */
-
-/* defns for pgrp_iterate */
-#define PGRP_DROPREF 1
-#define PGRP_BLOCKITERATE 2
-
-/* return values of the proc iteration callback routine */
-#define PROC_RETURNED 0
-#define PROC_RETURNED_DONE 1
-#define PROC_CLAIMED 2
-#define PROC_CLAIMED_DONE 3
-
/* process creation arguments */
#define PROC_CREATE_FORK 0 /* independent child (running) */
#define PROC_CREATE_SPAWN 1 /* independent child (suspended) */
#define PROC_CREATE_VFORK 2 /* child borrows context */
-
/* LP64 version of extern_proc. all pointers
* grow when we're dealing with a 64-bit process.
* WARNING - keep in sync with extern_proc
#ifdef KERNEL
#include <sys/time.h> /* user_timeval, user_itimerval */
+/* This packing breaks symmetry with userspace side (struct extern_proc
+ * of proc.h) for the ARMV7K ABI where 64-bit types are 64-bit aligned
+ */
#pragma pack(4)
struct user32_extern_proc {
union {
#define SESS_LEADER(p, sessp) ((sessp)->s_leader == (p))
-/* Lock and unlock a login context. */
-#define LCTX_LOCK(lc) lck_mtx_lock(&(lc)->lc_mtx)
-#define LCTX_UNLOCK(lc) lck_mtx_unlock(&(lc)->lc_mtx)
-#define LCTX_LOCKED(lc)
-#define LCTX_LOCK_ASSERT(lc, type)
-#define ALLLCTX_LOCK lck_mtx_lock(&alllctx_lock)
-#define ALLLCTX_UNLOCK lck_mtx_unlock(&alllctx_lock)
-extern lck_mtx_t alllctx_lock;
-extern lck_grp_t * lctx_lck_grp;
-extern lck_grp_attr_t * lctx_lck_grp_attr;
-extern lck_attr_t * lctx_lck_attr;
-
#define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
extern u_long pidhash;
#if CONFIG_FINE_LOCK_GROUPS
extern lck_grp_t * proc_mlock_grp;
extern lck_grp_t * proc_fdmlock_grp;
+extern lck_grp_t * proc_ucred_mlock_grp;
extern lck_grp_t * proc_slock_grp;
#endif
extern lck_grp_attr_t * proc_lck_grp_attr;
LIST_HEAD(proclist, proc);
extern struct proclist allproc; /* List of all processes. */
extern struct proclist zombproc; /* List of zombie processes. */
+
extern struct proc *initproc;
extern void procinit(void);
extern void proc_lock(struct proc *);
extern void proc_fdlock_spin(struct proc *);
extern void proc_fdunlock(struct proc *);
extern void proc_fdlock_assert(proc_t p, int assertflags);
+extern void proc_ucred_lock(struct proc *);
+extern void proc_ucred_unlock(struct proc *);
__private_extern__ int proc_core_name(const char *name, uid_t uid, pid_t pid,
char *cr_name, size_t cr_name_len);
extern int isinferior(struct proc *, struct proc *);
__private_extern__ void proc_drop_zombref(struct proc * p); /* Find zombie by id. */
-extern struct lctx *lcfind(pid_t); /* Find a login context by id */
-extern struct lctx *lccreate(void); /* Create a new login context */
-
extern int chgproccnt(uid_t uid, int diff);
-extern void enterlctx(struct proc *p, struct lctx *l, int create);
extern void pinsertchild(struct proc *parent, struct proc *child);
extern int enterpgrp(struct proc *p, pid_t pgid, int mksess);
extern void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
extern int inferior(struct proc *p);
extern int leavepgrp(struct proc *p);
-extern void leavelctx(struct proc *p);
extern void resetpriority(struct proc *);
extern void setrunnable(struct proc *);
extern void setrunqueue(struct proc *);
extern void vfork_return(struct proc *child, int32_t *retval, int rval);
extern int exit1(struct proc *, int, int *);
extern int exit1_internal(struct proc *, int, int *, boolean_t, boolean_t, int);
-extern int fork1(proc_t, thread_t *, int, coalition_t);
+extern int exit_with_reason(struct proc *, int, int *, boolean_t, boolean_t, int, struct os_reason *);
+extern int fork1(proc_t, thread_t *, int, coalition_t *);
extern void vfork_exit_internal(struct proc *p, int rv, int forced);
extern void proc_reparentlocked(struct proc *child, struct proc * newparent, int cansignal, int locked);
-extern int pgrp_iterate(struct pgrp * pgrp, int flags, int (*callout)(proc_t , void *), void *arg, int (*filterfn)(proc_t , void *), void *filterarg);
-extern int proc_iterate(int flags, int (*callout)(proc_t , void *), void *arg, int (*filterfn)(proc_t , void *), void *filterarg);
-extern int proc_rebootscan(int (*callout)(proc_t , void *), void *arg, int (*filterfn)(proc_t , void *), void *filterarg);
-extern int proc_childrenwalk(proc_t p, int (*callout)(proc_t , void *), void *arg);
+
extern proc_t proc_findinternal(int pid, int locked);
extern proc_t proc_findthread(thread_t thread);
extern void proc_refdrain(proc_t);
+extern proc_t proc_refdrain_with_refwait(proc_t p, boolean_t get_ref_and_allow_wait);
+extern void proc_refwake(proc_t p);
extern void proc_childdrainlocked(proc_t);
extern void proc_childdrainstart(proc_t);
extern void proc_childdrainend(proc_t);
extern int proc_parentdropref(proc_t, int);
int itimerfix(struct timeval *tv);
int itimerdecr(struct proc * p, struct itimerval *itp, int usec);
+void proc_free_realitimer(proc_t proc);
+int timespec_is_valid(const struct timespec *);
void proc_signalstart(struct proc *, int locked);
void proc_signalend(struct proc *, int locked);
int proc_transstart(struct proc *, int locked, int non_blocking);
struct proc *proc_ref_locked(struct proc * p);
void proc_knote(struct proc * p, long hint);
void proc_knote_drain(struct proc *p);
-void workqueue_init_lock(proc_t p);
-void workqueue_destroy_lock(proc_t p);
void proc_setregister(proc_t p);
void proc_resetregister(proc_t p);
/* returns the first thread_t in the process, or NULL XXX for NFS, DO NOT USE */
#endif /* PSYNCH */
struct uthread * current_uthread(void);
-/* return 1 if process is forcing case-sensitive HFS+ access, 0 for default */
-extern int proc_is_forcing_hfs_case_sensitivity(proc_t);
+/* process iteration */
+
+#define ALLPROC_FOREACH(var) \
+ LIST_FOREACH((var), &allproc, p_list)
+
+#define ZOMBPROC_FOREACH(var) \
+ LIST_FOREACH((var), &zombproc, p_list)
+
+#define PGMEMBERS_FOREACH(group, var) \
+ LIST_FOREACH((var), &((struct pgrp *)(group))->pg_members, p_pglist)
+
+#define PCHILDREN_FOREACH(parent, var) \
+ LIST_FOREACH((var), &(((struct proc *)(parent))->p_children), p_sibling)
+
+typedef int (*proc_iterate_fn_t)(proc_t, void *);
+
+/*
+ * These are the only valid return values of `callout` functions provided to
+ * process iterators.
+ *
+ * CLAIMED returns expect the caller to call proc_rele on the proc. DONE
+ * returns stop iterating processes early.
+ */
+#define PROC_RETURNED (0)
+#define PROC_RETURNED_DONE (1)
+#define PROC_CLAIMED (2)
+#define PROC_CLAIMED_DONE (3)
+
+/*
+ * pgrp_iterate walks the provided process group, calling `filterfn` with
+ * `filterarg` for each process. For processes where `filterfn` returned
+ * non-zero, `callout` is called with `arg`. If `PGRP_DROPREF` is supplied in
+ * `flags`, a reference will be dropped from the process group after obtaining
+ * the list of processes to call `callout` on.
+ *
+ * `PGMEMBERS_FOREACH` might also be used under the pgrp_lock to achieve a
+ * similar effect.
+ */
+#define PGRP_DROPREF (1)
+
+extern int pgrp_iterate(struct pgrp *pgrp, unsigned int flags, proc_iterate_fn_t callout, void *arg, proc_iterate_fn_t filterfn, void *filterarg);
+
+/*
+ * proc_iterate walks the `allproc` and/or `zombproc` lists, calling `filterfn`
+ * with `filterarg` for each process. For processes where `filterfn` returned
+ * non-zero, `callout` is called with `arg`. If the `PROC_NOWAITTRANS` flag is
+ * set, this function waits for transitions.
+ *
+ * `ALLPROC_FOREACH` or `ZOMBPROC_FOREACH` might also be used under the
+ * `proc_list_lock` to achieve a similar effect.
+ */
+#define PROC_ALLPROCLIST (1U << 0) /* walk the allproc list (processes not yet exited) */
+#define PROC_ZOMBPROCLIST (1U << 1) /* walk the zombie list */
+#define PROC_NOWAITTRANS (1U << 2) /* do not wait for transitions (checkdirs only) */
+
+extern int proc_iterate(unsigned int flags, proc_iterate_fn_t callout, void *arg, proc_iterate_fn_t filterfn, void *filterarg);
+
+/*
+ * proc_childrenwalk walks the children of process `p`, calling `callout` for
+ * each one.
+ *
+ * `PCHILDREN_FOREACH` might also be used under the `proc_list_lock` to achieve
+ * a similar effect.
+ */
+extern int proc_childrenwalk(proc_t p, proc_iterate_fn_t callout, void *arg);
+
+/*
+ * proc_rebootscan should only be used by kern_shutdown.c
+ */
+extern void proc_rebootscan(proc_iterate_fn_t callout, void *arg, proc_iterate_fn_t filterfn, void *filterarg);
pid_t dtrace_proc_selfpid(void);
pid_t dtrace_proc_selfppid(void);
uid_t dtrace_proc_selfruid(void);
+
#endif /* !_SYS_PROC_INTERNAL_H_ */