static int vshAdmConnect(vshControl *ctl, unsigned int flags) { vshAdmControlPtr priv = ctl->privData; priv->conn = virAdmConnectOpen(ctl->connname, flags); if (!priv->conn) { if (priv->wantReconnect) vshError(ctl, "%s", _("Failed to reconnect to the admin server")); else vshError(ctl, "%s", _("Failed to connect to the admin server")); return -1; } else { if (virAdmConnectRegisterCloseCallback(priv->conn, vshAdmCatchDisconnect, NULL, NULL) < 0) vshError(ctl, "%s", _("Unable to register disconnect callback")); if (priv->wantReconnect) vshPrint(ctl, "%s\n", _("Reconnected to the admin server")); } return 0; }
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(" %-5" PRIu64 " %-15s %-15s\n", (uint64_t)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; }
int main(int argc, char **argv) { int ret, c; virAdmConnectPtr conn = NULL; char *get_outputs = NULL; char *get_filters = NULL; const char *set_outputs = NULL; const char *set_filters = NULL; ret = c = -1; opterr = 0; while ((c = getopt(argc, argv, ":hpo:f:")) > 0) { switch (c) { case 'h': printHelp(argv[0]); exit(EXIT_SUCCESS); case 'o': set_outputs = optarg; break; case 'f': set_filters = optarg; break; case ':': fprintf(stderr, "Missing argument for option -%c\n", optopt); exit(EXIT_FAILURE); case '?': fprintf(stderr, "Unrecognized option '-%c'\n", optopt); exit(EXIT_FAILURE); } } /* first, open a connection to the daemon */ if (!(conn = virAdmConnectOpen(NULL, 0))) goto cleanup; /* get the currently defined log outputs and filters */ if (virAdmConnectGetLoggingOutputs(conn, &get_outputs, 0) < 0 || virAdmConnectGetLoggingFilters(conn, &get_filters, 0) < 0) goto cleanup; fprintf(stdout, "Current settings:\n" " outputs: %s\n" " filters: %s\n" "\n", get_outputs, get_filters ? get_filters : "None"); free(get_outputs); free(get_filters); /* no arguments were provided */ if (argc == 1) { ret = 0; goto cleanup; } /* now, try to change the redefine the current log output and filters */ if (virAdmConnectSetLoggingOutputs(conn, set_outputs, 0) < 0) goto cleanup; if (virAdmConnectSetLoggingFilters(conn, set_filters, 0) < 0) goto cleanup; /* get the currently defined log outputs and filters */ if (virAdmConnectGetLoggingOutputs(conn, &get_outputs, 0) < 0 || virAdmConnectGetLoggingFilters(conn, &get_filters, 0) < 0) goto cleanup; fprintf(stdout, "New settings:\n" " outputs: %s\n" " filters: %s\n" "\n", get_outputs ? get_outputs : "Default", get_filters ? get_filters : "None"); free(get_outputs); free(get_filters); ret = 0; cleanup: virAdmConnectClose(conn); return ret; }