summaryrefslogtreecommitdiff
path: root/sysfs.c
diff options
context:
space:
mode:
authorNeilBrown <neilb@suse.de>2013-07-01 13:28:13 +1000
committerNeilBrown <neilb@suse.de>2013-07-01 13:28:13 +1000
commitefc67e8e9fe430d5833236f16ea287ef363dadc5 (patch)
tree4da51e5aa6580aa3ae63eaf318fe16d93ad59163 /sysfs.c
parenta2836f12c4c45738b495403a91a0f0db2e88e0cb (diff)
New function: sysfs_wait
We have several places that wait for activity on a sysfs file. Combine most of these into a single 'sysfs_wait' function. Signed-off-by: NeilBrown <neilb@suse.de>
Diffstat (limited to 'sysfs.c')
-rw-r--r--sysfs.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/sysfs.c b/sysfs.c
index cde8f197..b7d41a57 100644
--- a/sysfs.c
+++ b/sysfs.c
@@ -844,3 +844,32 @@ int sysfs_freeze_array(struct mdinfo *sra)
return 0;
return 1;
}
+
+int sysfs_wait(int fd, int *msec)
+{
+ /* Wait up to '*msec' for fd to have an exception condition.
+ * if msec == NULL, wait indefinitely.
+ */
+ fd_set fds;
+ int n;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ if (msec == NULL)
+ n = select(fd+1, NULL, NULL, &fds, NULL);
+ else if (*msec < 0)
+ n = 0;
+ else {
+ struct timeval start, end, tv;
+ gettimeofday(&start, NULL);
+ if (*msec < 1000)
+ tv.tv_usec = (*msec)*1000;
+ else
+ tv.tv_sec = (*msec)/1000;
+ n = select(fd+1, NULL, NULL, &fds, &tv);
+ gettimeofday(&end, NULL);
+ end.tv_sec -= start.tv_sec;
+ *msec -= (end.tv_sec * 1000 + end.tv_usec/1000
+ - start.tv_usec/100) + 1;
+ }
+ return n;
+}