/** Forward a received call to another destination - slow version. * * This function is the slow verision of the sys_ipc_forward_fast interface. * It can copy all five new arguments and the new interface and method from * the userspace. It naturally extends the functionality of the fast version. * For system methods, it additionally stores the new value of arg3 to ARG4. * For non-system methods, it additionally stores the new value of arg3, arg4 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively. * * @param callid Hash of the call to forward. * @param phoneid Phone handle to use for forwarding. * @param data Userspace address of the new IPC data. * @param mode Flags that specify mode of the forward operation. * * @return 0 on succes, otherwise an error code. * */ sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid, ipc_data_t *data, unsigned int mode) { ipc_data_t newdata; int rc = copy_from_uspace(&newdata.args, &data->args, sizeof(newdata.args)); if (rc != 0) return (sysarg_t) rc; return sys_ipc_forward_common(callid, phoneid, IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata), IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata), IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true); }
static void notification_received(ipc_callid_t callid, ipc_call_t *call) { switch (IPC_GET_IMETHOD(*call)) { case VFS_TASK_STATE_CHANGE: if (IPC_GET_ARG1(*call) == VFS_PASS_HANDLE) vfs_pass_handle( (task_id_t) MERGE_LOUP32(IPC_GET_ARG4(*call), IPC_GET_ARG5(*call)), call->in_task_id, (int) IPC_GET_ARG2(*call)); break; default: break; } }
static int request_preprocess(call_t *call, phone_t *phone) { phone_t *sender_phone; task_t *other_task_s; if (phone_get(IPC_GET_ARG5(call->data), &sender_phone) != EOK) return ENOENT; mutex_lock(&sender_phone->lock); if (sender_phone->state != IPC_PHONE_CONNECTED) { mutex_unlock(&sender_phone->lock); return EINVAL; } other_task_s = sender_phone->callee->task; mutex_unlock(&sender_phone->lock); /* Remember the third party task hash. */ IPC_SET_ARG5(call->data, (sysarg_t) other_task_s); return EOK; }
#ifdef __32_BITS__ printf("%10p ", call); #endif #ifdef __64_BITS__ printf("%18p ", call); #endif spinlock_lock(&call->forget_lock); printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun " %-7x", IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data), IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), call->flags); if (call->forget) { printf(" ? (call forgotten)\n"); } else { printf(" %" PRIu64 " (%s)\n", call->sender->taskid, call->sender->name); } spinlock_unlock(&call->forget_lock); } } /** List answerbox contents. *
/** Perform a path lookup. * * @param path Path to be resolved; it must be a NULL-terminated * string. * @param lflag Flags to be used during lookup. * @param result Empty structure where the lookup result will be stored. * Can be NULL. * @param altroot If non-empty, will be used instead of rootfs as the root * of the whole VFS tree. * * @return EOK on success or an error code from errno.h. * */ int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result, vfs_pair_t *altroot, ...) { vfs_pair_t *root; if (altroot) root = altroot; else root = &rootfs; if (!root->fs_handle) return ENOENT; size_t len; path = canonify(path, &len); if (!path) return EINVAL; fs_index_t index = 0; if (lflag & L_LINK) { va_list ap; va_start(ap, altroot); index = va_arg(ap, fs_index_t); va_end(ap); } fibril_mutex_lock(&plb_mutex); plb_entry_t entry; link_initialize(&entry.plb_link); entry.len = len; size_t first; /* the first free index */ size_t last; /* the last free index */ if (list_empty(&plb_entries)) { first = 0; last = PLB_SIZE - 1; } else { plb_entry_t *oldest = list_get_instance( list_first(&plb_entries), plb_entry_t, plb_link); plb_entry_t *newest = list_get_instance( list_last(&plb_entries), plb_entry_t, plb_link); first = (newest->index + newest->len) % PLB_SIZE; last = (oldest->index - 1) % PLB_SIZE; } if (first <= last) { if ((last - first) + 1 < len) { /* * The buffer cannot absorb the path. */ fibril_mutex_unlock(&plb_mutex); return ELIMIT; } } else { if (PLB_SIZE - ((first - last) + 1) < len) { /* * The buffer cannot absorb the path. */ fibril_mutex_unlock(&plb_mutex); return ELIMIT; } } /* * We know the first free index in PLB and we also know that there is * enough space in the buffer to hold our path. */ entry.index = first; entry.len = len; /* * Claim PLB space by inserting the entry into the PLB entry ring * buffer. */ list_append(&entry.plb_link, &plb_entries); fibril_mutex_unlock(&plb_mutex); /* * Copy the path into PLB. */ size_t cnt1 = min(len, (PLB_SIZE - first) + 1); size_t cnt2 = len - cnt1; memcpy(&plb[first], path, cnt1); memcpy(plb, &path[cnt1], cnt2); ipc_call_t answer; async_exch_t *exch = vfs_exchange_grab(root->fs_handle); aid_t req = async_send_5(exch, VFS_OUT_LOOKUP, (sysarg_t) first, (sysarg_t) (first + len - 1) % PLB_SIZE, (sysarg_t) root->service_id, (sysarg_t) lflag, (sysarg_t) index, &answer); sysarg_t rc; async_wait_for(req, &rc); vfs_exchange_release(exch); fibril_mutex_lock(&plb_mutex); list_remove(&entry.plb_link); /* * Erasing the path from PLB will come handy for debugging purposes. */ memset(&plb[first], 0, cnt1); memset(plb, 0, cnt2); fibril_mutex_unlock(&plb_mutex); if ((int) rc < EOK) return (int) rc; if (!result) return EOK; result->triplet.fs_handle = (fs_handle_t) rc; result->triplet.service_id = (service_id_t) IPC_GET_ARG1(answer); result->triplet.index = (fs_index_t) IPC_GET_ARG2(answer); result->size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(answer), IPC_GET_ARG4(answer)); result->lnkcnt = (unsigned int) IPC_GET_ARG5(answer); if (lflag & L_FILE) result->type = VFS_NODE_FILE; else if (lflag & L_DIRECTORY) result->type = VFS_NODE_DIRECTORY; else result->type = VFS_NODE_UNKNOWN; return EOK; }
/** Forward a received call to another destination * * Common code for both the fast and the slow version. * * @param callid Hash of the call to forward. * @param phoneid Phone handle to use for forwarding. * @param imethod New interface and method to use for the forwarded call. * @param arg1 New value of the first argument for the forwarded call. * @param arg2 New value of the second argument for the forwarded call. * @param arg3 New value of the third argument for the forwarded call. * @param arg4 New value of the fourth argument for the forwarded call. * @param arg5 New value of the fifth argument for the forwarded call. * @param mode Flags that specify mode of the forward operation. * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise * the function considers only the fast version arguments: * i.e. arg1 and arg2. * * @return 0 on succes, otherwise an error code. * * Warning: Make sure that ARG5 is not rewritten for certain system IPC * */ static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow) { call_t *call = get_call(callid); phone_t *phone; bool need_old = answer_need_old(call); bool after_forward = false; ipc_data_t old; int rc; if (!call) return ENOENT; if (need_old) old = call->data; if (phone_get(phoneid, &phone) != EOK) { rc = ENOENT; goto error; } if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) { rc = EPERM; goto error; } call->flags |= IPC_CALL_FORWARDED; /* * User space is not allowed to change interface and method of system * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by * means of imethod, arg1, arg2 and arg3. * If the interface and method is immutable, don't change anything. */ if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) { if (method_is_system(IPC_GET_IMETHOD(call->data))) { if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME) phone_dealloc(IPC_GET_ARG5(call->data)); IPC_SET_ARG1(call->data, imethod); IPC_SET_ARG2(call->data, arg1); IPC_SET_ARG3(call->data, arg2); if (slow) IPC_SET_ARG4(call->data, arg3); /* * For system methods we deliberately don't * overwrite ARG5. */ } else { IPC_SET_IMETHOD(call->data, imethod); IPC_SET_ARG1(call->data, arg1); IPC_SET_ARG2(call->data, arg2); if (slow) { IPC_SET_ARG3(call->data, arg3); IPC_SET_ARG4(call->data, arg4); IPC_SET_ARG5(call->data, arg5); } } } rc = ipc_forward(call, phone, &TASK->answerbox, mode); if (rc != EOK) { after_forward = true; goto error; } return EOK; error: IPC_SET_RETVAL(call->data, EFORWARD); (void) answer_preprocess(call, need_old ? &old : NULL); if (after_forward) _ipc_answer_free_call(call, false); else ipc_answer(&TASK->answerbox, call); return rc; }
int main(int argc, char **argv) { printf("%s: HelenOS IPC Naming Service\n", NAME); int rc = service_init(); if (rc != EOK) return rc; rc = clonable_init(); if (rc != EOK) return rc; rc = task_init(); if (rc != EOK) return rc; printf("%s: Accepting connections\n", NAME); while (true) { process_pending_conn(); process_pending_wait(); ipc_call_t call; ipc_callid_t callid = ipc_wait_for_call(&call); task_id_t id; sysarg_t retval; switch (IPC_GET_IMETHOD(call)) { case IPC_M_PHONE_HUNGUP: retval = ns_task_disconnect(&call); break; case IPC_M_CONNECT_TO_ME: /* * Server requests service registration. */ if (service_clonable(IPC_GET_ARG1(call))) { register_clonable(IPC_GET_ARG1(call), IPC_GET_ARG5(call), &call, callid); continue; } else { retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG5(call), &call); } break; case IPC_M_CONNECT_ME_TO: /* * Client requests to be connected to a service. */ if (service_clonable(IPC_GET_ARG1(call))) { connect_to_clonable(IPC_GET_ARG1(call), &call, callid); continue; } else { connect_to_service(IPC_GET_ARG1(call), &call, callid); continue; } break; case NS_PING: retval = EOK; break; case NS_TASK_WAIT: id = (task_id_t) MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call)); wait_for_task(id, &call, callid); continue; case NS_ID_INTRO: retval = ns_task_id_intro(&call); break; case NS_RETVAL: retval = ns_task_retval(&call); break; default: retval = ENOENT; break; } if (!(callid & IPC_CALLID_NOTIFICATION)) ipc_answer_0(callid, retval); } /* Not reached */ return 0; }