]> rtime.felk.cvut.cz Git - frescor/fwp.git/commitdiff
Reworked error handling
authorMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 17 Jul 2008 20:46:04 +0000 (22:46 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 17 Jul 2008 20:46:04 +0000 (22:46 +0200)
Now, fwp library doesn't use perror() and printf() and error code
is returned in errno variable. Most functions return -1 or NULL on error.

fwp/lib/core/fwp_endpoint.c
fwp/lib/core/fwp_msgb.c
fwp/lib/core/fwp_msgq.c
fwp/lib/core/fwp_util.c
fwp/lib/core/fwp_vres.c
fwp/lib/mngt/fwp_contract.c
fwp/lib/mngt/fwp_mngt.c

index 6b58212d89902aab81a4465fbb65a1cb4eca055b..17d9b522e63cc002fcc1dfca6c95a82e0b496677 100644 (file)
@@ -1,5 +1,6 @@
 #include "fwp_endpoint.h"
 #include "fwp_msgb.h"
+#include <errno.h>
 
 #include <pthread.h>
 
@@ -83,13 +84,20 @@ int fwp_endpoint_is_bound(fwp_endpoint_d_t epointd)
        return (epointd->status == FWP_EPOINT_BOUND);
 }
 
+/** 
+ * Initializes endpoint table.
+ * 
+ * @param max_endpoints For how many endpoints allocate the table.
+ * 
+ * @return Zero on success, -1 on error (errno contains the code).
+ */
 int fwp_endpoint_table_init(unsigned int max_endpoints)
 {
        int table_size = max_endpoints * sizeof(fwp_endpoint_t);
 
        fwp_endpoint_table.entry = (fwp_endpoint_t*) malloc(table_size);
        if (!fwp_endpoint_table.entry)
-               return -ENOMEM;
+               return -1;      /* errno is set to ENOMEM by malloc() */
        memset(fwp_endpoint_table.entry, 0, table_size);
        fwp_endpoint_table.max_endpoints = max_endpoints;
        return 0;
@@ -116,6 +124,7 @@ static fwp_endpoint_t* fwp_endpoint_alloc()
        
        if (i == max_endpoints) {
                pthread_mutex_unlock(&fwp_endpoint_table.lock);
+               errno = ENOBUFS;
                return NULL;
        }
 
@@ -129,14 +138,16 @@ static fwp_endpoint_t* fwp_endpoint_alloc()
  *
  * \param[in] epointd Endpoint descriptor
  * \return On success 0 is returned. 
- * On error, negative error value is returned. 
+ * On error, negative error value is returned and errno is set appropriately
  */
 int fwp_endpoint_destroy(fwp_endpoint_d_t epointd)
 {
        fwp_endpoint_t *epoint = epointd;
        
-       if (!fwp_endpoint_is_valid(epointd))
-               return -EINVAL;
+       if (!fwp_endpoint_is_valid(epointd)) {
+               errno = EINVAL;
+               return -1;
+       }
        
        epoint->status = FWP_EPOINT_FREE;
        if (epoint->sockd > 0) 
@@ -182,8 +193,7 @@ int fwp_endpoint_attr_init(fwp_endpoint_attr_t *attr)
  * \param[in] attr Endpoint attributes
  * \param[out] epointdp  Pointer to the descriptor of newly created endpoint
  *
- * \return On success returns descriptor of endpoint. 
- * On error, negative error code is returned. 
+ * \return Zero on success, -1 on error and sets errno appropriately. 
  *
  */
 int fwp_send_endpoint_create(unsigned int node, unsigned int port,
@@ -195,7 +205,8 @@ int fwp_send_endpoint_create(unsigned int node, unsigned int port,
 
        epoint = fwp_endpoint_alloc();  
         if (!epoint) {
-               return -ENOMEM;
+               errno = ENOMEM;
+               return -1;
        }
        
        epoint->type = FWP_SEND_EPOINT;
@@ -216,27 +227,27 @@ int fwp_send_endpoint_create(unsigned int node, unsigned int port,
        
        if (epoint->attr.reliability == FWP_EPOINT_RELIABLE) {
                epoint->sockd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+               if (epoint->sockd < 0) {
+                       goto err;
+               }
        } else {
                epoint->sockd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
-
+               if (epoint->sockd < 0) {
+                       goto err;
+               }
+       
                /* Enable broadcasts */
                unsigned int yes = 1;
                if (setsockopt(epoint->sockd,SOL_SOCKET, SO_BROADCAST, 
                               &yes, sizeof(yes)) == -1) {
-                       perror("setsockopt(SO_BROADCAST)");
-                       return (-errno);
+                       FWP_DEBUG("setsockopt(SO_BROADCAST): %s", strerror(errno));
+                       goto err;;
                }
        
        }
        
-       if (epoint->sockd < 0) {
-                       perror("Unable to open socket");
-                       goto err;
-       }
-       
        if (connect(epoint->sockd,(struct sockaddr*) &epoint->peer.addr, 
                        epoint->peer.addrlen)) {
-               perror("Connect error");
                goto err;
        }
        
@@ -245,7 +256,7 @@ int fwp_send_endpoint_create(unsigned int node, unsigned int port,
        return 0;               
 err:
        fwp_endpoint_destroy(epoint);
-       return (-errno);        
+       return -1;      
 }
 
 /**
@@ -255,8 +266,7 @@ err:
  * \param[in] attr Endpoint attributes
  * \param[out] epointdp  Pointer to the descriptor of newly created endpoint
  *
- * \return On success returns descriptor of endpoint. 
- * On error, negative error code is returned. 
+ * \return Zero on success, -1 on error and errno is set.
  */
 int fwp_receive_endpoint_create(/*unsigned int node,*/ unsigned int port,
                                fwp_endpoint_attr_t *attr, 
@@ -268,7 +278,8 @@ int fwp_receive_endpoint_create(/*unsigned int node,*/ unsigned int port,
 
        epoint = fwp_endpoint_alloc();  
         if (!epoint) {
-               return -ENOMEM;
+               errno = ENOMEM;
+               return -1;
        }
        
        epoint->type = FWP_RECV_EPOINT;
@@ -288,20 +299,20 @@ int fwp_receive_endpoint_create(/*unsigned int node,*/ unsigned int port,
        if (epoint->attr.reliability == FWP_EPOINT_RELIABLE) {
                if ((epoint->sockd = socket(PF_INET, SOCK_STREAM, 
                                                IPPROTO_TCP)) < 0) {
-                       perror("Unable to open socket");
+                       FWP_ERROR("Unable to open socket: %s", strerror(errno));
                        goto err;
                }       
                
                int yes = 1;
                if (setsockopt(epoint->sockd,SOL_SOCKET, SO_REUSEADDR,
                               &yes, sizeof(yes)) == -1) {
-                       perror("setsockopt(SO_REUSEADDR)");
-                       return (-errno);
+                       FWP_ERROR("setsockopt(SO_REUSEADDR): %s", strerror(errno));
+                       goto err;
                }
 
                if (bind(epoint->sockd, (struct sockaddr*) &epoint->peer.addr, 
                                epoint->peer.addrlen) == -1) {
-                       perror("Bind error");
+                       FWP_ERROR("Bind error: %s", strerror(errno));
                        /* TODO: remove all error messages from all libraries */
                        goto err;
                }
@@ -320,13 +331,13 @@ int fwp_receive_endpoint_create(/*unsigned int node,*/ unsigned int port,
        } else {
                if ((epoint->sockd = socket(PF_INET, SOCK_DGRAM, 
                                                IPPROTO_UDP)) < 0) {
-                       perror("Unable to open socket");
+                       FWP_ERROR("Unable to open socket: %s", strerror(errno));
                        goto err;
                }
                
                if (bind(epoint->sockd, (struct sockaddr*) &epoint->peer.addr, 
                                epoint->peer.addrlen) == -1) {
-                       perror("Bind error");
+                       FWP_ERROR("Bind error: %s", strerror(errno));
                        goto err;
                }
        }
@@ -334,7 +345,7 @@ int fwp_receive_endpoint_create(/*unsigned int node,*/ unsigned int port,
        /*if (setsockopt(epoint->sockd, SOL_SOCKET, SO_RCVBUF, 
                        &rcvbuf_size, sizeof(rcvbuf_size)) == -1) {
                
-               perror("Unable to set socket buffer size");
+               FWP_ERROR("Unable to set socket buffer size: %s", strerror(errno));
                return -1;
        }else {
                FWP_DEBUG("Receive endpoint buffer size is set.\n");
@@ -352,9 +363,9 @@ int fwp_receive_endpoint_create(/*unsigned int node,*/ unsigned int port,
        *epointdp = epoint; 
        return 0;
 
-err:   
+err:
        fwp_endpoint_destroy(epoint);
-       return (-errno);        
+       return -1;
 }
 
 /**
@@ -363,7 +374,7 @@ err:
  * \param[in] vres_id identifier of vres
  * \param[in] epoint_id send endpoint identifier
  *
- * \return On success returns 0. On error, negative error code is returned 
+ * \return On success returns 0. On error, -1 and errno is set appropriately.
  */
 int fwp_send_endpoint_bind(fwp_endpoint_d_t epointd, fwp_vres_d_t vresd)
 {
@@ -374,14 +385,14 @@ int fwp_send_endpoint_bind(fwp_endpoint_d_t epointd, fwp_vres_d_t vresd)
        pthread_mutex_lock(&fwp_endpoint_table.lock);
        if ((!fwp_endpoint_is_valid(epointd))|| 
                (epoint->type != FWP_SEND_EPOINT)) {    
-               rv = -EINVAL;
+               errno = EINVAL;
                goto err;
        }
        if (epoint->status == FWP_EPOINT_BOUND) { /* already bound*/
-               rv = -EPERM;
+               errno = EPERM;
                goto err;
        }
-       if ((rv = _fwp_vres_bind(vresd, epoint->sockd)) < 0) { 
+       if ((rv = _fwp_vres_bind(vresd, epoint->sockd)) < 0) {
                goto err;
        }
 
@@ -401,7 +412,7 @@ err:
  * Unbinds send endpoint from vres
  *
  * \param[in] epointd Send endpoint descriptor 
- * \return On success returns 0. On error, negative error code is returned 
+ * \return On success returns 0. On error, -1 is returned and errno is set appropriately.
  *
  */
 int fwp_send_endpoint_unbind(fwp_endpoint_d_t epointd)
@@ -409,10 +420,14 @@ int fwp_send_endpoint_unbind(fwp_endpoint_d_t epointd)
        fwp_endpoint_t *epoint = epointd;
        int rv = 0;
 
-       if (!fwp_endpoint_is_valid(epointd))
-               return -EINVAL;
-       if (epoint->status != FWP_EPOINT_BOUND)
-               return -EPERM;
+       if (!fwp_endpoint_is_valid(epointd)) {
+               errno = EINVAL;
+               return -1;
+       }                       
+       if (epoint->status != FWP_EPOINT_BOUND) {
+               errno = EPERM;
+               return -1;
+       }
 
        /* unlink epoint-vres mutually */
        if ((rv = _fwp_vres_unbind(epoint->vresd)) < 0) 
@@ -431,7 +446,7 @@ int fwp_send_endpoint_unbind(fwp_endpoint_d_t epointd)
  *
  * \return
  * On success, it returns number of received bytes.  
- * On error, negative error code is returned,
+ * On error, -1 is returned and errno is set appropriately.
  *
  */
 ssize_t fwp_recv(fwp_endpoint_d_t epointd, void *buffer, size_t buffer_size, 
@@ -443,12 +458,13 @@ ssize_t fwp_recv(fwp_endpoint_d_t epointd, void *buffer, size_t buffer_size,
        fd_set fdset;
        int csockd, i;
        
-       if (!fwp_endpoint_is_valid(epointd))
-               return -EINVAL;
+       if (!fwp_endpoint_is_valid(epointd)) {
+               errno = EINVAL;
+               return -1;
+       }
        
        if (epoint->attr.reliability == FWP_EPOINT_BESTEFFORT) {        
-               _fwp_recvfrom(len, epoint->sockd, buffer, buffer_size, 0, 
-                             peer->addr, &peer->addrlen);
+               len = _fwp_recvfrom(epoint->sockd, buffer, buffer_size, 0, peer);
                return len;
        }
        
@@ -459,8 +475,8 @@ ssize_t fwp_recv(fwp_endpoint_d_t epointd, void *buffer, size_t buffer_size,
                if (select(FD_SETSIZE, &fdset, (fd_set *)0, 
                           (fd_set *)0, NULL) < 0) {
                
-                       perror("Error in select");
-                       return (-errno);
+                       FWP_ERROR("Error in select: %s", strerror(errno));
+                       return -1;
                }
        
                if (FD_ISSET(epoint->sockd, &fdset)) { /* is it listen socket? */ 
@@ -487,9 +503,10 @@ ssize_t fwp_recv(fwp_endpoint_d_t epointd, void *buffer, size_t buffer_size,
                /* Check client TCP sockets */
                for (i = 0; i < epoint->nr_connections; i++) {
                        if (FD_ISSET(epoint->c_sockd[i], &fdset)) {
-                               _fwp_recvfrom(len, epoint->c_sockd[i], buffer, buffer_size,
-                                             0, peer->addr, &peer->addrlen);
-                       
+                               len = _fwp_recvfrom(epoint->c_sockd[i], buffer, buffer_size,
+                                                   0, peer);
+                               if (len < 0) /* Error */
+                                       return len;
                                FWP_DEBUG("Received tcp data\n");
                                if (len)
                                        return len;
@@ -515,7 +532,7 @@ ssize_t fwp_recv(fwp_endpoint_d_t epointd, void *buffer, size_t buffer_size,
  *
  * \return
  * On success, it returns zero.  
- * On error, negative error code is returned,
+ * On error, -1 is returned and errno is set appropriately.
  *
  */
 int fwp_send(fwp_endpoint_d_t epointd, void *msg, size_t size, int flags)
@@ -524,17 +541,21 @@ int fwp_send(fwp_endpoint_d_t epointd, void *msg, size_t size, int flags)
        struct fwp_msgb *msgb;
 
        if (!fwp_endpoint_is_valid(epointd)){
-               return -EINVAL;
+               errno = EINVAL;
+               return -1;
        }
        if (!fwp_endpoint_is_bound(epointd)){
-               return -EPERM;
+               errno = EPERM;
+               return -1;
        }
 
        /*if (flags && MSG_DONTWAIT) 
                msgb = fwp_msgb_alloc(buffer_size);
        else {*/
-               if (!(msgb = fwp_msgb_alloc(size)))
-                       return -ENOMEM;
+               if (!(msgb = fwp_msgb_alloc(size))) {
+                       errno = ENOMEM;
+                       return -1;
+               }
 
                msgb->peer = &epoint->peer;
                /*msgb->data = msg;*/
index c69a6e211f8988a4af59389bfe64e2cf78cac65f..c1714ac53113cd0ed303b02cf54b104f8bdc5bdb 100644 (file)
@@ -73,7 +73,7 @@ inline unsigned char* fwp_msgb_put(struct fwp_msgb *msgb, unsigned int len)
  *
  * \param msgb Pointer to msgb
  * \param len The lenght data 
- * \return Previous tail pointer
+ * \return Previous tail pointer on NULL in case of lack of space in the buffer.
  *
  */
 inline unsigned char* fwp_msgb_pull(struct fwp_msgb *msgb, unsigned int len)
index 3c621229649dcb62a56141cc46b14a61c899c895..67c5314d2123722f153f6cf6fcf0b5e0801d941a 100644 (file)
@@ -1,4 +1,5 @@
 #include "fwp_msgq.h"
+#include <errno.h>
 
 /**
  *Initialize message queue 
@@ -19,7 +20,7 @@ void fwp_msgq_init(struct fwp_msgq *msgq)
  * @param[in] msgq Message queue
  *`@param[in] msgb Message buffer which stores a message
  * @return
- *   0 it there are no errors 
+ *   Zero on success, -1 or error.
  **/
 int fwp_msgq_enqueue(struct fwp_msgq *msgq, struct fwp_msgb *msgb)
 {
@@ -31,6 +32,7 @@ int fwp_msgq_enqueue(struct fwp_msgq *msgq, struct fwp_msgb *msgb)
                
                /*release queue mutex*/
                pthread_mutex_unlock(&msgq->lock);
+               errno = ENOBUFS;
                return -1;
                /* }
                 * if (msgq->qr_policy == OLDEST)
index eff91a60f55790099d52576b10787ae81756a4f7..14fb664ea88e7067c166b08547bee2804d772a60 100644 (file)
@@ -39,34 +39,34 @@ int fwp_set_rt_prio(int priority)
        static struct sched_param param;
 
        if ((maxpri = sched_get_priority_max(SCHED_FIFO)) == -1)        {
-               perror("fwp_set_rt_prio - sched_get_priority_max call failed");
-               return -errno;
+               FWP_ERROR("sched_get_priority_max call failed: %s", strerror(errno));
+               return -1;
        }
 
        if ((minpri = sched_get_priority_min(SCHED_FIFO)) == -1)        {
-               perror("fwp_set_rt_prio - sched_get_priority_min call failed");
-               return -errno;
+               FWP_ERROR("sched_get_priority_min call failed: %s", strerror(errno));
+               return -1;
        }
 
        if (priority > maxpri)  {
-               fprintf(stderr,"fwp_set_rt_prio - priority parameter %d is"
-                       " greater than the maximal allowed priority %d.\n", 
-                       priority, maxpri);
-               return -EINVAL;
+               FWP_ERROR("parameter %d is greater than the maximal allowed priority %d.\n", 
+                         priority, maxpri);
+               errno = EINVAL;
+               return -1;
        }
 
        if (priority < minpri)  {
-               fprintf(stderr,"fwp_set_rt_prio - priority parameter %d is"
-                       " lower than the minimal allowed priority %d.\n", 
-                       priority, minpri);
-               return -EINVAL;
+               FWP_ERROR(stderr,"priority parameter %d is lower than the minimal allowed priority %d.\n", 
+                         priority, minpri);
+               errno = EINVAL;
+               return -1;
        }
 
        param.sched_priority = priority;
 
        if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
-               perror("fwp_set_rt_prio - sched_setscheduler call failed");
-               return -errno;
+               FWP_ERROR("sched_setscheduler call failed: %s", strerror(errno));
+               return -1;
        }
 
        return 0;
@@ -77,8 +77,8 @@ int fwp_create_unix_socket(char *path, struct sockaddr_un *addr)
        int sockfd;
 
        if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1){
-               perror("fwp_create_unix_socket - socket error");
-               return (-errno);
+               FWP_ERROR("socket error: %s", strerror(errno));
+               return (-1);
        }
        
        bzero(addr, sizeof(addr));
@@ -89,8 +89,8 @@ int fwp_create_unix_socket(char *path, struct sockaddr_un *addr)
 
        if (bind(sockfd, (struct sockaddr*)addr, 
                 sizeof(*addr)) == -1) {
-                       perror("fwp_open_unix_socket - bind error");
-                       return (-errno);
+                       FWP_ERROR("fwp_open_unix_socket - bind error: %s", strerror(errno));
+                       return (-1);
        }
        
        return sockfd;  
@@ -101,8 +101,8 @@ int fwp_create_inet_socket(unsigned int port, struct sockaddr_in *addr)
        int sockfd;
 
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
-               perror("fwp_create_inet_socket - socket error");
-               return (-errno);
+               FWP_ERROR("socket error: %s", strerror(errno));
+               return (-1);
        }
        
        addr->sin_family = AF_INET;
@@ -112,8 +112,8 @@ int fwp_create_inet_socket(unsigned int port, struct sockaddr_in *addr)
        if (bind(sockfd, (struct sockaddr*)addr, 
                 sizeof(*addr)) == -1) {
                        
-               perror("fwp_create_inet_socket - bind error");
-               return (-errno);
+               FWP_ERROR("bind error: %s", strerror(errno));
+               return (-1);
        }
        
        return sockfd;  
@@ -128,7 +128,7 @@ int fwp_create_inet_socket(unsigned int port, struct sockaddr_in *addr)
        sigaddset(&sigset, SIGTERM);
        ret = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
        if (ret != 0) {
-               perror("pthread_sigmask failed");
+               FWP_ERROR("pthread_sigmask failed: %s", strerror(errno));
                exit(1);
        }
 }
index 08a48edd084cf79f0760e657557bbbfa01ebe5c3..8b3142d664dfa0d3d55849a71866cfd82f405562 100644 (file)
@@ -69,8 +69,8 @@ static inline int fwp_vres_set_ac(int sockd, fwp_ac_t ac_id)
        
        tos = ac_to_tos[ac_id];
        if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
-               perror("Root permission needed to set AC");
-               //return (-errno);
+               FWP_ERROR("setsockopt: %s", strerror(errno));
+               return (-1);
        }
        
        return 0;
@@ -83,21 +83,24 @@ static int fwp_vres_ac_open(fwp_ac_t ac_id)
        int sockd;
        unsigned int tos;
        
-       if ((ac_id < 0)||(ac_id >= FWP_AC_NUM))
-               return -EINVAL;
+       if ((ac_id < 0)||(ac_id >= FWP_AC_NUM)) {
+               errno = EINVAL;
+               return -1;
+       }
 
        if ((sockd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
-               perror("Unable to open socket for AC");
-               return (-errno);
+               FWP_ERROR("Unable to open socket for AC: %s", strerror(errno));
+               return (-1);
        }
 
        tos = ac_to_tos[ac_id];
        
        if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
-               perror("Root permission needed to set AC");
-               /* let pass
-                * close(sockfd);
-                *return (-errno);*/
+               int e = errno;
+               FWP_ERROR("setsockopt(IP_TOS): %s", strerror(errno));
+               close(sockfd);
+               errno = e;
+               return -1;
        }
        
        return sockd;
@@ -108,8 +111,7 @@ static inline int __fwp_vres_send(unsigned int ac_sockd, struct fwp_msgb* msgb)
 {
        /*_fwp_sendto(ac_sockd, msgb->data, msgb->len, 0, 
                        msgb->peer->addr, msgb->peer->addrlen);*/
-       _fwp_send(ac_sockd, msgb->data, msgb->len, 0);
-       return 0;
+       return _fwp_send(ac_sockd, msgb->data, msgb->len, 0);
 }
 
 static inline void fwp_vres_free(fwp_vres_t *vres)
@@ -142,8 +144,8 @@ int fwp_vres_table_init(unsigned int max_vres)
        unsigned int table_size = max_vres * sizeof(fwp_vres_t);
 
        fwp_vres_table.entry = (fwp_vres_t*) malloc(table_size);
-       if (!fwp_vres_table.entry)
-               return -ENOMEM;
+       if (!fwp_vres_table.entry) 
+               return -1;      /* Errno is set by malloc */
 
        memset((void*) fwp_vres_table.entry, 0, table_size);
        fwp_vres_table.max_vres = max_vres;
@@ -178,6 +180,7 @@ fwp_vres_d_t fwp_vres_alloc()
        
        if (i == max_vres) {
                pthread_mutex_unlock(&fwp_vres_table.lock);
+               errno = ENOBUFS;
                return NULL;
        }
 
@@ -202,7 +205,8 @@ int fwp_vres_set_params(fwp_vres_d_t vresd, fwp_vres_params_t *params)
        int rv;
        
        if (!fwp_vres_is_valid(vresd)) {
-               return -EINVAL;
+               errno = EINVAL;
+               return -1;
        }
        /* copy vres paramters into vres structure */
        memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
@@ -256,7 +260,7 @@ int fwp_vres_create(fwp_vres_params_t *params, fwp_vres_d_t *vresdp)
 
        vres = fwp_vres_alloc();
        if (!vres) {
-               return -ENOMEM;
+               return -1;
        }
        /* copy vres paramters into vres structure */
        memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
@@ -275,7 +279,8 @@ int fwp_vres_create(fwp_vres_params_t *params, fwp_vres_d_t *vresdp)
        return 0;
 err:   
        fwp_vres_free(vres);
-       return rv; 
+       errno = rv;
+       return -1; 
 }
 
 /**
@@ -291,8 +296,10 @@ int fwp_vres_destroy(fwp_vres_d_t vresd)
 {      
        fwp_vres_t *vres = vresd;
 
-       if (!fwp_vres_is_valid(vresd))
-               return -EINVAL;
+       if (!fwp_vres_is_valid(vresd)) {
+               errno = EINVAL;
+               return -1;
+       }
        
        vres->status = FWP_VRES_FREE;
        pthread_cancel(vres->tx_thread);
@@ -392,8 +399,10 @@ int _fwp_vres_send(fwp_vres_d_t vresd, struct fwp_msgb* msgb)
 
        if (vres->status == FWP_VRES_BOUND) {
                return fwp_msgq_enqueue(&vres->tx_queue, msgb);
-       } else 
-               return -EPERM;
+       } else {
+               errno = EPERM;  /* TODO: Use more appropriate error than EPERM. */
+               return -1;
+       }
 }
 
 /*int _fwp_vres_bind(fwp_vres_d_t vresd, fwp_endpoint_t *epoint)*/
@@ -404,12 +413,14 @@ int _fwp_vres_bind(fwp_vres_d_t vresd, int sockd)
 
        pthread_mutex_lock(&fwp_vres_table.lock);
        if (!fwp_vres_is_valid(vresd)) {
-               rv = -EINVAL;
+               errno = EINVAL;
+               rv = -1;
                goto err;
        }
 
        if (vres->status != FWP_VRES_UNBOUND) { /*if already bounded */
-               rv = -EPERM;
+               errno = EPERM;  
+               rv = -1;
                goto err;
        }
 
@@ -426,7 +437,8 @@ int _fwp_vres_unbind(fwp_vres_d_t vresd)
        fwp_vres_t *vres = vresd;
        
        if (!fwp_vres_is_valid(vresd)) {
-               return -EINVAL;
+               errno = EINVAL;
+               return -1;
        }
        vres->status = FWP_VRES_UNBOUND;
        /* TODO: consider what to do with pending messages */
index 4262aa87f28d03746da55c931c304aa2710f8f48..0b6a076c52945a8f1a58ad78dba57e80addfc998 100644 (file)
@@ -29,16 +29,19 @@ int fwp_contract_is_negotiated(fwp_contract_d_t contract)
  *
  * \return  
  * If successful, the  function returns zero and vres descriptor is
- * stored in vresdp parameter, otherwise,  an  error  number  shall  be
- * returned to indicate the error. If the contract is not negotiated EAGAIN 
+ * stored in vresdp parameter, otherwise -1 is returned and errno is set appropriately. If the contract is not negotiated EAGAIN 
  * error is returned.
  */
 /*int fwp_contract_negotiate(fwp_contract_d_t contractd, void *vresdp) */
 int fwp_contract_negotiate(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
 {
-       fwp_contract_reserve(contractd);
-       if (!fwp_contract_is_reserved(contractd)) {     
-               return -EAGAIN;
+       int ret;
+       ret = fwp_contract_reserve(contractd);
+       if (ret < 0)
+               return ret;
+       if (!fwp_contract_is_reserved(contractd)) {
+               errno = EAGAIN;
+               return -1;
        }       
        
        fwp_contract_commit(contractd, vresdp);
@@ -88,8 +91,10 @@ int fwp_contract_destroy(fwp_contract_d_t contractd)
 {
        fwp_contract_data_t *contdata = contractd;
 
-       if (contdata->status != FWP_CONT_NOTNEGOTIATED)
-               return -EPERM;
+       if (contdata->status != FWP_CONT_NOTNEGOTIATED) {
+               errno = EINVAL;
+               return -1;
+       }
        fwp_contract_data_delete(contdata);
        return 0;
 }
@@ -109,6 +114,7 @@ int fwp_contract_reserve(fwp_contract_d_t contractd)
        fwp_msgb_t *msgb;
        fwp_msg_type_t msg_type;
        fwp_participant_id_t participant_id;
+       int ret;
        
        /*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
                                                contract_id);*/
@@ -127,10 +133,13 @@ int fwp_contract_reserve(fwp_contract_d_t contractd)
        fwp_msg_contract_in(msgb->tail, &contdata->contract);
        fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
 
-       fwp_mngt_send(FWP_MSG_RESERVE, msgb, 
-                       fwp_participant_this, fwp_participant_mngr);
-       fwp_mngt_recv(&msg_type, &participant_id, msgb);
-
+       ret = fwp_mngt_send(FWP_MSG_RESERVE, msgb, 
+                           fwp_participant_this, fwp_participant_mngr);
+       if (ret < 0)
+               return ret;
+       ret = fwp_mngt_recv(&msg_type, &participant_id, msgb);
+       if (ret < 0)
+               return ret;
        fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
        fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
        
@@ -154,13 +163,14 @@ int fwp_contract_reserve(fwp_contract_d_t contractd)
  * \param[out] vresd Descriptor of vres
  *
  * \return On success, returns zero.
- * On error, returns negative error code.
+ * On error, returns -1 and sets errno appropriately.
  *
  */
 int fwp_contract_commit(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
 {
        fwp_contract_data_t *contdata = contractd;
        fwp_msgb_t *msgb;
+       int ret;
        
        /* Send COMMIT to manager */
        msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
@@ -170,16 +180,19 @@ int fwp_contract_commit(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
        fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
        FWP_DEBUG("Commit contract id =%d\n", contdata->id);
        
-       fwp_mngt_send(FWP_MSG_COMMIT, msgb, 
-                       fwp_participant_this, fwp_participant_mngr);
+       ret = fwp_mngt_send(FWP_MSG_COMMIT, msgb, 
+                           fwp_participant_this, fwp_participant_mngr);
+       if (ret < 0)
+               return ret;
        
        contdata->status = FWP_CONT_NEGOTIATED;
        /* Set parameters of vres 
         * and activate it if needed */
-       fwp_vres_set_params(contdata->vresd, &contdata->vres_params);
+       ret = fwp_vres_set_params(contdata->vresd, &contdata->vres_params);
+       if (ret < 0)
+               return ret;
        *vresdp = contdata->vresd;
 
-       /*TODO: error handling */
        return 0;
 }
 
@@ -197,8 +210,10 @@ int fwp_contract_cancel(fwp_contract_d_t contractd)
        fwp_contract_data_t *contdata = contractd;
        fwp_msgb_t *msgb;
        
-       if (contdata->status != FWP_CONT_NEGOTIATED) 
-               return -EPERM;
+       if (contdata->status != FWP_CONT_NEGOTIATED) {
+               errno = EINVAL;
+               return -1;
+       }
 
        /* Send CANCEL to manager */
        msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
index 43f9ecadc5b5023fe2fe4eadb3d40db45e06a75e..2b88bac53632cca0675e0a92c7d05415f50a822e 100644 (file)
@@ -31,12 +31,14 @@ static fwp_vres_params_t fwp_service_vres_params = {
 int fwp_mngt_send(fwp_msg_type_t type,fwp_msgb_t *msgb,
                  fwp_participant_t *source, fwp_participant_t *dest)
 {
+       int ret;
+       
        fwp_msgb_push(msgb, sizeof(struct fwp_msg_header));
        fwp_msg_header_in(msgb->data, type, msgb->len, source->id);
 
-       fwp_send(dest->epointd, msgb->data, msgb->len, 0);
+       ret = fwp_send(dest->epointd, msgb->data, msgb->len, 0);
 
-       return 0;
+       return ret;
 }
 
 /**
@@ -51,6 +53,8 @@ int fwp_mngt_recv(fwp_msg_type_t *type, fwp_participant_id_t *participant_id,
        fwp_msgb_reset_data(msgb);
        size = fwp_recv(fwp_participant_this->epointd, msgb->data, 
                        msgb->buffer_size, 0);
+       if (size < 0)
+               return size;
        fwp_msgb_put(msgb, size);
 
        fwp_msg_header_out(msgb->data, type, participant_id);