Hi All,

While testing I noticed that idbm_lock() uses exit when it cannot lock, leading 
to interesting effect when using it from libiscsi, when typing "import 
libiscsi" in python as normal user, my entire python interpreter exited, not 
good.

The attached patch instead returns an error code, and fixes all callers to 
check this.

Regards,

Hans

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"open-iscsi" group.
To post to this group, send email to open-iscsi@googlegroups.com
To unsubscribe from this group, send email to 
open-iscsi+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/open-iscsi
-~----------~----~----~----~------~----~------~--~---

--- open-iscsi-2.0-870.1/usr/idbm.c~    2009-01-28 13:23:47.000000000 +0100
+++ open-iscsi-2.0-870.1/usr/idbm.c     2009-01-28 13:25:06.000000000 +0100
@@ -843,7 +843,7 @@ int idbm_lock(void)
        if (access(LOCK_DIR, F_OK) != 0) {
                if (mkdir(LOCK_DIR, 0660) != 0) {
                        log_error("Could not open %s. Exiting\n", LOCK_DIR);
-                       exit(-1);
+                       return errno;
                }
        }
 
@@ -857,10 +857,10 @@ int idbm_lock(void)
                        break;
 
                if (errno != EEXIST) {
+                       log_error("Maybe you are not root?");
                        log_error("Could not lock discovery DB: %s: %s",
                                        LOCK_WRITE_FILE, strerror(errno));
-                       log_error("Maybe you are not root?");
-                       exit(-1);
+                       return errno;
                } else if (i == 0)
                        log_debug(2, "Waiting for discovery DB lock");
 
@@ -915,7 +915,10 @@ static int __idbm_rec_read(node_rec_t *o
        if (!info)
                return ENOMEM;
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto free_info;
+
        f = fopen(conf, "r");
        if (!f) {
                log_debug(5, "Could not open %s err %d\n", conf, errno);
@@ -931,6 +934,7 @@ static int __idbm_rec_read(node_rec_t *o
 
 unlock:
        idbm_unlock();
+free_info:
        free(info);
        return rc;
 }
@@ -1386,14 +1390,18 @@ idbm_discovery_read(discovery_rec_t *out
                return ENOMEM;
 
        portal = malloc(PATH_MAX);
-       if (!portal)
+       if (!portal) {
+               rc = ENOMEM;
                goto free_info;
+       }
 
        snprintf(portal, PATH_MAX, "%s/%s,%d", ST_CONFIG_DIR,
                 addr, port);
        log_debug(5, "Looking for config file %s\n", portal);
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto free_info;
 
        f = idbm_open_rec_r(portal, ST_CONFIG_NAME);
        if (!f) {
@@ -1494,7 +1502,9 @@ static int idbm_rec_write(node_rec_t *re
                 rec->name, rec->conn[0].address, rec->conn[0].port);
        log_debug(5, "Looking for config file %s", portal);
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto free_portal;
 
        rc = stat(portal, &statb);
        if (rc) {
@@ -1579,13 +1589,16 @@ idbm_discovery_write(discovery_rec_t *re
                return ENOMEM;
        }
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto free_portal;
+
        snprintf(portal, PATH_MAX, "%s", ST_CONFIG_DIR);
        if (access(portal, F_OK) != 0) {
                if (mkdir(portal, 0660) != 0) {
                        log_error("Could not make %s\n", portal);
                        rc = errno;
-                       goto free_portal;
+                       goto unlock;
                }
        }
 
@@ -1596,13 +1609,14 @@ idbm_discovery_write(discovery_rec_t *re
        if (!f) {
                log_error("Could not open %s err %d\n", portal, errno);
                rc = errno;
-               goto free_portal;
+               goto unlock;
        }
 
        idbm_print(IDBM_PRINT_TYPE_DISCOVERY, rec, 1, f);
        fclose(f);
-free_portal:
+unlock:
        idbm_unlock();
+free_portal:
        free(portal);
        return rc;
 }
@@ -1722,7 +1736,10 @@ int idbm_add_node(node_rec_t *newrec, di
        log_debug(7, "node addition making link from %s to %s", node_portal,
                 disc_portal);
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto free_portal;
+
        if (symlink(node_portal, disc_portal)) {
                if (errno == EEXIST)
                        log_debug(7, "link from %s to %s exists", node_portal,
@@ -2009,7 +2026,10 @@ static int idbm_remove_disc_to_node_link
        if (rc)
                goto done;
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto done;
+
        if (!stat(portal, &statb)) {
                if (unlink(portal)) {
                        log_error("Could not remove link %s err %d\n",
@@ -2046,7 +2066,10 @@ int idbm_delete_node(node_rec_t *rec)
        log_debug(5, "Removing config file %s iface id %s\n",
                  portal, rec->iface.name);
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto free_portal;
+
        if (!stat(portal, &statb))
                goto rm_conf;
 
diff -up open-iscsi-2.0-870.1/usr/iface.c~ open-iscsi-2.0-870.1/usr/iface.c
--- open-iscsi-2.0-870.1/usr/iface.c~   2009-01-28 13:29:31.000000000 +0100
+++ open-iscsi-2.0-870.1/usr/iface.c    2009-01-28 13:29:31.000000000 +0100
@@ -208,7 +208,10 @@ int iface_conf_read(struct iface_rec *if
                return 0;
        }
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               return rc;
+
        rc = __iface_conf_read(iface);
        idbm_unlock();
        return rc;
@@ -232,11 +235,15 @@ int iface_conf_delete(struct iface_rec *
                return ENOMEM;
 
        sprintf(iface_conf, "%s/%s", IFACE_CONFIG_DIR, iface->name);
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto free_conf;
+
        if (unlink(iface_conf))
                rc = errno;
        idbm_unlock();
 
+free_conf:
        free(iface_conf);
        return rc;
 }
@@ -267,10 +274,14 @@ int iface_conf_write(struct iface_rec *i
                goto free_conf;
        }
 
-       idbm_lock();
+       rc = idbm_lock();
+       if (rc)
+               goto close_f;
+
        idbm_print(IDBM_PRINT_TYPE_IFACE, iface, 1, f);
        idbm_unlock();
 
+close_f:
        fclose(f);
 free_conf:
        free(iface_conf);
@@ -471,7 +482,9 @@ void iface_setup_host_bindings(void)
 {
        int nr_found = 0;
 
-       idbm_lock();
+       if (idbm_lock())
+               return;
+
        if (access(IFACE_CONFIG_DIR, F_OK) != 0) {
                if (mkdir(IFACE_CONFIG_DIR, 0660) != 0) {
                        log_error("Could not make %s. HW/OFFLOAD iscsi "
@@ -658,7 +671,12 @@ int iface_for_each_iface(void *data, int
                        continue;
                }
 
-               idbm_lock();
+               err = idbm_lock();
+               if (err) {
+                       free(iface);
+                       continue;
+               }
+
                err = __iface_conf_read(iface);
                idbm_unlock();
                if (err) {

Reply via email to