[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 18 of 21] blktap3/drivers: Introduce tapdisk utility functions



This patch copies assorted utility functions from blktap2.

Signed-off-by: Thanos Makatos <thanos.makatos@xxxxxxxxxx>

diff --git a/tools/blktap2/drivers/tapdisk-utils.c 
b/tools/blktap3/drivers/tapdisk-utils.c
copy from tools/blktap2/drivers/tapdisk-utils.c
copy to tools/blktap3/drivers/tapdisk-utils.c
--- a/tools/blktap2/drivers/tapdisk-utils.c
+++ b/tools/blktap3/drivers/tapdisk-utils.c
@@ -25,10 +25,13 @@
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+
+#include <stdlib.h>
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <linux/fs.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 #include <sys/ioctl.h>
@@ -38,31 +41,102 @@
 #include <linux/version.h>
 #endif
 
-#include "blk.h"
+#define SYSLOG_NAMES
+#include <syslog.h>
+
+#include <time.h>
+
 #include "tapdisk.h"
-#include "blktaplib.h"
 #include "tapdisk-log.h"
 #include "tapdisk-utils.h"
+#include "tapdisk-syslog.h"
 
-void
-tapdisk_start_logging(const char *name)
+#define MIN(a,b) (((a) < (b)) ? (a) : (b))
+
+static int tapdisk_syslog_facility_by_name(const char *name)
 {
-       static char buf[128];
+    int facility;
+    CODE *c;
 
-       snprintf(buf, sizeof(buf), "%s[%d]", name, getpid());
-       openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
-       open_tlog("/tmp/tapdisk.log", (64 << 10), TLOG_WARN, 0);
+    facility = -1;
+
+    for (c = facilitynames; c->c_name != NULL; ++c)
+        if (!strcmp(c->c_name, name)) {
+            facility = c->c_val;
+            break;
 }
 
-void
-tapdisk_stop_logging(void)
-{
-       closelog();
-       close_tlog();
+    return facility;
 }
 
-int
-tapdisk_set_resource_limits(void)
+int tapdisk_syslog_facility(const char *arg)
+{
+    int facility;
+    char *endptr;
+
+    if (arg) {
+        facility = strtol(arg, &endptr, 0);
+        if (*endptr == 0)
+            return facility;
+
+        facility = tapdisk_syslog_facility_by_name(arg);
+        if (facility >= 0)
+            return facility;
+}
+
+    return LOG_DAEMON;
+}
+
+char *tapdisk_syslog_ident(const char *name)
+{
+    char ident[TD_SYSLOG_IDENT_MAX + 1];
+    size_t size, len;
+    pid_t pid;
+
+    pid = getpid();
+    size = sizeof(ident);
+    len = 0;
+
+    len = snprintf(NULL, 0, "[%d]", pid);
+    len = snprintf(ident, size - len, "%s", name);
+    len += snprintf(ident + len, size - len, "[%d]", pid);
+
+    return strdup(ident);
+}
+
+size_t
+tapdisk_syslog_strftime(char *buf, size_t size, const struct timeval * tv)
+{
+    const char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+    };
+    struct tm tm;
+
+    /*
+     * TIMESTAMP :=  <Mmm> " " <dd> " " <hh> ":" <mm> ":" <ss>.
+     * Local time, no locales.
+     */
+
+    localtime_r(&tv->tv_sec, &tm);
+
+    return snprintf(buf, size, "%s %2d %02d:%02d:%02d",
+                    mon[tm.tm_mon], tm.tm_mday,
+                    tm.tm_hour, tm.tm_min, tm.tm_sec);
+}
+
+size_t
+tapdisk_syslog_strftv(char *buf, size_t size, const struct timeval * tv)
+{
+    struct tm tm;
+
+    localtime_r(&tv->tv_sec, &tm);
+
+    return snprintf(buf, size, "[%02d:%02d:%02d.%03ld]",
+                    tm.tm_hour, tm.tm_min, tm.tm_sec,
+                    (long) tv->tv_usec / 1000);
+}
+
+int tapdisk_set_resource_limits(void)
 {
        int err;
        struct rlimit rlim;
@@ -111,10 +185,9 @@ tapdisk_namedup(char **dup, const char *
 int
 tapdisk_get_image_size(int fd, uint64_t *_sectors, uint32_t *_sector_size)
 {
-       int ret;
        struct stat stat;
-       uint64_t sectors;
-       uint64_t sector_size;
+    uint64_t sectors, bytes;
+    uint32_t sector_size;
 
        sectors       = 0;
        sector_size   = 0;
@@ -128,12 +201,28 @@ tapdisk_get_image_size(int fd, uint64_t 
 
        if (S_ISBLK(stat.st_mode)) {
                /*Accessing block device directly*/
-               if (blk_getimagesize(fd, &sectors) != 0)
+        if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
+            sectors = bytes >> SECTOR_SHIFT;
+        } else if (ioctl(fd, BLKGETSIZE, &sectors) != 0) {
+            DPRINTF
+                ("ERR: BLKGETSIZE and BLKGETSIZE64 failed, couldn't stat 
image");
                        return -EINVAL;
+        }
 
                /*Get the sector size*/
-               if (blk_getsectorsize(fd, &sector_size) != 0)
+#if defined(BLKSSZGET)
+        {
                        sector_size = DEFAULT_SECTOR_SIZE;
+            ioctl(fd, BLKSSZGET, &sector_size);
+
+            if (sector_size != DEFAULT_SECTOR_SIZE)
+                DPRINTF("Note: sector size is %u (not %d)\n",
+                        sector_size, DEFAULT_SECTOR_SIZE);
+        }
+#else
+        sector_size = DEFAULT_SECTOR_SIZE;
+#endif
+
        } else {
                /*Local file? try fstat instead*/
                sectors     = (stat.st_size >> SECTOR_SHIFT);
@@ -175,40 +264,3 @@ int tapdisk_linux_version(void)
 }
 
 #endif
-int read_exact(int fd, void *data, size_t size)
-{
-    size_t offset = 0;
-    ssize_t len;
-
-    while ( offset < size )
-    {
-        len = read(fd, (char *)data + offset, size - offset);
-        if ( (len == -1) && (errno == EINTR) )
-            continue;
-        if ( len == 0 )
-            errno = 0;
-        if ( len <= 0 )
-            return -1;
-        offset += len;
-    }
-
-    return 0;
-}
-
-int write_exact(int fd, const void *data, size_t size)
-{
-    size_t offset = 0;
-    ssize_t len;
-
-    while ( offset < size )
-    {
-        len = write(fd, (const char *)data + offset, size - offset);
-        if ( (len == -1) && (errno == EINTR) )
-            continue;
-        if ( len <= 0 )
-            return -1;
-        offset += len;
-    }
-
-    return 0;
-}

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.