From bc9c60382b226e5a75a11a99364b9b799dc2b0c2 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Tue, 5 Jul 2016 18:52:12 +0200 Subject: lib: Change invalid pid to -1 The stack used pid 0 (the scheduler) to indicate an invalid process instance, probably as a leftover from the deprecated application process instance id. Using -1 is a better solution. Fixes #16. --- src/ipcpd/shim-udp/main.c | 4 ++-- src/irmd/main.c | 20 ++++++++++++-------- src/irmd/registry.c | 13 ++++--------- src/lib/dev.c | 6 +++--- src/lib/ipcp.c | 2 +- src/lib/irm.c | 6 +++--- src/lib/irmd_messages.proto | 4 ++-- src/lib/shm_ap_rbuff.c | 4 ++-- src/lib/shm_du_map.c | 12 ++++++------ 9 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index c22947fa..9354ec2f 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -961,7 +961,7 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) /* NOTE: Disgusted with this crap */ static int ddns_send(char * cmd) { - pid_t api = 0; + pid_t api = -1; int wstatus; int pipe_fd[2]; char * argv[] = {NSUPDATE_EXEC, 0}; @@ -1005,7 +1005,7 @@ static int ddns_send(char * cmd) static uint32_t ddns_resolve(char * name, uint32_t dns_addr) { - pid_t api = 0; + pid_t api = -1; int wstatus; int pipe_fd[2]; char dnsstr[INET_ADDRSTRLEN]; diff --git a/src/irmd/main.c b/src/irmd/main.c index 8475839b..de100cf8 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -51,8 +51,6 @@ #include #include -#define API_INVALID 0 - #define IRMD_CLEANUP_TIMER ((IRMD_FLOW_TIMEOUT / 20) * MILLION) /* ns */ struct ipcp_entry { @@ -114,9 +112,9 @@ static struct port_map_entry * port_map_entry_create() if (e == NULL) return NULL; - e->n_api = 0; - e->n_1_api = 0; - e->port_id = 0; + e->n_api = -1; + e->n_1_api = -1; + e->port_id = -1; e->state = FLOW_NULL; if (pthread_cond_init(&e->res_signal, NULL)) { @@ -256,7 +254,7 @@ static pid_t get_ipcp_by_dst_name(char * dst_name, } } - return 0; + return -1; } static pid_t create_ipcp(char * name, @@ -269,7 +267,7 @@ static pid_t create_ipcp(char * name, if (instance->state != IRMD_RUNNING) { pthread_rwlock_unlock(&instance->state_lock); - return 0; + return -1; } api = ipcp_create(ipcp_type); @@ -456,6 +454,9 @@ static int bind_name(char * name, char ** argv_dup = NULL; char * apn = path_strip(ap_name); + if (name == NULL || ap_name == NULL) + return -EINVAL; + pthread_rwlock_rdlock(&instance->state_lock); if (instance->state != IRMD_RUNNING) { @@ -578,6 +579,9 @@ static int ap_reg(char * name, struct reg_entry * reg = NULL; struct list_head * pos = NULL; + if (name == NULL || difs == NULL || len == 0 || difs[0] == NULL) + return -EINVAL; + pthread_rwlock_rdlock(&instance->state_lock); if (instance->state != IRMD_RUNNING) { @@ -1113,7 +1117,7 @@ static struct port_map_entry * flow_req_arr(pid_t api, case REG_NAME_FLOW_ACCEPT: pme->n_api = reg_entry_resolve_api(rne); - if (pme->n_api == 0) { + if (pme->n_api == -1) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Invalid api returned."); diff --git a/src/irmd/registry.c b/src/irmd/registry.c index 063309a4..a4e2fcfa 100644 --- a/src/irmd/registry.c +++ b/src/irmd/registry.c @@ -290,7 +290,7 @@ pid_t reg_entry_resolve_api(struct reg_entry * e) return r->api; } - return 0; + return -1; } char ** reg_entry_resolve_auto(struct reg_entry * e) @@ -498,7 +498,7 @@ struct reg_instance * registry_add_ap_instance(struct list_head * registry, struct reg_entry * e = NULL; struct reg_instance * i = NULL; - if (name == NULL || api == 0) + if (name == NULL || api == -1) return NULL; e = registry_get_entry_by_name(registry, name); @@ -507,11 +507,6 @@ struct reg_instance * registry_add_ap_instance(struct list_head * registry, return NULL; } - if (api == 0) { - LOG_DBG("Invalid api."); - return NULL; - } - if (reg_entry_has_api(e, api)) { LOG_DBG("Instance already registered with this name."); return NULL; @@ -546,8 +541,8 @@ int registry_remove_ap_instance(struct list_head * registry, struct reg_entry * e = NULL; struct reg_instance * i = NULL; - if (name == NULL || api == 0) - return -1; + if (name == NULL || api == -1) + return -EINVAL; e = registry_get_entry_by_name(registry, name); if (e == NULL) { diff --git a/src/lib/dev.c b/src/lib/dev.c index 19bc90e5..d85afc45 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -93,7 +93,7 @@ int ap_init(char * ap_name) for (i = 0; i < AP_MAX_FLOWS; ++i) { _ap_instance->flows[i].rb = NULL; _ap_instance->flows[i].port_id = -1; - _ap_instance->flows[i].api = 0; /* API_INVALID */ + _ap_instance->flows[i].api = -1; } pthread_rwlock_init(&_ap_instance->flows_lock, NULL); @@ -397,7 +397,7 @@ int flow_dealloc(int fd) _ap_instance->flows[fd].port_id = -1; shm_ap_rbuff_close(_ap_instance->flows[fd].rb); _ap_instance->flows[fd].rb = NULL; - _ap_instance->flows[fd].api = 0; + _ap_instance->flows[fd].api = -1; bmp_release(_ap_instance->fds, fd); @@ -543,7 +543,7 @@ ssize_t flow_read(int fd, void * buf, size_t count) if (_ap_instance->flows[fd].oflags & FLOW_O_NONBLOCK) { idx = shm_ap_rbuff_read_port(_ap_instance->rb, - _ap_instance->flows[fd].port_id); + _ap_instance->flows[fd].port_id); } else { /* block */ while ((idx = shm_ap_rbuff_read_port(_ap_instance->rb, diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index b336155e..e4a82b60 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -103,7 +103,7 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, pid_t ipcp_create(enum ipcp_type ipcp_type) { - pid_t api = 0; + pid_t api = -1; char irmd_api[10]; size_t len = 0; char * ipcp_dir = "/sbin/"; diff --git a/src/lib/irm.c b/src/lib/irm.c index ee1851a3..4c71817d 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -67,7 +67,7 @@ int irm_destroy_ipcp(pid_t api) irm_msg_t * recv_msg = NULL; int ret = -1; - if (api == 0) + if (api == -1) return -EINVAL; msg.code = IRM_MSG_CODE__IRM_DESTROY_IPCP; @@ -97,7 +97,7 @@ int irm_bootstrap_ipcp(pid_t api, irm_msg_t * recv_msg = NULL; int ret = -1; - if (api == 0 || conf == NULL) + if (api == -1 || conf == NULL) return -EINVAL; msg.code = IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP; @@ -209,7 +209,7 @@ int irm_enroll_ipcp(pid_t api, irm_msg_t * recv_msg = NULL; int ret = -1; - if (api == 0 || dif_name == NULL) + if (api == -1 || dif_name == NULL) return -EINVAL; msg.code = IRM_MSG_CODE__IRM_ENROLL_IPCP; diff --git a/src/lib/irmd_messages.proto b/src/lib/irmd_messages.proto index fa2ca258..315d6092 100644 --- a/src/lib/irmd_messages.proto +++ b/src/lib/irmd_messages.proto @@ -47,7 +47,7 @@ message irm_msg { required irm_msg_code code = 1; optional string ap_name = 2; optional string ae_name = 3; - optional uint32 api = 4; + optional sint32 api = 4; optional uint32 ipcp_type = 5; repeated string dif_name = 6; repeated string args = 7; @@ -56,6 +56,6 @@ message irm_msg { optional sint32 port_id = 10; optional dif_config_msg conf = 11; optional uint32 opts = 12; - repeated int32 apis = 13; + repeated sint32 apis = 13; optional sint32 result = 14; }; diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c index f54627b7..e90afe19 100644 --- a/src/lib/shm_ap_rbuff.c +++ b/src/lib/shm_ap_rbuff.c @@ -348,9 +348,9 @@ ssize_t shm_ap_rbuff_read_port(struct shm_ap_rbuff * rb, int port_id) pid_t shm_ap_rbuff_get_api(struct shm_ap_rbuff *rb) { - pid_t api = 0; + pid_t api = -1; if (rb == NULL) - return 0; + return -1; pthread_mutex_lock(rb->shm_mutex); api = rb->api; diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index cf0bad19..6289857f 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -89,12 +89,12 @@ static void garbage_collect(struct shm_du_map * dum) { #ifdef SHM_DU_MAP_MULTI_BLOCK struct shm_du_buff * sdb; - while ((sdb = get_tail_ptr(dum))->dst_api == 0 && + while ((sdb = get_tail_ptr(dum))->dst_api == -1 && !shm_map_empty(dum)) *dum->ptr_tail = (*dum->ptr_tail + sdb->blocks) & (SHM_BLOCKS_IN_MAP - 1); #else - while (get_tail_ptr(dum)->dst_api == 0 && + while (get_tail_ptr(dum)->dst_api == -1 && !shm_map_empty(dum)) *dum->ptr_tail = (*dum->ptr_tail + 1) & (SHM_BLOCKS_IN_MAP - 1); @@ -110,7 +110,7 @@ static void clean_sdus(struct shm_du_map * dum, pid_t api) while (idx != *dum->ptr_head) { buf = idx_to_du_buff_ptr(dum, idx); if (buf->dst_api == api) - buf->dst_api = 0; + buf->dst_api = -1; #ifdef SHM_DU_MAP_MULTI_BLOCK idx = (idx + buf->blocks) & (SHM_BLOCKS_IN_MAP - 1); #else @@ -271,7 +271,7 @@ struct shm_du_map * shm_du_map_open() pid_t shm_du_map_owner(struct shm_du_map * dum) { if (dum == NULL) - return 0; + return -1; return *dum->api; } @@ -444,7 +444,7 @@ ssize_t shm_du_map_write(struct shm_du_map * dum, sdb = get_head_ptr(dum); sdb->size = 0; sdb->blocks = padblocks; - sdb->dst_api = 0; + sdb->dst_api = -1; sdb->du_head = 0; sdb->du_tail = 0; @@ -518,7 +518,7 @@ int shm_du_map_remove(struct shm_du_map * dum, ssize_t idx) return -1; } - idx_to_du_buff_ptr(dum, idx)->dst_api = 0; + idx_to_du_buff_ptr(dum, idx)->dst_api = -1; if (idx != *dum->ptr_tail) { pthread_mutex_unlock(dum->shm_mutex); -- cgit v1.2.3