Code

Renamed the project to SysDB (System DataBase).
[sysdb.git] / src / utils / unixsock.c
index da2ca1e..564aba7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * syscollector - src/utils/unixsock.c
+ * SysDB - src/utils/unixsock.c
  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
  * All rights reserved.
  *
  * private data types
  */
 
-struct sc_unixsock_client {
+struct sdb_unixsock_client {
        char *path;
        FILE *fh;
 
        int shutdown;
 };
 
-#define SC_SHUT_RD   (1 << SHUT_RD)
-#define SC_SHUT_WR   (1 << SHUT_WR)
-#define SC_SHUT_RDWR (SC_SHUT_RD | SC_SHUT_WR)
+#define SDB_SHUT_RD   (1 << SHUT_RD)
+#define SDB_SHUT_WR   (1 << SHUT_WR)
+#define SDB_SHUT_RDWR (SDB_SHUT_RD | SDB_SHUT_WR)
 
 /*
  * private helper functions
  */
 
 static int
-sc_unixsock_get_column_count(const char *string, const char *delim)
+sdb_unixsock_get_column_count(const char *string, const char *delim)
 {
        int count = 1;
 
@@ -85,32 +85,32 @@ sc_unixsock_get_column_count(const char *string, const char *delim)
                }
        }
        return count;
-} /* sc_unixsock_get_column_count */
+} /* sdb_unixsock_get_column_count */
 
 static int
-sc_unixsock_parse_cell(char *string, int type, sc_data_t *data)
+sdb_unixsock_parse_cell(char *string, int type, sdb_data_t *data)
 {
        char *endptr = NULL;
 
        switch (type) {
-               case SC_TYPE_INTEGER:
+               case SDB_TYPE_INTEGER:
                        errno = 0;
                        data->data.integer = strtoll(string, &endptr, 0);
                        break;
-               case SC_TYPE_DECIMAL:
+               case SDB_TYPE_DECIMAL:
                        errno = 0;
                        data->data.decimal = strtod(string, &endptr);
                        break;
-               case SC_TYPE_STRING:
+               case SDB_TYPE_STRING:
                        data->data.string = string;
                        break;
-               case SC_TYPE_DATETIME:
+               case SDB_TYPE_DATETIME:
                        {
                                double datetime = strtod(string, &endptr);
-                               data->data.datetime = DOUBLE_TO_SC_TIME(datetime);
+                               data->data.datetime = DOUBLE_TO_SDB_TIME(datetime);
                        }
                        break;
-               case SC_TYPE_BINARY:
+               case SDB_TYPE_BINARY:
                        /* we don't support any binary information containing 0-bytes */
                        data->data.binary.length = strlen(string);
                        data->data.binary.datum = (const unsigned char *)string;
@@ -121,13 +121,13 @@ sc_unixsock_parse_cell(char *string, int type, sc_data_t *data)
                        return -1;
        }
 
-       if ((type == SC_TYPE_INTEGER) || (type == SC_TYPE_DECIMAL)
-                       || (type == SC_TYPE_DATETIME)) {
+       if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
+                       || (type == SDB_TYPE_DATETIME)) {
                if (errno || (string == endptr)) {
                        char errbuf[1024];
                        fprintf(stderr, "unixsock: Failed to parse string '%s' "
                                        "as numeric value (type %i): %s\n", string, type,
-                                       sc_strerror(errno, errbuf, sizeof(errbuf)));
+                                       sdb_strerror(errno, errbuf, sizeof(errbuf)));
                        return -1;
                }
                else if (endptr && (*endptr != '\0'))
@@ -138,15 +138,15 @@ sc_unixsock_parse_cell(char *string, int type, sc_data_t *data)
 
        data->type = type;
        return 0;
-} /* sc_unixsock_parse_cell */
+} /* sdb_unixsock_parse_cell */
 
 static int
-sc_unixsock_client_process_one_line(sc_unixsock_client_t *client,
-               char *line, sc_unixsock_client_data_cb callback,
-               sc_object_t *user_data, const char *delim,
+sdb_unixsock_client_process_one_line(sdb_unixsock_client_t *client,
+               char *line, sdb_unixsock_client_data_cb callback,
+               sdb_object_t *user_data, const char *delim,
                int column_count, int *types)
 {
-       sc_data_t data[column_count];
+       sdb_data_t data[column_count];
        char *orig_line = line;
 
        int i;
@@ -173,8 +173,8 @@ sc_unixsock_client_process_one_line(sc_unixsock_client_t *client,
                        ++next;
                }
 
-               if (sc_unixsock_parse_cell(line,
-                                       types ? types[i] : SC_TYPE_STRING, &data[i]))
+               if (sdb_unixsock_parse_cell(line,
+                                       types ? types[i] : SDB_TYPE_STRING, &data[i]))
                        return -1;
 
                line = next;
@@ -183,16 +183,16 @@ sc_unixsock_client_process_one_line(sc_unixsock_client_t *client,
        if (callback(client, (size_t)column_count, data, user_data))
                return -1;
        return 0;
-} /* sc_unixsock_client_process_one_line */
+} /* sdb_unixsock_client_process_one_line */
 
 /*
  * public API
  */
 
-sc_unixsock_client_t *
-sc_unixsock_client_create(const char *path)
+sdb_unixsock_client_t *
+sdb_unixsock_client_create(const char *path)
 {
-       sc_unixsock_client_t *client;
+       sdb_unixsock_client_t *client;
 
        if (! path)
                return NULL;
@@ -205,16 +205,16 @@ sc_unixsock_client_create(const char *path)
 
        client->path = strdup(path);
        if (! client->path) {
-               sc_unixsock_client_destroy(client);
+               sdb_unixsock_client_destroy(client);
                return NULL;
        }
 
        client->shutdown = 0;
        return client;
-} /* sc_unixsock_client_create */
+} /* sdb_unixsock_client_create */
 
 int
-sc_unixsock_client_connect(sc_unixsock_client_t *client)
+sdb_unixsock_client_connect(sdb_unixsock_client_t *client)
 {
        struct sockaddr_un sa;
        int fd;
@@ -231,7 +231,7 @@ sc_unixsock_client_connect(sc_unixsock_client_t *client)
        if (fd < 0) {
                char errbuf[1024];
                fprintf(stderr, "unixsock: Failed to open socket: %s\n",
-                               sc_strerror(errno, errbuf, sizeof(errbuf)));
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
        }
 
@@ -242,7 +242,7 @@ sc_unixsock_client_connect(sc_unixsock_client_t *client)
        if (connect(fd, (struct sockaddr *)&sa, sizeof(sa))) {
                char errbuf[1024];
                fprintf(stderr, "unixsock: Failed to connect to %s: %s\n",
-                               sa.sun_path, sc_strerror(errno, errbuf, sizeof(errbuf)));
+                               sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
                close(fd);
                return -1;
        }
@@ -251,51 +251,53 @@ sc_unixsock_client_connect(sc_unixsock_client_t *client)
        if (! client->fh) {
                char errbuf[1024];
                fprintf(stderr, "unixsock: Failed to open I/O stream for %s: %s\n",
-                               sa.sun_path, sc_strerror(errno, errbuf, sizeof(errbuf)));
+                               sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
                close(fd);
                return -1;
        }
 
        client->shutdown = 0;
        return 0;
-} /* sc_unixsock_client_connect */
+} /* sdb_unixsock_client_connect */
 
 int
-sc_unixsock_client_send(sc_unixsock_client_t *client, const char *msg)
+sdb_unixsock_client_send(sdb_unixsock_client_t *client,
+               const char *msg)
 {
        int status;
 
        if ((! client) || (! client->fh))
                return -1;
 
-       if (client->shutdown & SC_SHUT_WR) /* reconnect */
-               sc_unixsock_client_connect(client);
+       if (client->shutdown & SDB_SHUT_WR) /* reconnect */
+               sdb_unixsock_client_connect(client);
 
        status = fprintf(client->fh, "%s\r\n", msg);
        if (status < 0) {
                char errbuf[1024];
                fprintf(stderr, "unixsock: Failed to write to socket (%s): %s\n",
-                               client->path, sc_strerror(errno, errbuf, sizeof(errbuf)));
+                               client->path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return status;
        }
        return status;
-} /* sc_unixsock_client_send */
+} /* sdb_unixsock_client_send */
 
 char *
-sc_unixsock_client_recv(sc_unixsock_client_t *client, char *buffer, size_t buflen)
+sdb_unixsock_client_recv(sdb_unixsock_client_t *client,
+               char *buffer, size_t buflen)
 {
        if ((! client) || (! client->fh) || (! buffer))
                return NULL;
 
-       if (client->shutdown & SC_SHUT_RD) /* reconnect */
-               sc_unixsock_client_connect(client);
+       if (client->shutdown & SDB_SHUT_RD) /* reconnect */
+               sdb_unixsock_client_connect(client);
 
        buffer = fgets(buffer, (int)buflen - 1, client->fh);
        if (! buffer) {
                if (! feof(client->fh)) {
                        char errbuf[1024];
                        fprintf(stderr, "unixsock: Failed to read from socket (%s): %s\n",
-                                       client->path, sc_strerror(errno, errbuf, sizeof(errbuf)));
+                                       client->path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
                }
                return buffer;
        }
@@ -307,11 +309,11 @@ sc_unixsock_client_recv(sc_unixsock_client_t *client, char *buffer, size_t bufle
                --buflen;
        }
        return buffer;
-} /* sc_unixsock_client_recv */
+} /* sdb_unixsock_client_recv */
 
 int
-sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
-               sc_unixsock_client_data_cb callback, sc_object_t *user_data,
+sdb_unixsock_client_process_lines(sdb_unixsock_client_t *client,
+               sdb_unixsock_client_data_cb callback, sdb_object_t *user_data,
                long int max_lines, const char *delim, int n_cols, ...)
 {
        int *types = NULL;
@@ -333,7 +335,7 @@ sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
                for (i = 0; i < n_cols; ++i) {
                        types[i] = va_arg(ap, int);
 
-                       if ((types[i] < 1) || (types[i] > SC_TYPE_BINARY)) {
+                       if ((types[i] < 1) || (types[i] > SDB_TYPE_BINARY)) {
                                fprintf(stderr, "unixsock: Unknown column type %i while "
                                                "processing response from the UNIX socket @ %s.\n",
                                                types[i], client->path);
@@ -358,13 +360,13 @@ sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
                if (max_lines > 0)
                        --max_lines;
 
-               sc_unixsock_client_clearerr(client);
-               line = sc_unixsock_client_recv(client, buffer, sizeof(buffer));
+               sdb_unixsock_client_clearerr(client);
+               line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
 
                if (! line)
                        break;
 
-               column_count = sc_unixsock_get_column_count(line, delim);
+               column_count = sdb_unixsock_get_column_count(line, delim);
 
                if ((n_cols >= 0) && (n_cols != column_count)) {
                        fprintf(stderr, "unixsock: number of columns (%i) does not "
@@ -377,7 +379,7 @@ sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
                if (column_count <= 0) /* no data */
                        continue;
 
-               if (! sc_unixsock_client_process_one_line(client, line, callback,
+               if (! sdb_unixsock_client_process_one_line(client, line, callback,
                                        user_data, delim, column_count, types))
                        ++success;
        }
@@ -385,21 +387,21 @@ sc_unixsock_client_process_lines(sc_unixsock_client_t *client,
        free(types);
 
        if ((max_lines > 0)
-                       || ((max_lines < 0) && (! sc_unixsock_client_eof(client)))
-                       || sc_unixsock_client_error(client)) {
+                       || ((max_lines < 0) && (! sdb_unixsock_client_eof(client)))
+                       || sdb_unixsock_client_error(client)) {
                char errbuf[1024];
                fprintf(stderr, "unixsock: Unexpected end of data while reading "
                                "from socket (%s): %s\n", client->path,
-                               sc_strerror(errno, errbuf, sizeof(errbuf)));
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
        }
        if (! success)
                return -1;
        return 0;
-} /* sc_unixsock_client_process_lines */
+} /* sdb_unixsock_client_process_lines */
 
 int
-sc_unixsock_client_shutdown(sc_unixsock_client_t *client, int how)
+sdb_unixsock_client_shutdown(sdb_unixsock_client_t *client, int how)
 {
        int status;
 
@@ -413,43 +415,43 @@ sc_unixsock_client_shutdown(sc_unixsock_client_t *client, int how)
 
        if (! status) {
                if (how == SHUT_RDWR)
-                       client->shutdown |= SC_SHUT_RDWR;
+                       client->shutdown |= SDB_SHUT_RDWR;
                else
                        client->shutdown |= 1 << how;
        }
        return status;
-} /* sc_unixsock_client_shutdown */
+} /* sdb_unixsock_client_shutdown */
 
 void
-sc_unixsock_client_clearerr(sc_unixsock_client_t *client)
+sdb_unixsock_client_clearerr(sdb_unixsock_client_t *client)
 {
        if ((! client) || (! client->fh))
                return;
        clearerr(client->fh);
-} /* sc_unixsock_client_clearerr */
+} /* sdb_unixsock_client_clearerr */
 
 int
-sc_unixsock_client_eof(sc_unixsock_client_t *client)
+sdb_unixsock_client_eof(sdb_unixsock_client_t *client)
 {
        if ((! client) || (! client->fh)) {
                errno = EBADF;
                return -1;
        }
        return feof(client->fh);
-} /* sc_unixsock_client_eof */
+} /* sdb_unixsock_client_eof */
 
 int
-sc_unixsock_client_error(sc_unixsock_client_t *client)
+sdb_unixsock_client_error(sdb_unixsock_client_t *client)
 {
        if ((! client) || (! client->fh)) {
                errno = EBADF;
                return -1;
        }
        return ferror(client->fh);
-} /* sc_unixsock_client_error */
+} /* sdb_unixsock_client_error */
 
 void
-sc_unixsock_client_destroy(sc_unixsock_client_t *client)
+sdb_unixsock_client_destroy(sdb_unixsock_client_t *client)
 {
        if (! client)
                return;
@@ -463,15 +465,15 @@ sc_unixsock_client_destroy(sc_unixsock_client_t *client)
        client->fh = NULL;
 
        free(client);
-} /* sc_unixsock_client_destroy */
+} /* sdb_unixsock_client_destroy */
 
 const char *
-sc_unixsock_client_path(sc_unixsock_client_t *client)
+sdb_unixsock_client_path(sdb_unixsock_client_t *client)
 {
        if (! client)
                return NULL;
        return client->path;
-} /* sc_unixsock_client_path */
+} /* sdb_unixsock_client_path */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */