static void list_seats (GDBusConnection *connection) { GDBusProxy *proxy; GError *error; GVariant *res; GVariantIter *iter; gchar *path = NULL; proxy = g_dbus_proxy_new_sync (connection, G_DBUS_PROXY_FLAGS_NONE, NULL, CK_NAME, CK_MANAGER_PATH, CK_MANAGER_INTERFACE, NULL, &error); if (proxy == NULL) { g_print ("error creating proxy, %s", error->message); g_clear_error (&error); return; } error = NULL; res = g_dbus_proxy_call_sync (proxy, "GetSeats", g_variant_new ("()"), G_DBUS_CALL_FLAGS_NONE, 6000, NULL, &error); if (res == NULL) { g_warning ("Failed to get list of seats: %s", error->message); g_clear_error (&error); goto out; } g_variant_get (res, "(ao)", &iter); while (g_variant_iter_next (iter, "o", &path)) { list_sessions (connection, path); } g_variant_iter_free (iter); g_variant_unref (res); out: g_object_unref (proxy); }
int main(int argc, char **argv) { int ret; int session_id; plan_tests(NUM_TESTS); diag("Live unit tests"); ret = connect_viewer("localhost"); ok(ret == 0, "Connect viewer to relayd"); ret = establish_connection(); ok(ret == 0, "Established connection and version check with %d.%d", VERSION_MAJOR, VERSION_MINOR); ret = list_sessions(&session_id); ok(ret > 0, "List sessions : %d session(s)", ret); ret = attach_session(session_id); ok(ret > 0, "Attach to session, %d streams received", ret); ret = get_metadata(); ok(ret > 0, "Get metadata, received %d bytes", ret); ret = get_next_index(); ok(ret == 0, "Get one index per stream"); ret = get_data_packet(first_packet_stream_id, first_packet_offset, first_packet_len); ok(ret == first_packet_len, "Get one data packet for stream %d, offset %d, len %d", first_packet_stream_id, first_packet_offset, first_packet_len); return exit_status(); }
static struct dom_usr *get_domain_userlist(TALLOC_CTX *mem_ctx) { struct sessionid *session_list = NULL; char *machine_name, *p, *nm; const char *sep; struct dom_usr *users, *tmp; int i, num_users, num_sessions; sep = lp_winbind_separator(); if (!sep) { sep = "\\"; } num_sessions = list_sessions(mem_ctx, &session_list); if (num_sessions == 0) { errno = 0; return NULL; } users = talloc_array(mem_ctx, struct dom_usr, num_sessions); if (users == NULL) { TALLOC_FREE(session_list); return NULL; } for (i=num_users=0; i<num_sessions; i++) { if (!session_list[i].username || !session_list[i].remote_machine) { continue; } p = strpbrk(session_list[i].remote_machine, "./"); if (p) { *p = '\0'; } machine_name = talloc_asprintf_strupper_m( users, "%s", session_list[i].remote_machine); if (machine_name == NULL) { DEBUG(10, ("talloc_asprintf failed\n")); continue; } if (strcmp(machine_name, lp_netbios_name()) == 0) { p = session_list[i].username; nm = strstr(p, sep); if (nm) { /* * "domain+name" format so split domain and * name components */ *nm = '\0'; nm += strlen(sep); users[num_users].domain = talloc_asprintf_strupper_m(users, "%s", p); users[num_users].name = talloc_strdup(users, nm); } else { /* * Simple user name so get domain from smb.conf */ users[num_users].domain = talloc_strdup(users, lp_workgroup()); users[num_users].name = talloc_strdup(users, p); } users[num_users].login_time = session_list[i].connect_start; num_users++; } TALLOC_FREE(machine_name); } TALLOC_FREE(session_list); tmp = talloc_realloc(mem_ctx, users, struct dom_usr, num_users); if (tmp == NULL) { return NULL; } users = tmp; /* Sort the user list by time, oldest first */ TYPESAFE_QSORT(users, num_users, dom_user_cmp); errno = 0; return users; }
/* * The 'list <options>' first level command */ int cmd_list(int argc, const char **argv) { int opt, ret = CMD_SUCCESS; const char *session_name; static poptContext pc; struct lttng_domain domain; struct lttng_domain *domains = NULL; memset(&domain, 0, sizeof(domain)); if (argc < 1) { usage(stderr); ret = CMD_ERROR; goto end; } pc = poptGetContext(NULL, argc, argv, long_options, 0); poptReadDefaultConfig(pc, 0); while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: usage(stdout); goto end; case OPT_USERSPACE: opt_userspace = 1; break; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); goto end; default: usage(stderr); ret = CMD_UNDEFINED; goto end; } } /* Mi check */ if (lttng_opt_mi) { writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); if (!writer) { ret = CMD_ERROR; goto end; } /* Open command element */ ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_list); if (ret) { ret = CMD_ERROR; goto end; } /* Open output element */ ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); if (ret) { ret = CMD_ERROR; goto end; } } /* Get session name (trailing argument) */ session_name = poptGetArg(pc); DBG2("Session name: %s", session_name); if (opt_kernel) { domain.type = LTTNG_DOMAIN_KERNEL; } else if (opt_userspace) { DBG2("Listing userspace global domain"); domain.type = LTTNG_DOMAIN_UST; } else if (opt_jul) { DBG2("Listing JUL domain"); domain.type = LTTNG_DOMAIN_JUL; } else if (opt_log4j) { domain.type = LTTNG_DOMAIN_LOG4J; } else if (opt_python) { domain.type = LTTNG_DOMAIN_PYTHON; } if (!opt_kernel && opt_syscall) { WARN("--syscall will only work with the Kernel domain (-k)"); ret = CMD_ERROR; goto end; } if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) { handle = lttng_create_handle(session_name, &domain); if (handle == NULL) { ret = CMD_FATAL; goto end; } } if (session_name == NULL) { if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j && !opt_python) { ret = list_sessions(NULL); if (ret) { goto end; } } if (opt_kernel) { if (opt_syscall) { ret = list_syscalls(); if (ret) { goto end; } } else { ret = list_kernel_events(); if (ret) { goto end; } } } if (opt_userspace) { if (opt_fields) { ret = list_ust_event_fields(); } else { ret = list_ust_events(); } if (ret) { goto end; } } if (opt_jul || opt_log4j || opt_python) { ret = list_agent_events(); if (ret) { goto end; } } } else { /* List session attributes */ if (lttng_opt_mi) { /* Open element sessions * Present for xml consistency */ ret = mi_lttng_sessions_open(writer); if (ret) { goto end; } } /* MI: the ouptut of list_sessions is an unclosed session element */ ret = list_sessions(session_name); if (ret) { goto end; } /* Domain listing */ if (opt_domain) { ret = list_domains(session_name); goto end; } /* Channel listing */ if (opt_kernel || opt_userspace) { if (lttng_opt_mi) { /* Add of domains and domain element for xml * consistency and validation */ ret = mi_lttng_domains_open(writer); if (ret) { goto end; } /* Open domain and leave it open for * nested channels printing */ ret = mi_lttng_domain(writer, &domain, 1); if (ret) { goto end; } } ret = list_tracker_pids(); if (ret) { goto end; } ret = list_channels(opt_channel); if (ret) { goto end; } if (lttng_opt_mi) { /* Close domain and domain element */ ret = mi_lttng_close_multi_element(writer, 2); } if (ret) { goto end; } } else { int i, nb_domain; /* We want all domain(s) */ nb_domain = lttng_list_domains(session_name, &domains); if (nb_domain < 0) { ret = CMD_ERROR; ERR("%s", lttng_strerror(nb_domain)); goto end; } if (lttng_opt_mi) { ret = mi_lttng_domains_open(writer); if (ret) { ret = CMD_ERROR; goto end; } } for (i = 0; i < nb_domain; i++) { switch (domains[i].type) { case LTTNG_DOMAIN_KERNEL: MSG("=== Domain: Kernel ===\n"); break; case LTTNG_DOMAIN_UST: MSG("=== Domain: UST global ===\n"); MSG("Buffer type: %s\n", domains[i].buf_type == LTTNG_BUFFER_PER_PID ? "per PID" : "per UID"); break; case LTTNG_DOMAIN_JUL: MSG("=== Domain: JUL (Java Util Logging) ===\n"); break; case LTTNG_DOMAIN_LOG4J: MSG("=== Domain: LOG4j (Logging for Java) ===\n"); break; case LTTNG_DOMAIN_PYTHON: MSG("=== Domain: Python (logging) ===\n"); break; default: MSG("=== Domain: Unimplemented ===\n"); break; } if (lttng_opt_mi) { ret = mi_lttng_domain(writer, &domains[i], 1); if (ret) { ret = CMD_ERROR; goto end; } } /* Clean handle before creating a new one */ if (handle) { lttng_destroy_handle(handle); } handle = lttng_create_handle(session_name, &domains[i]); if (handle == NULL) { ret = CMD_FATAL; goto end; } if (domains[i].type == LTTNG_DOMAIN_JUL || domains[i].type == LTTNG_DOMAIN_LOG4J || domains[i].type == LTTNG_DOMAIN_PYTHON) { ret = list_session_agent_events(); if (ret) { goto end; } continue; } switch (domains[i].type) { case LTTNG_DOMAIN_KERNEL: case LTTNG_DOMAIN_UST: ret = list_tracker_pids(); if (ret) { goto end; } break; default: break; } ret = list_channels(opt_channel); if (ret) { goto end; } if (lttng_opt_mi) { /* Close domain element */ ret = mi_lttng_writer_close_element(writer); if (ret) { ret = CMD_ERROR; goto end; } } } if (lttng_opt_mi) { /* Close the domains, session and sessions element */ ret = mi_lttng_close_multi_element(writer, 3); if (ret) { ret = CMD_ERROR; goto end; } } } } /* Mi closing */ if (lttng_opt_mi) { /* Close output element */ ret = mi_lttng_writer_close_element(writer); if (ret) { ret = CMD_ERROR; goto end; } /* Command element close */ ret = mi_lttng_writer_command_close(writer); if (ret) { ret = CMD_ERROR; goto end; } } end: /* Mi clean-up */ if (writer && mi_lttng_writer_destroy(writer)) { /* Preserve original error code */ ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; } free(domains); if (handle) { lttng_destroy_handle(handle); } poptFreeContext(pc); return ret; }
static int waitfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { sid_t sid = 0; sid_t *sessions = NULL; sid_t *links = NULL; char *token, *brkt, *path_copy = NULL; char str[128]; (void) offset; (void) fi; printf ("waitfs_readdir\n"); path_copy = (char *) malloc (strlen (path) + 1); if (path_copy == NULL) goto error; strncpy (path_copy, path, strlen (path)); for (token = strtok_r(path_copy, "/", &brkt); token; token = strtok_r(NULL, "/", &brkt)) { if (sid == 0) sid = atoi (token); else goto error; } filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); if (sid == 0) { sessions = list_sessions (); if (sessions != NULL) { for (int i = 0; sessions[i] != 0; i ++) { snprintf (str, 128, "%d", sessions[i]); filler (buf, str, NULL, 0); } } } else // list links { links = list_links (sid); if (links != NULL) { for (int i = 0; links[i] != 0; i ++) { snprintf (str, 128, "%d", links[i]); filler (buf, str, NULL, 0); } } } return 0; error: if (path_copy != NULL) free (path_copy); return -ENOENT; }
/* * The 'list <options>' first level command */ int cmd_list(int argc, const char **argv) { int opt, ret = CMD_SUCCESS; const char *session_name; static poptContext pc; struct lttng_domain domain; struct lttng_domain *domains = NULL; memset(&domain, 0, sizeof(domain)); if (argc < 1) { usage(stderr); ret = CMD_ERROR; goto end; } pc = poptGetContext(NULL, argc, argv, long_options, 0); poptReadDefaultConfig(pc, 0); while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: usage(stdout); goto end; case OPT_USERSPACE: opt_userspace = 1; break; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); goto end; default: usage(stderr); ret = CMD_UNDEFINED; goto end; } } /* Get session name (trailing argument) */ session_name = poptGetArg(pc); DBG2("Session name: %s", session_name); if (opt_kernel) { domain.type = LTTNG_DOMAIN_KERNEL; } else if (opt_userspace) { DBG2("Listing userspace global domain"); domain.type = LTTNG_DOMAIN_UST; } else if (opt_jul) { DBG2("Listing JUL domain"); domain.type = LTTNG_DOMAIN_JUL; } if (opt_kernel || opt_userspace || opt_jul) { handle = lttng_create_handle(session_name, &domain); if (handle == NULL) { ret = CMD_FATAL; goto end; } } if (session_name == NULL) { if (!opt_kernel && !opt_userspace && !opt_jul) { ret = list_sessions(NULL); if (ret != 0) { goto end; } } if (opt_kernel) { ret = list_kernel_events(); if (ret < 0) { ret = CMD_ERROR; goto end; } } if (opt_userspace) { if (opt_fields) { ret = list_ust_event_fields(); } else { ret = list_ust_events(); } if (ret < 0) { ret = CMD_ERROR; goto end; } } if (opt_jul) { ret = list_jul_events(); if (ret < 0) { ret = CMD_ERROR; goto end; } } } else { /* List session attributes */ ret = list_sessions(session_name); if (ret != 0) { goto end; } /* Domain listing */ if (opt_domain) { ret = list_domains(session_name); goto end; } if (opt_kernel || opt_userspace) { /* Channel listing */ ret = list_channels(opt_channel); if (ret < 0) { goto end; } } else { int i, nb_domain; /* We want all domain(s) */ nb_domain = lttng_list_domains(session_name, &domains); if (nb_domain < 0) { ret = nb_domain; ERR("%s", lttng_strerror(ret)); goto end; } for (i = 0; i < nb_domain; i++) { switch (domains[i].type) { case LTTNG_DOMAIN_KERNEL: MSG("=== Domain: Kernel ===\n"); break; case LTTNG_DOMAIN_UST: MSG("=== Domain: UST global ===\n"); MSG("Buffer type: %s\n", domains[i].buf_type == LTTNG_BUFFER_PER_PID ? "per PID" : "per UID"); break; case LTTNG_DOMAIN_JUL: MSG("=== Domain: JUL (Java Util Logging) ===\n"); break; default: MSG("=== Domain: Unimplemented ===\n"); break; } /* Clean handle before creating a new one */ if (handle) { lttng_destroy_handle(handle); } handle = lttng_create_handle(session_name, &domains[i]); if (handle == NULL) { ret = CMD_FATAL; goto end; } if (domains[i].type == LTTNG_DOMAIN_JUL) { ret = list_session_jul_events(); if (ret < 0) { goto end; } continue; } ret = list_channels(opt_channel); if (ret < 0) { goto end; } } } } end: free(domains); if (handle) { lttng_destroy_handle(handle); } poptFreeContext(pc); return ret; }
int main(int argc, char *argv[]) { int ch, idx, plen, nready, interactive = 0, listonly = 0; const char *id, *user = NULL, *pattern = NULL, *tty = NULL, *decimal = "."; char path[PATH_MAX], buf[LINE_MAX], *cp, *ep; double seconds, to_wait, speed = 1.0, max_wait = 0; FILE *lfile; fd_set *fdsw; sigaction_t sa; size_t len, nbytes, nread, off; ssize_t nwritten; #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME) setprogname(argc > 0 ? argv[0] : "sudoreplay"); #endif #ifdef HAVE_SETLOCALE setlocale(LC_ALL, ""); decimal = localeconv()->decimal_point; #endif while ((ch = getopt(argc, argv, "d:f:hlm:s:V")) != -1) { switch(ch) { case 'd': session_dir = optarg; break; case 'f': /* Set the replay filter. */ replay_filter = 0; for (cp = strtok(optarg, ","); cp; cp = strtok(NULL, ",")) { if (strcmp(cp, "stdout") == 0) SET(replay_filter, 1 << IOFD_STDOUT); else if (strcmp(cp, "stderr") == 0) SET(replay_filter, 1 << IOFD_STDERR); else if (strcmp(cp, "ttyout") == 0) SET(replay_filter, 1 << IOFD_TTYOUT); else errorx(1, "invalid filter option: %s", optarg); } break; case 'h': help(); /* NOTREACHED */ case 'l': listonly = 1; break; case 'm': errno = 0; max_wait = strtod(optarg, &ep); if (*ep != '\0' || errno != 0) errorx(1, "invalid max wait: %s", optarg); break; case 's': errno = 0; speed = strtod(optarg, &ep); if (*ep != '\0' || errno != 0) errorx(1, "invalid speed factor: %s", optarg); break; case 'V': (void) printf("%s version %s\n", getprogname(), PACKAGE_VERSION); exit(0); default: usage(1); /* NOTREACHED */ } } argc -= optind; argv += optind; if (listonly) exit(list_sessions(argc, argv, pattern, user, tty)); if (argc != 1) usage(1); /* 6 digit ID in base 36, e.g. 01G712AB */ id = argv[0]; if (!VALID_ID(id)) errorx(1, "invalid ID %s", id); plen = snprintf(path, sizeof(path), "%s/%.2s/%.2s/%.2s/timing", session_dir, id, &id[2], &id[4]); if (plen <= 0 || plen >= sizeof(path)) errorx(1, "%s/%.2s/%.2s/%.2s/%.2s/timing: %s", session_dir, id, &id[2], &id[4], strerror(ENAMETOOLONG)); plen -= 7; /* Open files for replay, applying replay filter for the -f flag. */ for (idx = 0; idx < IOFD_MAX; idx++) { if (ISSET(replay_filter, 1 << idx) || idx == IOFD_TIMING) { io_fds[idx].v = open_io_fd(path, plen, io_fnames[idx]); if (io_fds[idx].v == NULL) error(1, "unable to open %s", path); } } /* Read log file. */ path[plen] = '\0'; strlcat(path, "/log", sizeof(path)); lfile = fopen(path, "r"); if (lfile == NULL) error(1, "unable to open %s", path); cp = NULL; len = 0; /* Pull out command (third line). */ if (getline(&cp, &len, lfile) == -1 || getline(&cp, &len, lfile) == -1 || getline(&cp, &len, lfile) == -1) { errorx(1, "invalid log file %s", path); } printf("Replaying sudo session: %s", cp); free(cp); fclose(lfile); fflush(stdout); zero_bytes(&sa, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESETHAND; sa.sa_handler = cleanup; (void) sigaction(SIGINT, &sa, NULL); (void) sigaction(SIGKILL, &sa, NULL); (void) sigaction(SIGTERM, &sa, NULL); (void) sigaction(SIGHUP, &sa, NULL); sa.sa_flags = SA_RESTART; sa.sa_handler = SIG_IGN; (void) sigaction(SIGTSTP, &sa, NULL); (void) sigaction(SIGQUIT, &sa, NULL); /* XXX - read user input from /dev/tty and set STDOUT to raw if not a pipe */ /* Set stdin to raw mode if it is a tty */ interactive = isatty(STDIN_FILENO); if (interactive) { ch = fcntl(STDIN_FILENO, F_GETFL, 0); if (ch != -1) (void) fcntl(STDIN_FILENO, F_SETFL, ch | O_NONBLOCK); if (!term_raw(STDIN_FILENO, 1)) error(1, "cannot set tty to raw mode"); } fdsw = (fd_set *)emalloc2(howmany(STDOUT_FILENO + 1, NFDBITS), sizeof(fd_mask)); /* * Timing file consists of line of the format: "%f %d\n" */ #ifdef HAVE_ZLIB_H while (gzgets(io_fds[IOFD_TIMING].g, buf, sizeof(buf)) != NULL) { #else while (fgets(buf, sizeof(buf), io_fds[IOFD_TIMING].f) != NULL) { #endif if (!parse_timing(buf, decimal, &idx, &seconds, &nbytes)) errorx(1, "invalid timing file line: %s", buf); if (interactive) check_input(STDIN_FILENO, &speed); /* Adjust delay using speed factor and clamp to max_wait */ to_wait = seconds / speed; if (max_wait && to_wait > max_wait) to_wait = max_wait; delay(to_wait); /* Even if we are not relaying, we still have to delay. */ if (io_fds[idx].v == NULL) continue; /* All output is sent to stdout. */ while (nbytes != 0) { if (nbytes > sizeof(buf)) len = sizeof(buf); else len = nbytes; #ifdef HAVE_ZLIB_H nread = gzread(io_fds[idx].g, buf, len); #else nread = fread(buf, 1, len, io_fds[idx].f); #endif nbytes -= nread; off = 0; do { /* no stdio, must be unbuffered */ nwritten = write(STDOUT_FILENO, buf + off, nread - off); if (nwritten == -1) { if (errno == EINTR) continue; if (errno == EAGAIN) { FD_SET(STDOUT_FILENO, fdsw); do { nready = select(STDOUT_FILENO + 1, NULL, fdsw, NULL, NULL); } while (nready == -1 && errno == EINTR); if (nready == 1) continue; } error(1, "writing to standard output"); } off += nwritten; } while (nread > off); } } term_restore(STDIN_FILENO, 1); exit(0); } static void delay(double secs) { struct timespec ts, rts; int rval; /* * Typical max resolution is 1/HZ but we can't portably check that. * If the interval is small enough, just ignore it. */ if (secs < 0.0001) return; rts.tv_sec = secs; rts.tv_nsec = (secs - (double) rts.tv_sec) * 1000000000.0; do { memcpy(&ts, &rts, sizeof(ts)); rval = nanosleep(&ts, &rts); } while (rval == -1 && errno == EINTR); if (rval == -1) error(1, "nanosleep: tv_sec %ld, tv_nsec %ld", ts.tv_sec, ts.tv_nsec); } static void * open_io_fd(char *path, int len, const char *suffix) { path[len] = '\0'; strlcat(path, suffix, PATH_MAX); #ifdef HAVE_ZLIB_H return gzopen(path, "r"); #else return fopen(path, "r"); #endif }