+
+/*
+ * DTrace wait for process execution
+ *
+ * This feature is using a list of entries, each entry containing a pointer
+ * on a process description. The description is provided by a client, and it
+ * contains the command we want to wait for along with a reserved space for
+ * the caught process id.
+ *
+ * Once an awaited process has been spawned, it will be suspended before
+ * notifying the client. Once the client has been back to userland, it's its
+ * duty to resume the task.
+ */
+
+LCK_MTX_DECLARE_ATTR(dtrace_procwaitfor_lock, &dtrace_lck_grp, &dtrace_lck_attr);
+
+typedef struct dtrace_proc_awaited_entry {
+ struct dtrace_procdesc *pdesc;
+ LIST_ENTRY(dtrace_proc_awaited_entry) entries;
+} dtrace_proc_awaited_entry_t;
+
+LIST_HEAD(listhead, dtrace_proc_awaited_entry) dtrace_proc_awaited_head
+ = LIST_HEAD_INITIALIZER(dtrace_proc_awaited_head);
+
+void (*dtrace_proc_waitfor_exec_ptr)(proc_t*) = NULL;
+
+static int
+dtrace_proc_get_execpath(proc_t *p, char *buffer, int *maxlen)
+{
+ int err = 0, vid = 0;
+ vnode_t tvp = NULLVP, nvp = NULLVP;
+
+ ASSERT(p);
+ ASSERT(buffer);
+ ASSERT(maxlen);
+
+ if ((tvp = p->p_textvp) == NULLVP)
+ return ESRCH;
+
+ vid = vnode_vid(tvp);
+ if ((err = vnode_getwithvid(tvp, vid)) != 0)
+ return err;
+
+ if ((err = vn_getpath_fsenter(tvp, buffer, maxlen)) != 0)
+ return err;
+ vnode_put(tvp);
+
+ if ((err = vnode_lookup(buffer, 0, &nvp, vfs_context_current())) != 0)
+ return err;
+ if (nvp != NULLVP)
+ vnode_put(nvp);
+
+ return 0;
+}
+
+
+static void
+dtrace_proc_exec_notification(proc_t *p) {
+ dtrace_proc_awaited_entry_t *entry, *tmp;
+ static char execpath[MAXPATHLEN];
+
+ ASSERT(p);
+ ASSERT(p->p_pid != -1);
+ ASSERT(current_task() != p->task);
+
+ lck_mtx_lock(&dtrace_procwaitfor_lock);
+
+ LIST_FOREACH_SAFE(entry, &dtrace_proc_awaited_head, entries, tmp) {
+ /* By default consider we're using p_comm. */
+ char *pname = p->p_comm;
+
+ /* Already matched with another process. */
+ if ((entry->pdesc->p_pid != -1))
+ continue;
+
+ /* p_comm is too short, use the execpath. */
+ if (entry->pdesc->p_name_length >= MAXCOMLEN) {
+ /*
+ * Retrieve the executable path. After the call, length contains
+ * the length of the string + 1.
+ */
+ int length = sizeof(execpath);
+ if (dtrace_proc_get_execpath(p, execpath, &length) != 0)
+ continue;
+ /* Move the cursor to the position after the last / */
+ pname = &execpath[length - 1];
+ while (pname != execpath && *pname != '/')
+ pname--;
+ pname = (*pname == '/') ? pname + 1 : pname;
+ }
+
+ if (!strcmp(entry->pdesc->p_name, pname)) {
+ entry->pdesc->p_pid = p->p_pid;
+ task_pidsuspend(p->task);
+ wakeup(entry);
+ }
+ }
+
+ lck_mtx_unlock(&dtrace_procwaitfor_lock);
+}
+
+int
+dtrace_proc_waitfor(dtrace_procdesc_t* pdesc) {
+ dtrace_proc_awaited_entry_t entry;
+ int res;
+
+ ASSERT(pdesc);
+ ASSERT(pdesc->p_name);
+
+ /*
+ * Never trust user input, compute the length of the process name and ensure the
+ * string is null terminated.
+ */
+ pdesc->p_name_length = (int) strnlen(pdesc->p_name, sizeof(pdesc->p_name));
+ if (pdesc->p_name_length >= (int) sizeof(pdesc->p_name))
+ return -1;
+
+ lck_mtx_lock(&dtrace_procwaitfor_lock);
+
+ /* Initialize and insert the entry, then install the hook. */
+ pdesc->p_pid = -1;
+ entry.pdesc = pdesc;
+ LIST_INSERT_HEAD(&dtrace_proc_awaited_head, &entry, entries);
+ dtrace_proc_waitfor_exec_ptr = &dtrace_proc_exec_notification;
+
+ /* Sleep until the process has been executed */
+ res = msleep(&entry, &dtrace_procwaitfor_lock, PCATCH, "dtrace_proc_waitfor", NULL);
+
+ /* Remove the entry and the hook if it is not needed anymore. */
+ LIST_REMOVE(&entry, entries);
+ if (LIST_EMPTY(&dtrace_proc_awaited_head))
+ dtrace_proc_waitfor_exec_ptr = NULL;
+
+ lck_mtx_unlock(&dtrace_procwaitfor_lock);
+
+ return res;
+}
+
+