diff --git a/iscsi-scst/usr/ctldev.c b/iscsi-scst/usr/ctldev.c index 303cca650..8fed2e285 100644 --- a/iscsi-scst/usr/ctldev.c +++ b/iscsi-scst/usr/ctldev.c @@ -28,59 +28,17 @@ #include "iscsid.h" -#define CTL_DEVICE "/dev/iscsi-scst-ctl" +#define CTL_DEVICE "iscsi-scst-ctl" int kernel_open(void) { - FILE *f; - char devname[256]; - char buf[256]; - int devn; int ctlfd = -1; int err; struct iscsi_kern_register_info reg; - if (!(f = fopen("/proc/devices", "r"))) { - err = -errno; - perror("Cannot open control path to the driver"); - goto out_err; - } - - devn = 0; - while (!feof(f)) { - if (!fgets(buf, sizeof(buf), f)) { - break; - } - if (sscanf(buf, "%d %s", &devn, devname) != 2) { - continue; - } - if (!strcmp(devname, "iscsi-scst-ctl")) { - break; - } - devn = 0; - } - - fclose(f); - if (!devn) { - err = -ENOENT; - printf("cannot find iscsictl in /proc/devices - " - "make sure the module is loaded\n"); - goto out_err; - } - - unlink(CTL_DEVICE); - if (mknod(CTL_DEVICE, (S_IFCHR | 0600), (devn << 8))) { - err = -errno; - printf("cannot create %s %s\n", CTL_DEVICE, strerror(errno)); - goto out_err; - } - - ctlfd = open(CTL_DEVICE, O_RDWR); - if (ctlfd < 0) { - err = -errno; - printf("cannot open %s %s\n", CTL_DEVICE, strerror(errno)); - goto out_err; - } + ctlfd = create_and_open_dev(CTL_DEVICE, 0); + if (ctlfd < 0) + goto out; memset(®, 0, sizeof(reg)); reg.version = (uintptr_t)ISCSI_SCST_INTERFACE_VERSION; @@ -104,7 +62,6 @@ out: out_close: close(ctlfd); -out_err: ctlfd = err; goto out; } diff --git a/iscsi-scst/usr/iscsi_scstd.c b/iscsi-scst/usr/iscsi_scstd.c index 5425c4501..00a03355c 100644 --- a/iscsi-scst/usr/iscsi_scstd.c +++ b/iscsi-scst/usr/iscsi_scstd.c @@ -170,6 +170,39 @@ static void create_listen_socket(struct pollfd *array) exit(1); } +static struct connection *alloc_and_init_conn(int fd) +{ + struct pollfd *pollfd; + struct connection *conn = NULL; + int i; + + for (i = 0; i < INCOMING_MAX; i++) { + if (!incoming[i]) + break; + } + if (i >= INCOMING_MAX) { + log_error("Unable to find incoming slot? %d\n", i); + goto out; + } + + conn = conn_alloc(); + if (!conn) { + log_error("Fail to allocate %s", "conn\n"); + goto out; + } + + conn->fd = fd; + incoming[i] = conn; + + pollfd = &poll_array[POLL_INCOMING + i]; + pollfd->fd = fd; + pollfd->events = POLLIN; + pollfd->revents = 0; + +out: + return conn; +} + static int transmit_sock(int fd, bool start) { int opt = start; @@ -184,9 +217,8 @@ static void accept_connection(int listen) struct sockaddr_in6 sin6; } from, to; socklen_t namesize; - struct pollfd *pollfd; struct connection *conn; - int fd, i, rc; + int fd, rc; char initiator_addr[ISCSI_PORTAL_LEN], initiator_port[NI_MAXSERV]; char target_portal[ISCSI_PORTAL_LEN], target_portal_port[NI_MAXSERV]; @@ -245,36 +277,20 @@ static void accept_connection(int listen) goto out_close; } - for (i = 0; i < INCOMING_MAX; i++) { - if (!incoming[i]) - break; - } - if (i >= INCOMING_MAX) { - log_error("Unable to find incoming slot? %d\n", i); + conn = alloc_and_init_conn(fd); + if (!conn) goto out_close; - } - if (!(conn = conn_alloc())) { - log_error("Fail to allocate %s", "conn\n"); - goto out_close; - } - - conn->fd = fd; conn->target_portal = strdup(target_portal); if (conn->target_portal == NULL) { log_error("Unable to duplicate target portal %s", target_portal); goto out_free; } - incoming[i] = conn; conn->transmit = transmit_sock; conn_read_pdu(conn); set_non_blocking(fd); - pollfd = &poll_array[POLL_INCOMING + i]; - pollfd->fd = fd; - pollfd->events = POLLIN; - pollfd->revents = 0; incoming_cnt++; diff --git a/iscsi-scst/usr/misc.c b/iscsi-scst/usr/misc.c index ae3210663..beb39ffdd 100644 --- a/iscsi-scst/usr/misc.c +++ b/iscsi-scst/usr/misc.c @@ -18,9 +18,74 @@ #include #include #include +#include +#include +#include +#include #include "iscsid.h" +int create_and_open_dev(const char *dev, int readonly) +{ + FILE *f; + char devname[256]; + char buf[256]; + int devn; + int ctlfd = -1; + int err; + int flags; + + f = fopen("/proc/devices", "r"); + if (!f) { + err = -errno; + perror("Cannot open control path to the driver"); + goto out; + } + + devn = 0; + while (!feof(f)) { + if (!fgets(buf, sizeof(buf), f)) + break; + if (sscanf(buf, "%d %s", &devn, devname) != 2) + continue; + if (!strcmp(devname, dev)) + break; + devn = 0; + } + + fclose(f); + if (!devn) { + err = -ENOENT; + printf("cannot find %s in /proc/devices - " + "make sure the module is loaded\n", dev); + goto out; + } + + sprintf(devname, "/dev/%s", dev); + + unlink(devname); + if (mknod(devname, (S_IFCHR | 0600), (devn << 8))) { + err = -errno; + printf("cannot create %s %s\n", devname, strerror(errno)); + goto out; + } + + if (readonly) + flags = O_RDONLY; + else + flags = O_RDWR; + + err = ctlfd = open(devname, flags); + if (ctlfd < 0) { + err = -errno; + printf("cannot open %s %s\n", devname, strerror(errno)); + goto out; + } + +out: + return err; +} + void set_non_blocking(int fd) { int res = fcntl(fd, F_GETFL); diff --git a/iscsi-scst/usr/misc.h b/iscsi-scst/usr/misc.h index 75d2a1583..127332d66 100644 --- a/iscsi-scst/usr/misc.h +++ b/iscsi-scst/usr/misc.h @@ -108,5 +108,6 @@ static inline int list_length_is_one(const struct __qelem *head) extern void set_non_blocking(int fd); extern void sock_set_keepalive(int sock, int timeout); +extern int create_and_open_dev(const char *dev, int readonly); #endif