stinit: Change stinit to return one if setting one or more options fails

Previously, stinit returned zero even if it fails to set the options.
This patch changes the return value to one if one or more of the options
can't be set for a device if the tape numbers are given on command line.
If no numbers are given, stinit returns zero (as before this patch).
After this change, scripts (udev rules) can more easily detect when
stinit fails.

Signed-off-by: Kai Mäkisara <Kai.Makisara@kolumbus.fi>
This commit is contained in:
Kai Mäkisara
2024-12-18 19:41:51 +02:00
committed by Iustin Pop
parent ff108770be
commit 42dd44bb90
3 changed files with 30 additions and 15 deletions

View File

@@ -263,10 +263,9 @@ The long timeout for the device is set to
seconds. seconds.
.SH RETURN VALUE .SH RETURN VALUE
The program exits with value one if the command line is incorrect, the The program exits with value one if the command line is incorrect, the
definition file is not found, or option -p is given and parsing the definition file is not found, option -p is given and parsing the
definition file fails. In all other cases the return value is definition file fails, or defining one or more of the options fails
zero (i.e., failing of initialization is not currently signaled by when the tape number(s) are given on command line.
the return value).
.SH RESTRICTIONS .SH RESTRICTIONS
With the exception of the -p option, the program can be used only by With the exception of the -p option, the program can be used only by
the superuser. This is because the program uses ioctls allowed only the superuser. This is because the program uses ioctls allowed only

View File

@@ -681,11 +681,11 @@ static int find_devfiles(int tapeno, char **names)
static int set_defs(devdef_tr *defs, char **fnames) static int set_defs(devdef_tr *defs, char **fnames)
{ {
int i, tape; int i, tape, fails;
int clear_set[2]; int clear_set[2];
struct mtop op; struct mtop op;
for (i = 0; i < NBR_MODES; i++) { for (i = fails = 0; i < NBR_MODES; i++) {
if (*fnames[i] == '\0' || !defs->modedefs[i].defined) if (*fnames[i] == '\0' || !defs->modedefs[i].defined)
continue; continue;
@@ -699,14 +699,19 @@ static int set_defs(devdef_tr *defs, char **fnames)
if (defs->do_rewind) { if (defs->do_rewind) {
op.mt_op = MTREW; op.mt_op = MTREW;
op.mt_count = 1; op.mt_count = 1;
ioctl(tape, MTIOCTOP, &op); /* Don't worry about result */ if (ioctl(tape, MTIOCTOP, &op) != 0) {
fails++;
fprintf(stderr, "Rewind of %s fails.\n", fnames[i]);
}
} }
if (defs->drive_buffering >= 0) { if (defs->drive_buffering >= 0) {
op.mt_op = MTSETDRVBUFFER; op.mt_op = MTSETDRVBUFFER;
op.mt_count = MT_ST_DEF_DRVBUFFER | defs->drive_buffering; op.mt_count = MT_ST_DEF_DRVBUFFER | defs->drive_buffering;
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fprintf(stderr, "Can't set drive buffering to %d.\n", defs->drive_buffering); fails++;
fprintf(stderr, "Can't set drive buffering to %d.\n",
defs->drive_buffering);
} }
} }
@@ -714,7 +719,9 @@ static int set_defs(devdef_tr *defs, char **fnames)
op.mt_op = MTSETDRVBUFFER; op.mt_op = MTSETDRVBUFFER;
op.mt_count = MT_ST_SET_TIMEOUT | defs->timeout; op.mt_count = MT_ST_SET_TIMEOUT | defs->timeout;
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fprintf(stderr, "Can't set device timeout %d s.\n", defs->timeout); fails++;
fprintf(stderr, "Can't set device timeout %d s.\n",
defs->timeout);
} }
} }
@@ -722,6 +729,7 @@ static int set_defs(devdef_tr *defs, char **fnames)
op.mt_op = MTSETDRVBUFFER; op.mt_op = MTSETDRVBUFFER;
op.mt_count = MT_ST_SET_LONG_TIMEOUT | defs->long_timeout; op.mt_count = MT_ST_SET_LONG_TIMEOUT | defs->long_timeout;
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fails++;
fprintf(stderr, "Can't set device long timeout %d s.\n", fprintf(stderr, "Can't set device long timeout %d s.\n",
defs->long_timeout); defs->long_timeout);
} }
@@ -731,6 +739,7 @@ static int set_defs(devdef_tr *defs, char **fnames)
op.mt_op = MTSETDRVBUFFER; op.mt_op = MTSETDRVBUFFER;
op.mt_count = MT_ST_SET_CLN | defs->cleaning; op.mt_count = MT_ST_SET_CLN | defs->cleaning;
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fails++;
fprintf(stderr, "Can't set cleaning request parameter to %x\n", fprintf(stderr, "Can't set cleaning request parameter to %x\n",
defs->cleaning); defs->cleaning);
} }
@@ -774,6 +783,7 @@ static int set_defs(devdef_tr *defs, char **fnames)
if (clear_set[0] != 0) { if (clear_set[0] != 0) {
op.mt_count = MT_ST_CLEARBOOLEANS | clear_set[0]; op.mt_count = MT_ST_CLEARBOOLEANS | clear_set[0];
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fails++;
fprintf(stderr, "Can't clear the tape options (bits 0x%x, mode %d).\n", fprintf(stderr, "Can't clear the tape options (bits 0x%x, mode %d).\n",
clear_set[0], i); clear_set[0], i);
} }
@@ -781,6 +791,7 @@ static int set_defs(devdef_tr *defs, char **fnames)
if (clear_set[1] != 0) { if (clear_set[1] != 0) {
op.mt_count = MT_ST_SETBOOLEANS | clear_set[1]; op.mt_count = MT_ST_SETBOOLEANS | clear_set[1];
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fails++;
fprintf(stderr, "Can't set the tape options (bits 0x%x, mode %d).\n", fprintf(stderr, "Can't set the tape options (bits 0x%x, mode %d).\n",
clear_set[1], i); clear_set[1], i);
} }
@@ -789,6 +800,7 @@ static int set_defs(devdef_tr *defs, char **fnames)
if (defs->modedefs[i].blocksize >= 0) { if (defs->modedefs[i].blocksize >= 0) {
op.mt_count = MT_ST_DEF_BLKSIZE | defs->modedefs[i].blocksize; op.mt_count = MT_ST_DEF_BLKSIZE | defs->modedefs[i].blocksize;
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fails++;
fprintf(stderr, "Can't set blocksize %d for mode %d.\n", fprintf(stderr, "Can't set blocksize %d for mode %d.\n",
defs->modedefs[i].blocksize, i); defs->modedefs[i].blocksize, i);
} }
@@ -796,6 +808,7 @@ static int set_defs(devdef_tr *defs, char **fnames)
if (defs->modedefs[i].density >= 0) { if (defs->modedefs[i].density >= 0) {
op.mt_count = MT_ST_DEF_DENSITY | defs->modedefs[i].density; op.mt_count = MT_ST_DEF_DENSITY | defs->modedefs[i].density;
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fails++;
fprintf(stderr, "Can't set density %x for mode %d.\n", fprintf(stderr, "Can't set density %x for mode %d.\n",
defs->modedefs[i].density, i); defs->modedefs[i].density, i);
} }
@@ -803,6 +816,7 @@ static int set_defs(devdef_tr *defs, char **fnames)
if (defs->modedefs[i].compression >= 0) { if (defs->modedefs[i].compression >= 0) {
op.mt_count = MT_ST_DEF_COMPRESSION | defs->modedefs[i].compression; op.mt_count = MT_ST_DEF_COMPRESSION | defs->modedefs[i].compression;
if (ioctl(tape, MTIOCTOP, &op) != 0) { if (ioctl(tape, MTIOCTOP, &op) != 0) {
fails++;
fprintf(stderr, "Can't set compression %d for mode %d.\n", fprintf(stderr, "Can't set compression %d for mode %d.\n",
defs->modedefs[i].compression, i); defs->modedefs[i].compression, i);
} }
@@ -810,7 +824,7 @@ static int set_defs(devdef_tr *defs, char **fnames)
close(tape); close(tape);
} }
return TRUE; return (fails == 0);
} }
@@ -876,7 +890,7 @@ static char usage(int retval)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
FILE *dbf = NULL; FILE *dbf = NULL;
int argn; int argn, retval = 0;
int tapeno, parse_only = FALSE; int tapeno, parse_only = FALSE;
char *dbname = NULL; char *dbname = NULL;
char *convp; char *convp;
@@ -933,8 +947,10 @@ int main(int argc, char **argv)
fprintf(stderr, "Can't find tape number for name '%s'.\n", argv[argn]); fprintf(stderr, "Can't find tape number for name '%s'.\n", argv[argn]);
continue; continue;
} }
if (!define_tape(tapeno, dbf, &defs, TRUE)) if (!define_tape(tapeno, dbf, &defs, TRUE)) {
fprintf(stderr, "Definition for '%s' failed.\n", argv[argn]); fprintf(stderr, "Definition for '%s' failed.\n", argv[argn]);
retval = 1;
}
} }
} else { /* Initialize all SCSI tapes */ } else { /* Initialize all SCSI tapes */
for (tapeno = 0; tapeno < MAX_TAPES; tapeno++) for (tapeno = 0; tapeno < MAX_TAPES; tapeno++)
@@ -945,5 +961,5 @@ int main(int argc, char **argv)
} }
} }
return 0; return retval;
} }

View File

@@ -39,11 +39,11 @@
# 1000 tapes. # 1000 tapes.
./stinit -f stinit.def.examples 1000 ./stinit -f stinit.def.examples 1000
>>>2 /Can't find any device files for tape 1000/ >>>2 /Can't find any device files for tape 1000/
>>>= 0 >>>= 1
./stinit -f stinit.def.examples 1000 ./stinit -f stinit.def.examples 1000
>>>2 /Definition for '1000' failed/ >>>2 /Definition for '1000' failed/
>>>= 0 >>>= 1
# Wrong tape number (non-numeric). # Wrong tape number (non-numeric).
./stinit -f stinit.def.examples 1000a ./stinit -f stinit.def.examples 1000a