Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/loader.c

    r0fe52ef r6e84dc3  
    3535#include <ipc/loader.h>
    3636#include <ipc/services.h>
    37 #include <ns.h>
     37#include <ipc/ns.h>
    3838#include <libc.h>
    3939#include <task.h>
     
    4444#include <vfs/vfs.h>
    4545#include <loader/loader.h>
    46 #include "private/loader.h"
    4746
    4847/** Connect to a new program loader.
     
    6463loader_t *loader_connect(void)
    6564{
     65        int phone_id = service_connect_blocking(SERVICE_LOAD, 0, 0);
     66        if (phone_id < 0)
     67                return NULL;
     68       
    6669        loader_t *ldr = malloc(sizeof(loader_t));
    6770        if (ldr == NULL)
    6871                return NULL;
    6972       
    70         async_sess_t *sess =
    71             service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOAD, 0, 0);
    72         if (sess == NULL) {
    73                 free(ldr);
    74                 return NULL;
    75         }
    76        
    77         ldr->sess = sess;
     73        ldr->phone_id = phone_id;
    7874        return ldr;
    7975}
     
    9288{
    9389        /* Get task ID. */
    94         async_exch_t *exch = async_exchange_begin(ldr->sess);
    95        
    96         ipc_call_t answer;
    97         aid_t req = async_send_0(exch, LOADER_GET_TASKID, &answer);
    98         sysarg_t rc = async_data_read_start(exch, task_id, sizeof(task_id_t));
    99        
    100         async_exchange_end(exch);
    101        
    102         if (rc != EOK) {
    103                 async_wait_for(req, NULL);
    104                 return (int) rc;
    105         }
    106        
    107         async_wait_for(req, &rc);
    108         return (int) rc;
     90        ipc_call_t answer;
     91        aid_t req = async_send_0(ldr->phone_id, LOADER_GET_TASKID, &answer);
     92        int rc = async_data_read_start(ldr->phone_id, task_id, sizeof(task_id_t));
     93        if (rc != EOK) {
     94                async_wait_for(req, NULL);
     95                return rc;
     96        }
     97       
     98        sysarg_t retval;
     99        async_wait_for(req, &retval);
     100        return (int) retval;
    109101}
    110102
     
    120112int loader_set_cwd(loader_t *ldr)
    121113{
    122         char *cwd = (char *) malloc(MAX_PATH_LEN + 1);
     114        char *cwd;
     115        size_t len;
     116
     117        cwd = (char *) malloc(MAX_PATH_LEN + 1);
    123118        if (!cwd)
    124119                return ENOMEM;
    125        
    126120        if (!getcwd(cwd, MAX_PATH_LEN + 1))
    127                 str_cpy(cwd, MAX_PATH_LEN + 1, "/");
    128        
    129         size_t len = str_length(cwd);
    130        
    131         async_exch_t *exch = async_exchange_begin(ldr->sess);
    132        
    133         ipc_call_t answer;
    134         aid_t req = async_send_0(exch, LOADER_SET_CWD, &answer);
    135         sysarg_t rc = async_data_write_start(exch, cwd, len);
    136        
    137         async_exchange_end(exch);
     121                str_cpy(cwd, MAX_PATH_LEN + 1, "/");
     122        len = str_length(cwd);
     123       
     124        ipc_call_t answer;
     125        aid_t req = async_send_0(ldr->phone_id, LOADER_SET_CWD, &answer);
     126        int rc = async_data_write_start(ldr->phone_id, cwd, len);
    138127        free(cwd);
    139        
    140         if (rc != EOK) {
    141                 async_wait_for(req, NULL);
    142                 return (int) rc;
    143         }
    144        
    145         async_wait_for(req, &rc);
    146         return (int) rc;
     128        if (rc != EOK) {
     129                async_wait_for(req, NULL);
     130                return rc;
     131        }
     132       
     133        sysarg_t retval;
     134        async_wait_for(req, &retval);
     135        return (int) retval;
    147136}
    148137
     
    164153        char *pa = absolutize(path, &pa_len);
    165154        if (!pa)
    166                 return ENOMEM;
     155                return 0;
    167156       
    168157        /* Send program pathname */
    169         async_exch_t *exch = async_exchange_begin(ldr->sess);
    170        
    171         ipc_call_t answer;
    172         aid_t req = async_send_0(exch, LOADER_SET_PATHNAME, &answer);
    173         sysarg_t rc = async_data_write_start(exch, (void *) pa, pa_len);
    174        
    175         async_exchange_end(exch);
     158        ipc_call_t answer;
     159        aid_t req = async_send_0(ldr->phone_id, LOADER_SET_PATHNAME, &answer);
     160        int rc = async_data_write_start(ldr->phone_id, (void *) pa, pa_len);
     161        if (rc != EOK) {
     162                free(pa);
     163                async_wait_for(req, NULL);
     164                return rc;
     165        }
     166       
    176167        free(pa);
    177168       
    178         if (rc != EOK) {
    179                 async_wait_for(req, NULL);
    180                 return (int) rc;
    181         }
    182        
    183         async_wait_for(req, &rc);
    184         return (int) rc;
     169        sysarg_t retval;
     170        async_wait_for(req, &retval);
     171        return (int) retval;
    185172}
    186173
     
    225212       
    226213        /* Send serialized arguments to the loader */
    227         async_exch_t *exch = async_exchange_begin(ldr->sess);
    228        
    229         ipc_call_t answer;
    230         aid_t req = async_send_0(exch, LOADER_SET_ARGS, &answer);
    231         sysarg_t rc = async_data_write_start(exch, (void *) arg_buf,
    232             buffer_size);
    233        
    234         async_exchange_end(exch);
     214        ipc_call_t answer;
     215        aid_t req = async_send_0(ldr->phone_id, LOADER_SET_ARGS, &answer);
     216        sysarg_t rc = async_data_write_start(ldr->phone_id, (void *) arg_buf, buffer_size);
     217        if (rc != EOK) {
     218                async_wait_for(req, NULL);
     219                return rc;
     220        }
     221       
     222        async_wait_for(req, &rc);
     223        if (rc != EOK)
     224                return rc;
     225       
     226        /* Free temporary buffer */
    235227        free(arg_buf);
    236228       
    237         if (rc != EOK) {
    238                 async_wait_for(req, NULL);
    239                 return (int) rc;
    240         }
    241        
    242         async_wait_for(req, &rc);
    243         return (int) rc;
     229        return EOK;
    244230}
    245231
     
    256242 *
    257243 */
    258 int loader_set_files(loader_t *ldr, int * const files[])
    259 {
     244int loader_set_files(loader_t *ldr, fdi_node_t *const files[])
     245{
     246        /*
     247         * Serialize the arguments into a single array. First
     248         * compute size of the buffer needed.
     249         */
     250        fdi_node_t *const *ap = files;
     251        size_t count = 0;
     252        while (*ap != NULL) {
     253                count++;
     254                ap++;
     255        }
     256       
     257        fdi_node_t *files_buf;
     258        files_buf = (fdi_node_t *) malloc(count * sizeof(fdi_node_t));
     259        if (files_buf == NULL)
     260                return ENOMEM;
     261       
     262        /* Fill the buffer */
     263        size_t i;
     264        for (i = 0; i < count; i++)
     265                files_buf[i] = *files[i];
     266       
    260267        /* Send serialized files to the loader */
    261         async_exch_t *exch = async_exchange_begin(ldr->sess);
    262         async_exch_t *vfs_exch = vfs_exchange_begin();
    263        
    264         int i;
    265         for (i = 0; files[i]; i++);
    266 
    267         ipc_call_t answer;
    268         aid_t req = async_send_1(exch, LOADER_SET_FILES, i, &answer);
    269 
    270         sysarg_t rc = EOK;
    271        
    272         for (i = 0; files[i]; i++) {
    273                 rc = async_state_change_start(exch, VFS_PASS_HANDLE, *files[i],
    274                     0, vfs_exch);
    275                 if (rc != EOK)
    276                         break;
    277         }
    278        
    279         vfs_exchange_end(vfs_exch);
    280         async_exchange_end(exch);
    281 
    282         if (rc != EOK) {
    283                 async_wait_for(req, NULL);
    284                 return (int) rc;
     268        ipc_call_t answer;
     269        aid_t req = async_send_0(ldr->phone_id, LOADER_SET_FILES, &answer);
     270        sysarg_t rc = async_data_write_start(ldr->phone_id, (void *) files_buf,
     271            count * sizeof(fdi_node_t));
     272        if (rc != EOK) {
     273                async_wait_for(req, NULL);
     274                return rc;
    285275        }
    286276       
    287277        async_wait_for(req, &rc);
    288         return (int) rc;
     278        if (rc != EOK)
     279                return rc;
     280       
     281        /* Free temporary buffer */
     282        free(files_buf);
     283       
     284        return EOK;
    289285}
    290286
     
    301297int loader_load_program(loader_t *ldr)
    302298{
    303         async_exch_t *exch = async_exchange_begin(ldr->sess);
    304         int rc = async_req_0_0(exch, LOADER_LOAD);
    305         async_exchange_end(exch);
    306        
    307         return rc;
     299        return (int) async_req_0_0(ldr->phone_id, LOADER_LOAD);
    308300}
    309301
     
    314306 * the task and its thread is stopped.
    315307 *
    316  * After using this function, no further operations can be performed
    317  * on the loader structure and it is deallocated.
     308 * After using this function, no further operations must be performed
     309 * on the loader structure. It should be de-allocated using free().
    318310 *
    319311 * @param ldr Loader connection structure.
     
    324316int loader_run(loader_t *ldr)
    325317{
    326         async_exch_t *exch = async_exchange_begin(ldr->sess);
    327         int rc = async_req_0_0(exch, LOADER_RUN);
    328         async_exchange_end(exch);
    329        
     318        int rc = async_req_0_0(ldr->phone_id, LOADER_RUN);
    330319        if (rc != EOK)
    331320                return rc;
    332321       
    333         async_hangup(ldr->sess);
    334         free(ldr);
    335        
     322        async_hangup(ldr->phone_id);
     323        ldr->phone_id = 0;
    336324        return EOK;
    337325}
     
    339327/** Cancel the loader session.
    340328 *
    341  * Tell the loader not to load any program and terminate.
    342  * After using this function, no further operations can be performed
    343  * on the loader structure and it is deallocated.
     329 * Tells the loader not to load any program and terminate.
     330 * After using this function, no further operations must be performed
     331 * on the loader structure. It should be de-allocated using free().
    344332 *
    345333 * @param ldr Loader connection structure.
     
    350338void loader_abort(loader_t *ldr)
    351339{
    352         async_hangup(ldr->sess);
    353         free(ldr);
     340        async_hangup(ldr->phone_id);
     341        ldr->phone_id = 0;
    354342}
    355343
Note: See TracChangeset for help on using the changeset viewer.