Index: usr.bin/iscsictl/iscsictl.8 =================================================================== --- usr.bin/iscsictl/iscsictl.8 +++ usr.bin/iscsictl/iscsictl.8 @@ -36,7 +36,9 @@ .Sh SYNOPSIS .Nm .Fl A -.Fl p Ar portal Fl t Ar target Op Fl u Ar user Fl s Ar secret +.Fl p Ar portal Fl t Ar target +.Op Fl u Ar user Fl s Ar secret +.Op Fl w Ar timeout .Nm .Fl A .Fl d Ar discovery-host Op Fl u Ar user Fl s Ar secret @@ -70,6 +72,7 @@ .Nm .Fl L .Op Fl v +.Op Fl w Ar timeout .Sh DESCRIPTION The .Nm @@ -113,6 +116,10 @@ CHAP login. .It Fl v Verbose mode. +.It Fl w +Instead of returning immediately, wait up to +.Ar timeout +seconds until all configured sessions are successfully established. .El .Pp Certain parameters are necessary when adding a session. @@ -132,9 +139,11 @@ .Pp Since connecting to the target is performed in background, non-zero exit status does not mean that the session was successfully established. -Use +Use either .Nm Fl L -to check the connection status. +to check the connection status, or the +.Fl w +flag to wait for session establishment. .Pp Note that in order for the iSCSI initiator to be able to connect to a target, the Index: usr.bin/iscsictl/iscsictl.c =================================================================== --- usr.bin/iscsictl/iscsictl.c +++ usr.bin/iscsictl/iscsictl.c @@ -592,12 +592,70 @@ return (0); } +static int +kernel_wait(int iscsi_fd, int timeout) +{ + struct iscsi_session_state *states = NULL; + const struct iscsi_session_state *state; + const struct iscsi_session_conf *conf; + struct iscsi_session_list isl; + unsigned int i, nentries = 1; + bool all_connected; + int error; + + for (;;) { + for (;;) { + states = realloc(states, + nentries * sizeof(struct iscsi_session_state)); + if (states == NULL) + xo_err(1, "realloc"); + + memset(&isl, 0, sizeof(isl)); + isl.isl_nentries = nentries; + isl.isl_pstates = states; + + error = ioctl(iscsi_fd, ISCSISLIST, &isl); + if (error != 0 && errno == EMSGSIZE) { + nentries *= 4; + continue; + } + break; + } + if (error != 0) { + xo_warn("ISCSISLIST"); + return (error); + } + + all_connected = true; + for (i = 0; i < isl.isl_nentries; i++) { + state = &states[i]; + conf = &state->iss_conf; + + if (!state->iss_connected) { + all_connected = false; + break; + } + } + + if (all_connected) + return (0); + + sleep(1); + + if (timeout > 0) { + timeout--; + if (timeout == 0) + return (1); + } + } +} + static void usage(void) { fprintf(stderr, "usage: iscsictl -A -p portal -t target " - "[-u user -s secret]\n"); + "[-u user -s secret] [-w timeout]\n"); fprintf(stderr, " iscsictl -A -d discovery-host " "[-u user -s secret]\n"); fprintf(stderr, " iscsictl -A -a [-c path]\n"); @@ -609,7 +667,7 @@ fprintf(stderr, " iscsictl -R [-p portal] [-t target]\n"); fprintf(stderr, " iscsictl -R -a\n"); fprintf(stderr, " iscsictl -R -n nickname [-c path]\n"); - fprintf(stderr, " iscsictl -L [-v]\n"); + fprintf(stderr, " iscsictl -L [-v] [-w timeout]\n"); exit(1); } @@ -631,6 +689,7 @@ const char *conf_path = DEFAULT_CONFIG_PATH; char *nickname = NULL, *discovery_host = NULL, *portal = NULL, *target = NULL, *user = NULL, *secret = NULL; + int timeout = -1; long long session_id = -1; char *end; int ch, error, iscsi_fd, retval, saved_errno; @@ -641,7 +700,7 @@ argc = xo_parse_args(argc, argv); xo_open_container("iscsictl"); - while ((ch = getopt(argc, argv, "AMRLac:d:i:n:p:t:u:s:v")) != -1) { + while ((ch = getopt(argc, argv, "AMRLac:d:i:n:p:t:u:s:vw:")) != -1) { switch (ch) { case 'A': Aflag = 1; @@ -692,6 +751,13 @@ case 'v': vflag = 1; break; + case 'w': + timeout = strtol(optarg, &end, 10); + if ((size_t)(end - optarg) != strlen(optarg)) + xo_errx(1, "trailing characters after timeout"); + if (timeout < 0) + xo_errx(1, "timeout cannot be negative"); + break; case '?': default: usage(); @@ -782,6 +848,8 @@ if (vflag != 0) xo_errx(1, "-v cannot be used with -M"); + if (timeout != -1) + xo_errx(1, "-w cannot be used with -M"); } else if (Rflag != 0) { if (user != NULL) @@ -811,6 +879,8 @@ xo_errx(1, "-i cannot be used with -R"); if (vflag != 0) xo_errx(1, "-v cannot be used with -R"); + if (timeout != -1) + xo_errx(1, "-w cannot be used with -R"); } else { assert(Lflag != 0); @@ -896,6 +966,9 @@ failed += kernel_list(iscsi_fd, targ, vflag); } + if (timeout != -1) + failed += kernel_wait(iscsi_fd, timeout); + error = close(iscsi_fd); if (error != 0) xo_err(1, "close");