Files
scst/iscsi-scst/usr/event.c
T
Vladislav Bolkhovitin 1213800b2f Patch from Bart Van Assche <bart.vanassche@gmail.com>, except few chuncks from qla2x00t/ obviously not related to the target mode addon:
One of the Linux kernel patch submission requirements is that source files do
not contain trailing whitespace. The patch below removes trailing whitespace
from .c and .h source files.

Note: it might be more convenient to run the script I used to generate this
patch than to review and apply the patch below. This is how I generated and
verified the patch below:

cat <<EOF >./strip-trailing-whitespace
#!/bin/bash
trap "rm -f $t" EXIT
t=/tmp/temporary-file.$$
for f in "$@"
do
  sed 's/[	 ]*$//' <"$f" >"$t" && mv "$t" "$f"
done
EOF
chmod a+x ./strip-trailing-whitespace
find -name '*.[ch]' | xargs ./strip-trailing-whitespace
svn diff -x -w

Signed-off-by: <bart.vanassche@gmail.com>



git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@377 d57e44dd-8a1f-0410-8b47-8ef2f437770f
2008-05-19 10:21:41 +00:00

144 lines
3.2 KiB
C

/*
* Event notification code.
*
* Copyright (C) 2005 FUJITA Tomonori <tomof@acm.org>
* Copyright (C) 2007 Vladislav Bolkhovitin
* Copyright (C) 2007 CMS Distribution Limited
*
* 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.
*
* 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.
*
* Some functions are based on open-iscsi code
* written by Dmitry Yusupov, Alex Aizman.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include "iscsid.h"
static struct sockaddr_nl src_addr, dest_addr;
static int nl_write(int fd, void *data, int len)
{
struct iovec iov[2];
struct msghdr msg;
struct nlmsghdr nlh;
iov[0].iov_base = &nlh;
iov[0].iov_len = sizeof(nlh);
iov[1].iov_base = data;
iov[1].iov_len = NLMSG_SPACE(len) - sizeof(nlh);
nlh.nlmsg_len = NLMSG_SPACE(len);
nlh.nlmsg_pid = getpid();
nlh.nlmsg_flags = 0;
nlh.nlmsg_type = 0;
memset(&msg, 0, sizeof(msg));
msg.msg_name= (void*)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = iov;
msg.msg_iovlen = 2;
return sendmsg(fd, &msg, 0);
}
static int nl_read(int fd, void *data, int len)
{
struct iovec iov[2];
struct msghdr msg;
struct nlmsghdr nlh;
iov[0].iov_base = &nlh;
iov[0].iov_len = sizeof(nlh);
iov[1].iov_base = data;
iov[1].iov_len = len;
memset(&msg, 0, sizeof(msg));
msg.msg_name= (void*)&src_addr;
msg.msg_namelen = sizeof(src_addr);
msg.msg_iov = iov;
msg.msg_iovlen = 2;
return recvmsg(fd, &msg, MSG_DONTWAIT);
}
void handle_iscsi_events(int fd)
{
struct session *session;
struct iscsi_event event;
int res;
retry:
if ((res = nl_read(fd, &event, sizeof(event))) < 0) {
if (errno == EAGAIN)
return;
if (errno == EINTR)
goto retry;
log_error("read netlink fd (%d)", errno);
exit(1);
}
log_debug(1, "conn %u session %#" PRIx64 " target %u, state %u",
event.cid, event.sid, event.tid, event.state);
switch (event.state) {
case E_CONN_CLOSE:
if (!(session = session_find_id(event.tid, event.sid))) {
log_warning("session %#" PRIx64 " not found?", event.sid);
goto retry;
}
if (!--session->conn_cnt)
session_remove(session);
break;
default:
log_warning("%s(%d) %u\n", __FUNCTION__, __LINE__, event.state);
exit(-1);
break;
}
}
int nl_open(void)
{
int nl_fd, res;
nl_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ISCSI_SCST);
if (nl_fd == -1) {
log_error("%s %d\n", __FUNCTION__, errno);
return -1;
}
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = 0; /* not in mcast groups */
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* kernel */
dest_addr.nl_groups = 0; /* unicast */
res = nl_write(nl_fd, NULL, 0);
if (res < 0) {
log_error("%s %d\n", __FUNCTION__, res);
return res;
}
return nl_fd;
}