#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
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 */
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 */
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
+ 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 */
-#endif
#if CONFIG_FREEZE
uint32_t p_memstat_suspendedfootprint; /* footprint at time of suspensions */
#endif /* CONFIG_FREEZE */
/* 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_LRETURNWAIT 0x00200000 /* process is completing spawn/vfork-exec/fork */
#define P_LRAGE_VNODES 0x00400000
#define P_LREGISTER 0x00800000 /* thread start fns registered */
#define P_LVMRSRCOWNER 0x01000000 /* can handle the resource ownership of */
-#define P_LRETURNWAITER 0x02000000 /* thread is waiting on P_LRETURNWAIT being cleared */
#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
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 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 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);
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);
-void proc_set_return_wait(struct proc *);
-void proc_clear_return_wait(proc_t p, thread_t child_thread);
-void proc_wait_to_return(void);
+/* process iteration */
+
+#define ALLPROC_FOREACH(var) \
+ LIST_FOREACH((var), &allproc, p_list)
+
+#define ZOMBPROC_FOREACH(var) \
+ LIST_FOREACH((var), &zombproc, p_list)
-/* return 1 if process is forcing case-sensitive HFS+ access, 0 for default */
-extern int proc_is_forcing_hfs_case_sensitivity(proc_t);
+#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_ */