libvirt/examples/admin/list_clients.c
Eric Blake acf522e85a examples: Avoid gnulib, have standalone examples
Commit 0c6ad476 updated gnulib, which rearranged some of the
conditions in gnulib wrapper headers such that compilation
started failing on BSD systems when the normal system <unistd.h>
tried to include another system header but instead got a
gnulib wrapper header in an incomplete state; this is because
gnulib headers only work if <config.h> is included first.

Commit b6f78259 papered over the symptoms of that by including
<config.h> in all the examples.  But this logic is backwards -
if our examples are truly meant to be stand-alone, they should
NOT depend on how libvirt was configured, and should NOT
depend on the gnulib fixes for system quirks.  In particular,
if an example does not need to link against libgnulib.la,
then it also does not need to use -Ignulib in its compile
flags, and likewise does not need to include <config.h> since
none of the gnulib wrapper headers should be interfering.

So, revert (most of) b6f78259 (except for the bogus pre-patch
use of "config.h" in admin/logging.c: if config.h is included,
it should be via <> rather than "", and must be before any
system headers); then additionally nuke all mention of
<config.h>, -Ignulib, and -llibgnu.la, making all of the
examples truly standalone.

Signed-off-by: Eric Blake <eblake@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2019-01-08 09:26:51 -06:00

112 lines
3.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <libvirt/libvirt-admin.h>
static const char *
exampleTransportToString(int transport)
{
const char *str = NULL;
switch ((virClientTransport) transport) {
case VIR_CLIENT_TRANS_UNIX:
str = "unix";
break;
case VIR_CLIENT_TRANS_TCP:
str = "tcp";
break;
case VIR_CLIENT_TRANS_TLS:
str = "tls";
break;
}
return str ? str : "unknown";
}
static char *
exampleGetTimeStr(time_t then)
{
char *ret = NULL;
struct tm timeinfo;
if (!localtime_r(&then, &timeinfo))
return NULL;
if (!(ret = calloc(64, sizeof(char))))
return NULL;
if (strftime(ret, 64, "%Y-%m-%d %H:%M:%S%z",
&timeinfo) == 0) {
free(ret);
return NULL;
}
return ret;
}
int main(int argc, char **argv)
{
int ret = -1;
virAdmConnectPtr conn = NULL;
virAdmServerPtr srv = NULL; /* which server list the clients from */
virAdmClientPtr *clients = NULL; /* where to store the servers */
ssize_t i = 0;
int count = 0;
if (argc != 2) {
fprintf(stderr, "One argument specifying the server to list connected "
"clients for is expected\n");
return -1;
}
/* first, open a connection to the daemon */
if (!(conn = virAdmConnectOpen(NULL, 0)))
return -1;
/* first a virAdmServerPtr handle is necessary to obtain, that is done by
* doing a lookup for specific server, let's get a handle on "libvirtd"
* server
*/
if (!(srv = virAdmConnectLookupServer(conn, argv[1], 0)))
goto cleanup;
/* now get the currently connected clients to server @srv */
if ((count = virAdmServerListClients(srv, &clients, 0)) < 0)
goto cleanup;
/* let's print the currently connected clients and some basic info about
* them, we have 2 options how to interate over the returned list,
* use @count as the boundary or use the fact that @clients are guaranteed
* to contain 1 extra element NULL;
* this example uses the first option
*/
printf(" %-5s %-15s %-15s\n%s\n", "Id", "Transport", "Connected since",
"--------------------------------------------------");
for (i = 0; i < count; i++) {
virAdmClientPtr client = clients[i];
unsigned long long id = virAdmClientGetID(client);
int transport = virAdmClientGetTransport(client);
char * timestr = NULL;
if (!(timestr =
exampleGetTimeStr(virAdmClientGetTimestamp(client))))
goto cleanup;
printf(" %-5llu %-15s %-15s\n", id,
exampleTransportToString(transport), timestr);
free(timestr);
}
ret = 0;
cleanup:
/* Once finished, free the list of clients, free the server handle and
* close the connection properly, @conn will be deallocated automatically
*/
for (i = 0; i < count; i++)
virAdmClientFree(clients[i]);
free(clients);
virAdmServerFree(srv);
virAdmConnectClose(conn);
return ret;
}