-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from alessandrocarminati/main
Add manual scsi probe of a Lun
- Loading branch information
Showing
15 changed files
with
362 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
CC=gcc | ||
CFLAGS= -g | ||
#QUIET = &>/dev/null | ||
VALGRIND_FLAGS= --error-exitcode=1 --leak-check=full -s | ||
|
||
tests: tests_res/parse_kernel_cmdline.exec.log tests_res/parse_kernel_cmdline.valgrind.log tests_res/fetch_kernel_cmdline.exec.log tests_res/fetch_kernel_cmdline.valgrind.log tests_res/trigger_scan.valgrind.log tests_res/trigger_scan.exec.log | ||
|
||
################################################################ | ||
tests_res/parse_kernel_cmdline.exec.log: tests/parse_kernel_cmdline | ||
@echo "======================= Testing... " $< " exec" | ||
@$< > $@ | ||
|
||
tests_res/parse_kernel_cmdline.valgrind.log: tests/parse_kernel_cmdline | ||
@echo "======================= Testing... " $< "valgrind" | ||
@valgrind $(VALGRIND_FLAGS) $< >$@ 2>&1 | ||
|
||
tests/parse_kernel_cmdline: tests/parse_kernel_cmdline.c scsi_probe.o | ||
@echo "======================= Build... " $< | ||
@$(CC) $(CFLAGS) $^ -o $@ | ||
|
||
########################################################################### | ||
tests_res/fetch_kernel_cmdline.exec.log: tests/fetch_kernel_cmdline | ||
@echo "======================= Testing... " $< " exec" | ||
@$< > $@ | ||
|
||
tests_res/fetch_kernel_cmdline.valgrind.log: tests/fetch_kernel_cmdline | ||
@echo "======================= Testing... " $< "valgrind" | ||
@valgrind $(VALGRIND_FLAGS) $< >$@ 2>&1 | ||
|
||
tests/fetch_kernel_cmdline: tests/fetch_kernel_cmdline.c scsi_probe.o | ||
@echo "======================= Build... " $< | ||
@$(CC) $(CFLAGS) $^ -o $@ | ||
|
||
########################################################################### | ||
tests_res/trigger_scan.exec.log: tests/trigger_scan | ||
@echo "======================= Testing... " $< " exec" | ||
@$< > $@ | ||
|
||
tests_res/trigger_scan.valgrind.log: tests/trigger_scan | ||
@echo "======================= Testing... " $< "valgrind" | ||
@valgrind $(VALGRIND_FLAGS) $< >$@ 2>&1 | ||
|
||
tests/trigger_scan: tests/trigger_scan.c scsi_probe.o | ||
@echo "======================= Build... " $< | ||
@$(CC) $(CFLAGS) $^ -o $@ | ||
|
||
########################################################################### | ||
scsi_probe.o: scsi_probe.c scsi_probe.h | ||
@echo "======================= Build... " $< | ||
@$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
clean: | ||
@echo "======================= CLEAN... " | ||
@rm -f scsi_probe.o tests/parse_kernel_cmdline tests_res/*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
#include "scsi_probe.h" | ||
|
||
static int c2i(char *str){ | ||
char *endptr; | ||
int num; | ||
|
||
errno = 0; | ||
num = strtol(str, &endptr, 10); | ||
if ((errno != 0 && num == 0) || (num > 99)) return -1; | ||
if (endptr == str) return -1; | ||
return num; | ||
} | ||
|
||
inline int update_scsi_args(const char *ba_str, struct args *ba){ | ||
char *token; | ||
char *ba_str2; | ||
char *tmp1, *tmp2; | ||
|
||
if (!ba_str) return 0; | ||
if ((ba_str2 = strdup(ba_str)) == NULL) return 0; | ||
token = strtok(ba_str2, " "); | ||
while (token != NULL) { | ||
if (strncmp(token, SCSI_ADDR_BOOT_ARG, strlen(SCSI_ADDR_BOOT_ARG)) == 0) { | ||
tmp1 = strchr(token, '='); | ||
if (tmp1 != NULL) { | ||
tmp2 = tmp1 + 1; | ||
sscanf(tmp2, "%d:%d:%d:%d", &ba->scsi_host, &ba->scsi_channel, &ba->scsi_id, &ba->scsi_lun); | ||
} | ||
} | ||
token = strtok(NULL, " "); | ||
} | ||
free(ba_str2); | ||
return 1; | ||
} | ||
|
||
|
||
int parse_kernel_cmdline(const char *ba_str, struct args *ba){ | ||
memset(ba, 0, sizeof(struct args)); | ||
ba->scsi_manual=strstr(ba_str, "scsi_mod.scan=manual")?1:0; | ||
return update_scsi_args(ba_str, ba); | ||
} | ||
|
||
char *fetch_kernel_cmdline(const char *cmdline_fn){ | ||
FILE *file; | ||
char *cmdline; | ||
|
||
file = fopen(cmdline_fn, "r"); | ||
if (file == NULL) return NULL; | ||
cmdline = (char *) malloc(MAX_BUF); | ||
if (! fgets(cmdline, MAX_BUF, file)) { | ||
free(cmdline); | ||
return NULL; | ||
} | ||
fclose(file); | ||
return cmdline; | ||
} | ||
|
||
|
||
int trigger_scan(struct args *ba, const char *scsi_sys_tmpl, const int scsi_sys_tmpl_sz) { | ||
FILE *file; | ||
char buf[128]; | ||
int n; | ||
|
||
if (ba->scsi_manual==0) return 0; | ||
snprintf(buf, scsi_sys_tmpl_sz, scsi_sys_tmpl, ba->scsi_host); | ||
file = fopen(buf, "w"); | ||
if (file == NULL) return 0; | ||
n = snprintf(buf, MAX_BUF, SCSI_SYS_SCAN_STR, ba->scsi_channel, ba->scsi_id, ba->scsi_lun); | ||
fwrite(buf, n, 1, file); | ||
fclose(file); | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#define MAX_ARGS 100 | ||
#define MAX_BUF 128 | ||
|
||
#define SCSI_SYS_SCAN_STR "%d %d %d\n" | ||
|
||
#define SCSI_ADDR_BOOT_ARG "scsi.addr" | ||
|
||
struct args { | ||
int scsi_manual; | ||
int scsi_host; | ||
int scsi_channel; | ||
int scsi_id; | ||
int scsi_lun; | ||
}; | ||
|
||
int update_scsi_args(const char *, struct args *); | ||
int parse_kernel_cmdline(const char *, struct args *); | ||
char *fetch_kernel_cmdline(const char *); | ||
int trigger_scan(struct args *, const char *, const int); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pippo=1 pluto=2 paperino=3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pippo=1 pluto=2 paperino=3 | ||
pippo=z pluto=x paperino=c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 2 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3 4 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include "../scsi_probe.h" | ||
|
||
#define TEST_BUFF_SIZE 200 | ||
|
||
char *results[] = { | ||
"pippo=1 pluto=2 paperino=3\n", | ||
"pippo=1 pluto=2 paperino=3\n", | ||
NULL | ||
}; | ||
|
||
char *test_patterns[] = { | ||
"testfiles/cmd1", | ||
"testfiles/cmd2", | ||
"testfiles/cmd3", | ||
}; | ||
|
||
int main(){ | ||
int i; | ||
char *res; | ||
|
||
for (i=0; i< sizeof(test_patterns)/sizeof(char *); i++) { | ||
res = fetch_kernel_cmdline(test_patterns[i]); | ||
printf( "Test pattern='%s', expected result='%s' Actual result ='%s' -> ", | ||
test_patterns[i], results[i], res); | ||
if (res && results[i]) { | ||
if (strcmp(res, results[i])!=0) { | ||
printf("Failed\n"); | ||
free(res); | ||
return -1; | ||
} | ||
} | ||
printf("Success\n"); | ||
free(res); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include "../scsi_probe.h" | ||
|
||
#define TEST_BUFF_SIZE 200 | ||
|
||
char *results[] = { | ||
"has manual=1 host=1 addr=2:3:45", | ||
"has manual=1 host=1 addr=2:3:45", | ||
"has manual=1 host=0 addr=0:0:0", | ||
"has manual=0 host=0 addr=0:0:0" | ||
}; | ||
|
||
char *test_patterns[] = { | ||
"pippo=1 pluto=2 scsi_mod.scan=manual scsi.addr=1:2:3:45 paperino=peppe", | ||
"pippo=1 pluto=2 scsi_mod.scan=manual posw scsi.addr=1:2:3:45 paperino=peppe", | ||
"pippo=1 pluto=2 scsi_mod.scan=manual paperino=peppe", | ||
"pippo=1 pluto=2 paperino=peppe", | ||
|
||
}; | ||
|
||
int main(){ | ||
struct args ba; | ||
int i; | ||
char res[TEST_BUFF_SIZE]; | ||
|
||
for (i=0; i< sizeof(test_patterns)/sizeof(char *); i++) { | ||
if (!parse_kernel_cmdline(test_patterns[i], &ba)) return -1; | ||
sprintf(res, "has manual=%d host=%d addr=%d:%d:%d", ba.scsi_manual, ba.scsi_host, ba.scsi_channel, ba.scsi_id, ba.scsi_lun); | ||
|
||
printf( "Test pattern='%s', expected result='%s' Actual result ='%s' -> ", | ||
test_patterns[i], results[i], res); | ||
if (strcmp(res, results[i])) { | ||
printf("Failed\n"); | ||
return -1; | ||
} | ||
printf("Success\n"); | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.