const char *add_to_sessvar(const char *desc, const char *uuid) { size_t envlen; /* Length of the new string */ char *sessvar; /* Copy of the original envvar */ char *envbuf; /* Temporary buffer for the finished string */ assert(valid_uuid(uuid, TRUE)); if (!is_valid_desc_string(desc)) desc = NULL; if (getenv(ENV_SESS)) { char *ap; ap = strdup(getenv(ENV_SESS)); clean_up_sessvar(ap); sessvar = strdup(ap); free(ap); } else sessvar = strdup(""); if (!sessvar) { myerror("add_to_sessvar(): Could not duplicate %s " "environment variable", ENV_SESS); return NULL; } envlen = strlen(ENV_SESS) + 1 + strlen(sessvar) + 1 + strlen(desc) + 1 + UUID_LENGTH + 1 + 1; envbuf = malloc(envlen); if (!envbuf) { myerror("Could not allocate %lu bytes for %s buffer", envlen, ENV_SESS); free(sessvar); return NULL; } snprintf(envbuf, envlen, "%s=%s,%s%s%s,", ENV_SESS, sessvar ? sessvar : "", desc ? desc : "", desc ? "/" : "", uuid); if (putenv(envbuf)) { myerror("Could not set %s environment variable", ENV_SESS); free(sessvar); return NULL; } free(sessvar); return getenv(ENV_SESS); }
int run_session(const struct Options *orig_opt, const int argc, char * const argv[]) { int retval = EXIT_SUCCESS; struct Options opt = *orig_opt; char *cmd = NULL; char *start_uuid = NULL; char *cmd_desc = NULL; struct uuid_result result; assert(orig_opt); assert(argv); cmd = concat_cmd_string(argc, argv); if (!cmd) return -1; cmd_desc = get_desc_from_command(cmd); msg(2, "cmd_desc = \"%s\"", cmd_desc); opt.count = 1; opt.whereto = "e"; result = create_and_log_uuids(&opt); if (!result.success) { myerror("Error generating UUID, session not started"); retval = -1; goto cleanup; } start_uuid = strdup(result.lastuuid); if (!start_uuid) { myerror("Could not duplicate start UUID"); retval = -1; goto cleanup; } assert(valid_uuid(start_uuid, TRUE)); msg(3, "old %s: \"%s\"", ENV_SESS, getenv(ENV_SESS)); add_to_sessvar(cmd_desc, start_uuid); msg(3, "new %s: \"%s\"", ENV_SESS, getenv(ENV_SESS)); msg(1, "Executing \"%s\"", cmd); retval = system(cmd); /* fixme: This value is shifted with 8 bits in * main(). Check if it's ok. */ msg(2, "run_session(): retval from system() = %d (0x%x)", retval, retval); cleanup: free(start_uuid); free(cmd_desc); free(cmd); return(retval); }
int fill_sess(struct Entry *dest, const char *uuid, const char *desc, const size_t desclen) { char *auuid = NULL, *adesc = NULL; static unsigned int sessind = 0; assert(dest); assert(valid_uuid(uuid, FALSE)); if (sessind >= MAX_SESS) { fprintf(stderr, "%s: Maximum number of sess entries (%u) " "exceeded\n", progname, MAX_SESS); return EXIT_FAILURE; } auuid = strndup(uuid, UUID_LENGTH); if (!auuid) { myerror("fill_sess(): Memory allcation error, " "could not duplicate UUID"); return EXIT_FAILURE; } if (desc && desclen) { adesc = strndup(desc, desclen); if (!adesc) { myerror("fill_sess(): Memory allocation error, " "could not duplicate desc"); free(auuid); return EXIT_FAILURE; } if (!is_valid_desc_string(adesc)) free(adesc); else dest->sess[sessind].desc = adesc; } dest->sess[sessind].uuid = auuid; sessind++; return EXIT_SUCCESS; }
/** * terminal_profiles_list_dup_uuid: * @list: * @uuid: (allow-none): * @error: * * Returns: (transfer full): the UUID of the profile specified by @uuid, or %NULL */ char * terminal_profiles_list_dup_uuid (TerminalSettingsList *list, const char *uuid, GError **error) { char *rv; if (uuid == NULL) { rv = terminal_settings_list_dup_default_child (list); if (rv == NULL) goto err; return rv; } else if (!valid_uuid (uuid, error)) return NULL; if (terminal_settings_list_has_child (list, uuid)) return g_strdup (uuid); err: g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "No profile with UUID \"%s\" exists", uuid); return NULL; }
struct uuid_result create_and_log_uuids(const struct Options *opt) { struct uuid_result retval; char *rcfile = NULL, *logfile = NULL; unsigned int i, count; struct Rc rc; struct Entry entry; struct Logs logs; assert(opt); logs.logfp = NULL; count = opt->count; retval.count = 0; memset(retval.lastuuid, 0, UUID_LENGTH + 1); retval.success = TRUE; init_xml_entry(&entry); /* * Get information about the environment; hostname, current directory, * tty, location of rc file and log directory, etc. */ if (init_randomness() == EXIT_FAILURE) { retval.success = FALSE; goto cleanup; } rcfile = get_rcfilename(opt); if (read_rcfile(rcfile, &rc) == EXIT_FAILURE) { retval.success = FALSE; goto cleanup; } if (fill_entry_struct(&entry, &rc, opt) == EXIT_FAILURE) { retval.success = FALSE; goto cleanup; } logfile = get_log_prefix(&rc, opt, ".xml"); if (!logfile) { retval.success = FALSE; goto cleanup; } signal(SIGHUP, sighandler); signal(SIGINT, sighandler); signal(SIGQUIT, sighandler); signal(SIGPIPE, sighandler); signal(SIGTERM, sighandler); /* * Open the log file. If it's missing, create it. */ logs.logfp = open_logfile(logfile); if (!logs.logfp) { retval.success = FALSE; goto cleanup; } /* * Generate the UUIDs and write them to the log file. */ if (opt->uuid) count = 1; for (i = 0; i < count; i++) { if (!process_uuid(&logs, &rc, opt, &entry)) { retval.success = FALSE; goto cleanup; } retval.count++; if (should_terminate) break; } if (valid_uuid(entry.uuid, TRUE)) strncpy(retval.lastuuid, entry.uuid, UUID_LENGTH + 1); /* * Check that the correct amount of UUIDs were created. */ if (retval.count < opt->count) fprintf(stderr, "%s: Generated only %u of %u UUIDs\n", progname, retval.count, opt->count); /* * Close up the shop and go home. */ cleanup: if (logs.logfp && (close_logfile(logs.logfp) == EXIT_FAILURE)) retval.success = FALSE; free(logfile); free_sess(&entry); free_tags(&entry); free(rc.uuidcmd); free(rc.macaddr); free(rc.hostname); free(entry.txt); free(entry.date); free(entry.cwd); free(rcfile); return retval; }
char *process_uuid(struct Logs *logs, const struct Rc *rc, const struct Options *opt, struct Entry *entry) { assert(logs); assert(logs->logfp); assert(rc); assert(opt); assert(entry); /* * Generate the UUID or use an already generated UUID stored in * opt->uuid. */ if (opt->uuid) { if (!valid_uuid(opt->uuid, TRUE)) { fprintf(stderr, "process_uuid(): UUID \"%s\" is not " "valid.\n", opt->uuid); return NULL; } entry->uuid = opt->uuid; } else entry->uuid = generate_uuid(rc, opt->random_mac); if (!entry->uuid) { fprintf(stderr, "%s: UUID generation failed\n", progname); return NULL; } /* * Extract the time stamp from the UUID and store it in an allocated * buffer. */ if (!entry->date) entry->date = malloc(DATE_LENGTH + 1); if (!entry->date) { myerror("process_uuid(): Could not allocate %lu bytes for " "date string", DATE_LENGTH + 1); return NULL; } if (!uuid_date_from_uuid(entry->date, entry->uuid)) return NULL; if (add_to_logfile(logs->logfp, entry, opt->raw) == EXIT_FAILURE) return NULL; /* * Write the UUID to stdout and/or stderr, or not, depending on the * -w/--whereto argument. */ if (!opt->whereto) puts(entry->uuid); else { if (strchr(opt->whereto, 'a') || strchr(opt->whereto, 'o')) fprintf(stdout, "%s\n", entry->uuid); if (strchr(opt->whereto, 'a') || strchr(opt->whereto, 'e')) fprintf(stderr, "%s\n", entry->uuid); } return entry->uuid; }
int get_sess_info(struct Entry *entry) { char *s, *p, *desc_found = NULL, *desc_end = NULL; assert(entry); if (!getenv(ENV_SESS)) return EXIT_SUCCESS; s = strdup(getenv(ENV_SESS)); if (!s) { myerror("get_sess_info(): Could not duplicate %s environment " "variable", ENV_SESS); return EXIT_FAILURE; } if (!scan_for_uuid(s)) { /* * The environment variable exists, but contains no valid * UUIDs. Not much to do about that, so just return gracefully. */ free(s); return EXIT_SUCCESS; } p = s; while (*p) { if (valid_uuid(p, FALSE)) { size_t desclen = 0; if (desc_found && !desc_end) desc_end = p; /* There was no slash between * desc and uuid, so desc_end * hasn't been set. */ if (desc_end > desc_found) desclen = desc_end - desc_found; if (fill_sess(entry, p, desc_found, desclen) == EXIT_FAILURE) { free(s); return EXIT_FAILURE; } p += UUID_LENGTH - 1; desc_found = desc_end = NULL; } else if (is_legal_desc_char(*p)) { if (!desc_found && p >= s) desc_found = p; } else if (*p == '/') { if (desc_found) desc_end = p; } else desc_found = desc_end = NULL; p++; } free(s); return EXIT_SUCCESS; }