📄 iscsi-device.c
字号:
/* * iSCSI driver for Linux * Copyright (C) 2001 Cisco Systems, Inc. * maintained by linux-iscsi-devel@lists.sourceforge.net * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * See the file COPYING included with this distribution for more details. * * $Id: iscsi-device.c,v 1.4 2004/07/07 09:45:10 surekhap Exp $ * * Determine if a Linux SCSI device is an iSCSI device. * */#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <errno.h>#include <string.h>#include <inttypes.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <errno.h>#include <ctype.h>/* <asm/types.h> does not export u8 definition outside kernel * forcing us to define it here. */typedef unsigned char u8;#include <scsi/scsi.h>#define ISCSI_SYS_DIR "/sys/class/scsi_host/"/* if the device name on the command line exists and is an iSCSI device, * then output a description of the iSCSI target to stdout, and exit 0. * if the device name might be an iSCSI device, exit 1. * if the device name definately is not an iSCSI device, exit 2. */intmain(int argc, char *argv[]){ FILE *fp; char devname[PATH_MAX + 1]; int host, channel, id, lun; char *device = argv[1]; char name[20]; int fd; u_long arg[2]; if (argc > 2) { fprintf(stderr, "iscsi-device: error, too many arguments," " only 1 device name is allowed.\n"); } if (argc != 2) { fprintf(stderr, "\nUsage: iscsi-device devicename\n\n"); exit(2); } /* get the (host,channel,id,lun) quadruple for this device, if possible */ if ((fd = open(device, O_RDONLY)) >= 0) { memset(&arg, 0, sizeof(arg)); /* try to get the host, channel, id, lun quadruple */ if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &arg) == 0) { id = arg[0] & 0xff; lun = (arg[0] >> 8) & 0xff; channel = (arg[0] >> 16) & 0xff; host = (arg[0] >> 24) & 0xff; } else { fprintf(stderr, "iscsi-device: ioctl SCSI_IOCTL_GET_IDLUN " "%s failed, %s\n", device, strerror(errno)); close(fd); exit(1); } } else { fprintf(stderr, "iscsi-device: couldn't open %s, %s\n", device, strerror(errno)); exit(1); } close(fd); sprintf(devname, "%s/host%d/proc_name", ISCSI_SYS_DIR, host); if ((fp = fopen(devname, "r"))) { fscanf(fp, "%s", name); fclose(fp); if (!strcmp(name, "iscsi-sfnet")) { printf("%s is an iSCSI device \n", device); exit(0); } } /* didn't find a match */ fprintf(stderr, "iscsi-device: %s is not an iSCSI device\n", device); exit(2);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -