Changeset b4cbef1 in mainline for uspace/lib


Ignore:
Timestamp:
2010-02-03T16:52:37Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3b3e776, eda925a
Parents:
472c09d
Message:

add minimal data size check into async_data_receive
introduce more convenience wrappers for common IPC patterns:

async_data_read_forward_fast
async_data_void
async_data_forward_fast

Location:
uspace/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/async.c

    r472c09d rb4cbef1  
    12871287}
    12881288
     1289/** Wrapper for forwarding any read request
     1290 *
     1291 *
     1292 */
     1293int async_data_read_forward_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
     1294    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
     1295{
     1296        ipc_callid_t callid;
     1297        if (!async_data_read_receive(&callid, NULL)) {
     1298                ipc_answer_0(callid, EINVAL);
     1299                return EINVAL;
     1300        }
     1301       
     1302        aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
     1303            dataptr);
     1304        if (msg == 0) {
     1305                ipc_answer_0(callid, EINVAL);
     1306                return EINVAL;
     1307        }
     1308       
     1309        int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
     1310            IPC_FF_ROUTE_FROM_ME);
     1311        if (retval != EOK) {
     1312                ipc_answer_0(callid, retval);
     1313                return retval;
     1314        }
     1315       
     1316        ipcarg_t rc;
     1317        async_wait_for(msg, &rc);
     1318       
     1319        return (int) rc;
     1320}
     1321
    12891322/** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
    12901323 *
    1291  * @param phoneid       Phone that will be used to contact the receiving side.
    1292  * @param src           Address of the beginning of the source buffer.
    1293  * @param size          Size of the source buffer.
    1294  *
    1295  * @return              Zero on success or a negative error code from errno.h.
     1324 * @param phoneid Phone that will be used to contact the receiving side.
     1325 * @param src     Address of the beginning of the source buffer.
     1326 * @param size    Size of the source buffer.
     1327 *
     1328 * @return Zero on success or a negative error code from errno.h.
     1329 *
    12961330 */
    12971331int async_data_write_start(int phoneid, const void *src, size_t size)
     
    13081342 * So far, this wrapper is to be used from within a connection fibril.
    13091343 *
    1310  * @param callid        Storage where the hash of the IPC_M_DATA_WRITE call will
    1311  *                      be stored.
    1312  * @param size          Storage where the suggested size will be stored. May be
    1313  *                      NULL
    1314  *
    1315  * @return              Non-zero on success, zero on failure.
     1344 * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
     1345 *               be stored.
     1346 * @param size   Storage where the suggested size will be stored. May be
     1347 *               NULL
     1348 *
     1349 * @return Non-zero on success, zero on failure.
     1350 *
    13161351 */
    13171352int async_data_write_receive(ipc_callid_t *callid, size_t *size)
     
    13201355       
    13211356        assert(callid);
    1322 
     1357       
    13231358        *callid = async_get_call(&data);
    13241359        if (IPC_GET_METHOD(data) != IPC_M_DATA_WRITE)
    13251360                return 0;
     1361       
    13261362        if (size)
    13271363                *size = (size_t) IPC_GET_ARG2(data);
     1364       
    13281365        return 1;
    13291366}
     
    13341371 * so that the user doesn't have to remember the meaning of each IPC argument.
    13351372 *
    1336  * @param callid        Hash of the IPC_M_DATA_WRITE call to answer.
    1337  * @param dst           Final destination address for the IPC_M_DATA_WRITE call.
    1338  * @param size          Final size for the IPC_M_DATA_WRITE call.
    1339  *
    1340  * @return              Zero on success or a value from @ref errno.h on failure.
     1373 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
     1374 * @param dst    Final destination address for the IPC_M_DATA_WRITE call.
     1375 * @param size   Final size for the IPC_M_DATA_WRITE call.
     1376 *
     1377 * @return Zero on success or a value from @ref errno.h on failure.
     1378 *
    13411379 */
    13421380int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
     
    13451383}
    13461384
    1347 /** Wrapper for receiving binary data via the async_data_write_*
     1385/** Wrapper for receiving binary data
    13481386 *
    13491387 * This wrapper only makes it more comfortable to use async_data_write_*
    1350  * functions to receive blobs.
     1388 * functions to receive binary data.
    13511389 *
    13521390 * @param data       Pointer to data pointer (which should be later disposed
    13531391 *                   by free()). If the operation fails, the pointer is not
    13541392 *                   touched.
     1393 * @param min_size   Minimum size (in bytes) of the data to receive.
    13551394 * @param max_size   Maximum size (in bytes) of the data to receive. 0 means
    13561395 *                   no limit.
     
    13621401 *
    13631402 */
    1364 int async_data_receive(void **data, const size_t max_size,
    1365     const size_t granularity, size_t *received)
     1403int async_data_receive(void **data, const size_t min_size,
     1404    const size_t max_size, const size_t granularity, size_t *received)
    13661405{
    13671406        ipc_callid_t callid;
     
    13721411        }
    13731412       
     1413        if (size < min_size) {
     1414                ipc_answer_0(callid, EINVAL);
     1415                return EINVAL;
     1416        }
     1417       
    13741418        if ((max_size > 0) && (size > max_size)) {
    13751419                ipc_answer_0(callid, EINVAL);
     
    14011445}
    14021446
    1403 /** Wrapper for receiving strings via the async_data_write_*
     1447/** Wrapper for receiving strings
    14041448 *
    14051449 * This wrapper only makes it more comfortable to use async_data_write_*
     
    14501494}
    14511495
     1496/** Wrapper for voiding any data that is about to be received
     1497 *
     1498 * This wrapper can be used to void any pending data
     1499 *
     1500 * @param retval Error value from @ref errno.h to be returned to the caller.
     1501 *
     1502 */
     1503void async_data_void(const int retval)
     1504{
     1505        ipc_callid_t callid;
     1506        async_data_write_receive(&callid, NULL);
     1507        ipc_answer_0(callid, retval);
     1508}
     1509
     1510/** Wrapper for forwarding any data that is about to be received
     1511 *
     1512 *
     1513 */
     1514int async_data_forward_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
     1515    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
     1516{
     1517        ipc_callid_t callid;
     1518        if (!async_data_write_receive(&callid, NULL)) {
     1519                ipc_answer_0(callid, EINVAL);
     1520                return EINVAL;
     1521        }
     1522       
     1523        aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
     1524            dataptr);
     1525        if (msg == 0) {
     1526                ipc_answer_0(callid, EINVAL);
     1527                return EINVAL;
     1528        }
     1529       
     1530        int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
     1531            IPC_FF_ROUTE_FROM_ME);
     1532        if (retval != EOK) {
     1533                ipc_answer_0(callid, retval);
     1534                return retval;
     1535        }
     1536       
     1537        ipcarg_t rc;
     1538        async_wait_for(msg, &rc);
     1539       
     1540        return (int) rc;
     1541}
     1542
    14521543/** @}
    14531544 */
  • uspace/lib/libc/include/async.h

    r472c09d rb4cbef1  
    277277extern int async_share_out_receive(ipc_callid_t *, size_t *, int *);
    278278extern int async_share_out_finalize(ipc_callid_t, void *);
     279
     280/*
     281 * User-friendly wrappers for async_data_read_forward_fast().
     282 */
     283#define async_data_read_forward_0_0(phoneid, method, answer) \
     284        async_data_read_forward_fast((phoneid), (method), 0, 0, 0, 0, NULL)
     285#define async_data_read_forward_0_1(phoneid, method, answer) \
     286        async_data_read_forward_fast((phoneid), (method), 0, 0, 0, 0, (answer))
     287#define async_data_read_forward_1_0(phoneid, method, arg1, answer) \
     288        async_data_read_forward_fast((phoneid), (method), (arg1), 0, 0, 0, NULL)
     289#define async_data_read_forward_1_1(phoneid, method, arg1, answer) \
     290        async_data_read_forward_fast((phoneid), (method), (arg1), 0, 0, 0, (answer))
     291#define async_data_read_forward_2_0(phoneid, method, arg1, arg2, answer) \
     292        async_data_read_forward_fast((phoneid), (method), (arg1), (arg2), 0, 0, NULL)
     293#define async_data_read_forward_2_1(phoneid, method, arg1, arg2, answer) \
     294        async_data_read_forward_fast((phoneid), (method), (arg1), (arg2), 0, 0, \
     295            (answer))
     296#define async_data_read_forward_3_0(phoneid, method, arg1, arg2, arg3, answer) \
     297        async_data_read_forward_fast((phoneid), (method), (arg1), (arg2), (arg3), 0, \
     298            NULL)
     299#define async_data_read_forward_3_1(phoneid, method, arg1, arg2, arg3, answer) \
     300        async_data_read_forward_fast((phoneid), (method), (arg1), (arg2), (arg3), 0, \
     301            (answer))
     302#define async_data_read_forward_4_0(phoneid, method, arg1, arg2, arg3, arg4, answer) \
     303        async_data_read_forward_fast((phoneid), (method), (arg1), (arg2), (arg3), \
     304            (arg4), NULL)
     305#define async_data_read_forward_4_1(phoneid, method, arg1, arg2, arg3, arg4, answer) \
     306        async_data_read_forward_fast((phoneid), (method), (arg1), (arg2), (arg3), \
     307            (arg4), (answer))
     308
    279309extern int async_data_read_start(int, void *, size_t);
    280310extern int async_data_read_receive(ipc_callid_t *, size_t *);
    281311extern int async_data_read_finalize(ipc_callid_t, const void *, size_t);
     312extern int async_data_read_forward_fast(int, ipcarg_t, ipcarg_t, ipcarg_t,
     313    ipcarg_t, ipcarg_t, ipc_call_t *);
     314
     315/*
     316 * User-friendly wrappers for async_data_forward_fast().
     317 */
     318#define async_data_forward_0_0(phoneid, method, answer) \
     319        async_data_forward_fast((phoneid), (method), 0, 0, 0, 0, NULL)
     320#define async_data_forward_0_1(phoneid, method, answer) \
     321        async_data_forward_fast((phoneid), (method), 0, 0, 0, 0, (answer))
     322#define async_data_forward_1_0(phoneid, method, arg1, answer) \
     323        async_data_forward_fast((phoneid), (method), (arg1), 0, 0, 0, NULL)
     324#define async_data_forward_1_1(phoneid, method, arg1, answer) \
     325        async_data_forward_fast((phoneid), (method), (arg1), 0, 0, 0, (answer))
     326#define async_data_forward_2_0(phoneid, method, arg1, arg2, answer) \
     327        async_data_forward_fast((phoneid), (method), (arg1), (arg2), 0, 0, NULL)
     328#define async_data_forward_2_1(phoneid, method, arg1, arg2, answer) \
     329        async_data_forward_fast((phoneid), (method), (arg1), (arg2), 0, 0, \
     330            (answer))
     331#define async_data_forward_3_0(phoneid, method, arg1, arg2, arg3, answer) \
     332        async_data_forward_fast((phoneid), (method), (arg1), (arg2), (arg3), 0, \
     333            NULL)
     334#define async_data_forward_3_1(phoneid, method, arg1, arg2, arg3, answer) \
     335        async_data_forward_fast((phoneid), (method), (arg1), (arg2), (arg3), 0, \
     336            (answer))
     337#define async_data_forward_4_0(phoneid, method, arg1, arg2, arg3, arg4, answer) \
     338        async_data_forward_fast((phoneid), (method), (arg1), (arg2), (arg3), \
     339            (arg4), NULL)
     340#define async_data_forward_4_1(phoneid, method, arg1, arg2, arg3, arg4, answer) \
     341        async_data_forward_fast((phoneid), (method), (arg1), (arg2), (arg3), \
     342            (arg4), (answer))
     343
    282344extern int async_data_write_start(int, const void *, size_t);
    283345extern int async_data_write_receive(ipc_callid_t *, size_t *);
    284346extern int async_data_write_finalize(ipc_callid_t, void *, size_t);
    285 
    286 extern int async_data_receive(void **, const size_t, const size_t, size_t *);
     347extern int async_data_receive(void **, const size_t, const size_t,
     348    const size_t, size_t *);
    287349extern int async_string_receive(char **, const size_t, size_t *);
     350extern void async_data_void(const int);
     351extern int async_data_forward_fast(int, ipcarg_t, ipcarg_t, ipcarg_t, ipcarg_t,
     352    ipcarg_t, ipc_call_t *);
    288353
    289354#endif
  • uspace/lib/libfs/libfs.c

    r472c09d rb4cbef1  
    161161        /* Accept the phone */
    162162        callid = async_get_call(&call);
    163         int mountee_phone = (int)IPC_GET_ARG1(call);
     163        int mountee_phone = (int) IPC_GET_ARG1(call);
    164164        if ((IPC_GET_METHOD(call) != IPC_M_CONNECTION_CLONE) ||
    165165            (mountee_phone < 0)) {
     
    172172        ipc_answer_0(callid, EOK);
    173173       
    174         res = async_data_write_receive(&callid, NULL);
    175         if (!res) {
    176                 ipc_hangup(mountee_phone);
    177                 ipc_answer_0(callid, EINVAL);
    178                 ipc_answer_0(rid, EINVAL);
    179                 return;
    180         }
    181        
    182174        fs_node_t *fn;
    183175        res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
    184176        if ((res != EOK) || (!fn)) {
    185177                ipc_hangup(mountee_phone);
    186                 ipc_answer_0(callid, combine_rc(res, ENOENT));
     178                async_data_void(combine_rc(res, ENOENT));
    187179                ipc_answer_0(rid, combine_rc(res, ENOENT));
    188180                return;
     
    192184                ipc_hangup(mountee_phone);
    193185                (void) ops->node_put(fn);
    194                 ipc_answer_0(callid, EBUSY);
     186                async_data_void(EBUSY);
    195187                ipc_answer_0(rid, EBUSY);
    196188                return;
     
    201193                ipc_hangup(mountee_phone);
    202194                (void) ops->node_put(fn);
    203                 ipc_answer_0(callid, rc);
     195                async_data_void(rc);
    204196                ipc_answer_0(rid, rc);
    205197                return;
     
    207199       
    208200        ipc_call_t answer;
    209         aid_t msg = async_send_1(mountee_phone, VFS_OUT_MOUNTED, mr_dev_handle,
     201        rc = async_data_forward_1_1(mountee_phone, VFS_OUT_MOUNTED, mr_dev_handle,
    210202            &answer);
    211         ipc_forward_fast(callid, mountee_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
    212         async_wait_for(msg, &rc);
    213203       
    214204        if (rc == EOK) {
Note: See TracChangeset for help on using the changeset viewer.