本文整理汇总了C++中safe_asprintf函数的典型用法代码示例。如果您正苦于以下问题:C++ safe_asprintf函数的具体用法?C++ safe_asprintf怎么用?C++ safe_asprintf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_asprintf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: icon_cirros
static char *
icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
{
char *ret = NOT_FOUND;
char *type = NULL;
char *local = NULL;
char *pngfile = NULL;
struct command *cmd;
int r;
r = guestfs_exists (g, CIRROS_LOGO);
if (r == -1) {
ret = NULL; /* a real error */
goto out;
}
if (r == 0) goto out;
/* Check the file type and geometry. */
type = guestfs_file (g, CIRROS_LOGO);
if (!type) goto out;
if (!STRPREFIX (type, "ASCII text")) goto out;
local = guestfs___download_to_tmp (g, fs, CIRROS_LOGO, "icon", 1024);
if (!local) goto out;
/* Use pbmtext to render it. */
pngfile = safe_asprintf (g, "%s/cirros.png", g->tmpdir);
cmd = guestfs___new_command (g);
guestfs___cmd_add_string_unquoted (cmd, PBMTEXT " < ");
guestfs___cmd_add_string_quoted (cmd, local);
guestfs___cmd_add_string_unquoted (cmd, " | " PNMTOPNG " > ");
guestfs___cmd_add_string_quoted (cmd, pngfile);
r = guestfs___cmd_run (cmd);
guestfs___cmd_close (cmd);
if (r == -1)
goto out;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
goto out;
/* Read it into memory. */
if (read_whole_file (g, pngfile, &ret, size_r) == -1) {
ret = NULL;
goto out;
}
out:
free (pngfile);
free (local);
free (type);
return ret;
}
示例2: http_callback_status
void
http_callback_status(httpd *webserver, request *r)
{
char * status = NULL;
char * prestatus = NULL;
status = get_status_text();
safe_asprintf(&prestatus, "<pre>\n%s\n</pre>", status);
http_nodogsplash_serve_info(r, "Nodogsplash Status",prestatus);
free(status);
free(prestatus);
}
示例3: http_callback_logout
void http_callback_logout(httpd *webserver, request *r)
{
t_client *client;
s_config *config = config_get_config();
LOCK_CLIENT_LIST();
client = client_list_find_by_ip(r->clientAddr);
/* Send logout to auth server if client is logged in */
if (client != NULL) {
t_authresponse authresponse;
char *ip = strdup(client->ip);
char *mac = strdup(client->mac);
char *token = strdup(client->token);
unsigned long long incoming = client->counters.incoming;
unsigned long long outgoing = client->counters.outgoing;
debug(LOG_INFO, "Got manual logout from client ip %s, mac %s, token %s",
client->ip, client->mac, client->token);
fw_deny(client->ip, client->mac, client->fw_connection_state);
client_list_delete(client);
/* Unlock client list here since auth_server_request may take a while */
UNLOCK_CLIENT_LIST();
/* Advertise the logout if we have an auth server */
if (config->auth_servers != NULL) {
auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT, ip,
mac, token, incoming, outgoing);
}
free(ip);
free(mac);
free(token);
} else {
/* Do nothing if the client is not in the client list (i.e. not logged in) */
debug(LOG_INFO, "Got manual logout from client %s, but client not in list", r->clientAddr);
UNLOCK_CLIENT_LIST();
}
if (config->auth_servers != NULL) {
/* Re-direct them to auth server */
char *urlFragment = NULL;
t_auth_serv *auth_server = get_auth_server();
safe_asprintf(&urlFragment, "%smessage=%s",
auth_server->authserv_msg_script_path_fragment,
GATEWAY_MESSAGE_ACCOUNT_LOGGED_OUT
);
http_send_redirect_to_auth(r, urlFragment, "Redirect to logout message");
free(urlFragment);
}
}
示例4: http_send_redirect
/** @brief Sends a redirect to the web browser
* @param r The request
* @param url The url to redirect to
* @param text The text to include in the redirect header and the manual redirect link title. NULL is acceptable */
void
http_send_redirect(request * r, const char *url, const char *text)
{
char *message = NULL;
char *header = NULL;
char *response = NULL;
/* Re-direct them to auth server */
debug(LOG_DEBUG, "Redirecting client browser to %s", url);
safe_asprintf(&header, "Location: %s", url);
// liudf 20160104; change 302 to 307
safe_asprintf(&response, "307 %s\r\n", text ? text : "Redirecting");
httpdSetResponse(r, response);
httpdAddHeader(r, header);
free(response);
free(header);
safe_asprintf(&message, "Please <a href='%s'>click here</a>.", url);
send_http_page(r, text ? text : "Redirection to message", message);
_httpd_closeSocket(r);
free(message);
}
示例5: http_nodogsplash_redirect
void
http_nodogsplash_redirect(request *r, const char url[])
{
char *header;
httpdSetResponse(r, "302 Found");
safe_asprintf(&header, "Location: %s",url);
httpdAddHeader(r, header);
httpdPrintf(r, "<html><head></head><body><a href='%s'>Click here to continue to<br>%s</a></body></html>",url,url);
free(header);
}
示例6: http_nodogsplash_redirect
void
http_nodogsplash_redirect(request *r, const char url[])
{
char *header;
debug(LOG_DEBUG,"Redirect client %s to %s",r->clientAddr,url);
httpdSetResponse(r, "302 Found\n");
safe_asprintf(&header, "Location: http://%s",url);
httpdAddHeader(r, header);
httpdPrintf(r, "<html><head></head><body><a href='%s'>Click here to continue to<br>%s</a></body></html>",url,url);
free(header);
}
示例7: read_osinfo_db_directory
static int
read_osinfo_db_directory (guestfs_h *g, const char *directory)
{
DIR *dir;
int r;
dir = opendir (directory);
if (!dir) {
debug (g, "osinfo: %s: %s", directory, strerror (errno));
return 0; /* This is not an error: RHBZ#948324. */
}
for (;;) {
struct dirent *d;
errno = 0;
d = readdir (dir);
if (!d) break;
if (STRSUFFIX (d->d_name, ".xml")) {
CLEANUP_FREE char *pathname = NULL;
pathname = safe_asprintf (g, "%s/%s", directory, d->d_name);
r = read_osinfo_db_xml (g, pathname);
if (r == -1)
goto error;
}
}
/* Check for failure in readdir. */
if (errno != 0) {
perrorf (g, "readdir: %s", directory);
goto error;
}
/* Close the directory handle. */
r = closedir (dir);
dir = NULL;
if (r == -1) {
perrorf (g, "closedir: %s", directory);
goto error;
}
return 1;
error:
if (dir)
closedir (dir);
return -1;
}
示例8: guestfs_int_get_uefi
/**
* Return the location of firmware needed to boot the appliance. This
* is aarch64 only currently, since that's the only architecture where
* UEFI is mandatory (and that only for RHEL).
*
* C<*code> is initialized with the path to the read-only UEFI code
* file. C<*vars> is initialized with the path to a copy of the UEFI
* vars file (which is cleaned up automatically on exit).
*
* If C<*code> == C<*vars> == C<NULL> then no UEFI firmware is
* available.
*
* C<*code> and C<*vars> should be freed by the caller.
*
* If the function returns C<-1> then there was a real error which
* should cause appliance building to fail (no UEFI firmware is not an
* error).
*
* See also F<v2v/utils.ml>:find_uefi_firmware
*/
int
guestfs_int_get_uefi (guestfs_h *g, char **code, char **vars, int *flags)
{
#ifdef __aarch64__
size_t i;
for (i = 0; guestfs_int_uefi_aarch64_firmware[i].code != NULL; ++i) {
const char *codefile = guestfs_int_uefi_aarch64_firmware[i].code;
const char *code_debug_file =
guestfs_int_uefi_aarch64_firmware[i].code_debug;
const char *varsfile = guestfs_int_uefi_aarch64_firmware[i].vars;
if (access (codefile, R_OK) == 0 && access (varsfile, R_OK) == 0) {
CLEANUP_CMD_CLOSE struct command *copycmd = guestfs_int_new_command (g);
char *varst;
int r;
/* Make a copy of NVRAM variables file. You can't just map it
* into the address space read-only as that triggers a different
* path inside UEFI.
*/
varst = safe_asprintf (g, "%s/vars.fd.%d", g->tmpdir, ++g->unique);
guestfs_int_cmd_add_arg (copycmd, "cp");
guestfs_int_cmd_add_arg (copycmd, varsfile);
guestfs_int_cmd_add_arg (copycmd, varst);
r = guestfs_int_cmd_run (copycmd);
if (r == -1 || !WIFEXITED (r) || WEXITSTATUS (r) != 0) {
free (varst);
return -1;
}
/* If debugging is enabled and we can find the code file with
* debugging enabled, use that instead.
*/
if (g->verbose && access (code_debug_file, R_OK) == 0)
codefile = code_debug_file;
/* Caller frees. */
*code = safe_strdup (g, codefile);
*vars = varst;
*flags = guestfs_int_uefi_aarch64_firmware[i].flags;
return 0;
}
}
#endif
/* Not found. */
*code = *vars = NULL;
*flags = 0;
return 0;
}
示例9: map_registry_disk_blob
/* Windows Registry HKLM\SYSTEM\MountedDevices uses a blob of data
* to store partitions. This blob is described here:
* http://www.goodells.net/multiboot/partsigs.shtml
* The following function maps this blob to a libguestfs partition
* name, if possible.
*/
static char *
map_registry_disk_blob (guestfs_h *g, const void *blob)
{
CLEANUP_FREE_STRING_LIST char **devices = NULL;
CLEANUP_FREE_PARTITION_LIST struct guestfs_partition_list *partitions = NULL;
size_t i, j, len;
uint64_t part_offset;
/* First 4 bytes are the disk ID. Search all devices to find the
* disk with this disk ID.
*/
devices = guestfs_list_devices (g);
if (devices == NULL)
return NULL;
for (i = 0; devices[i] != NULL; ++i) {
/* Read the disk ID. */
CLEANUP_FREE char *diskid =
guestfs_pread_device (g, devices[i], 4, 0x01b8, &len);
if (diskid == NULL)
continue;
if (len < 4)
continue;
if (memcmp (diskid, blob, 4) == 0) /* found it */
goto found_disk;
}
return NULL;
found_disk:
/* Next 8 bytes are the offset of the partition in bytes(!) given as
* a 64 bit little endian number. Luckily it's easy to get the
* partition byte offset from guestfs_part_list.
*/
memcpy (&part_offset, (char *) blob + 4, sizeof (part_offset));
part_offset = le64toh (part_offset);
partitions = guestfs_part_list (g, devices[i]);
if (partitions == NULL)
return NULL;
for (j = 0; j < partitions->len; ++j) {
if (partitions->val[j].part_start == part_offset) /* found it */
goto found_partition;
}
return NULL;
found_partition:
/* Construct the full device name. */
return safe_asprintf (g, "%s%d", devices[i], partitions->val[j].part_num);
}
示例10: run_qemu_img_info
static int
run_qemu_img_info (guestfs_h *g, const char *filename,
cmd_stdout_callback fn, void *data)
{
char *abs_filename = NULL;
char *safe_filename = NULL;
struct command *cmd;
int r;
if (guestfs___lazy_make_tmpdir (g) == -1)
return -1;
safe_filename = safe_asprintf (g, "%s/format.%d", g->tmpdir, ++g->unique);
/* 'filename' must be an absolute path so we can link to it. */
abs_filename = realpath (filename, NULL);
if (abs_filename == NULL) {
perrorf (g, "realpath");
goto error;
}
if (symlink (abs_filename, safe_filename) == -1) {
perrorf (g, "symlink");
goto error;
}
cmd = guestfs___new_command (g);
guestfs___cmd_add_arg (cmd, "qemu-img");
guestfs___cmd_add_arg (cmd, "info");
guestfs___cmd_add_arg (cmd, safe_filename);
guestfs___cmd_set_stdout_callback (cmd, fn, data, 0);
r = guestfs___cmd_run (cmd);
guestfs___cmd_close (cmd);
if (r == -1)
goto error;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
error (g, _("qemu-img: %s: child process failed"), filename);
goto error;
}
free (safe_filename);
free (abs_filename);
return 0;
error:
free (safe_filename);
free (abs_filename);
return -1;
}
示例11: guestfs_int_lazy_make_tmpdir
/* The g->tmpdir (per-handle temporary directory) is not created when
* the handle is created. Instead we create it lazily before the
* first time it is used, or during launch.
*/
int
guestfs_int_lazy_make_tmpdir (guestfs_h *g)
{
if (!g->tmpdir) {
CLEANUP_FREE char *tmpdir = guestfs_get_tmpdir (g);
g->tmpdir = safe_asprintf (g, "%s/libguestfsXXXXXX", tmpdir);
if (mkdtemp (g->tmpdir) == NULL) {
perrorf (g, _("%s: cannot create temporary directory"), g->tmpdir);
free (g->tmpdir);
g->tmpdir = NULL;
return -1;
}
}
return 0;
}
示例12: icon_cirros
static char *
icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
{
char *ret;
CLEANUP_FREE char *type = NULL;
CLEANUP_FREE char *local = NULL;
CLEANUP_FREE char *pngfile = NULL;
CLEANUP_CMD_CLOSE struct command *cmd = guestfs___new_command (g);
int r;
r = guestfs_is_file_opts (g, CIRROS_LOGO,
GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1);
if (r == -1)
return NULL; /* a real error */
if (r == 0)
return NOT_FOUND;
/* Check the file type and geometry. */
type = guestfs_file (g, CIRROS_LOGO);
if (!type)
return NOT_FOUND;
if (!STRPREFIX (type, "ASCII text"))
return NOT_FOUND;
local = guestfs___download_to_tmp (g, fs, CIRROS_LOGO, "icon", 1024);
if (!local)
return NOT_FOUND;
/* Use pbmtext to render it. */
pngfile = safe_asprintf (g, "%s/cirros.png", g->tmpdir);
guestfs___cmd_add_string_unquoted (cmd, PBMTEXT " < ");
guestfs___cmd_add_string_quoted (cmd, local);
guestfs___cmd_add_string_unquoted (cmd, " | " PNMTOPNG " > ");
guestfs___cmd_add_string_quoted (cmd, pngfile);
r = guestfs___cmd_run (cmd);
if (r == -1)
return NOT_FOUND;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
return NOT_FOUND;
/* Read it into memory. */
if (read_whole_file (g, pngfile, &ret, size_r) == -1)
return NULL;
return ret;
}
示例13: format_time
/* Malloc's */
char * format_time(unsigned long int secs)
{
unsigned int days, hours, minutes, seconds;
char * str;
days = secs / (24 * 60 * 60);
secs -= days * (24 * 60 * 60);
hours = secs / (60 * 60);
secs -= hours * (60 * 60);
minutes = secs / 60;
secs -= minutes * 60;
seconds = secs;
safe_asprintf(&str,"%ud %uh %um %us", days, hours, minutes, seconds);
return str;
}
示例14: create_cow_overlay_direct
static char *
create_cow_overlay_direct (guestfs_h *g, void *datav, struct drive *drv)
{
char *overlay = NULL;
CLEANUP_FREE char *backing_drive = NULL;
CLEANUP_CMD_CLOSE struct command *cmd = guestfs___new_command (g);
int r;
backing_drive = guestfs___drive_source_qemu_param (g, &drv->src);
if (!backing_drive)
goto error;
if (guestfs___lazy_make_tmpdir (g) == -1)
goto error;
overlay = safe_asprintf (g, "%s/overlay%d", g->tmpdir, ++g->unique);
guestfs___cmd_add_arg (cmd, "qemu-img");
guestfs___cmd_add_arg (cmd, "create");
guestfs___cmd_add_arg (cmd, "-f");
guestfs___cmd_add_arg (cmd, "qcow2");
guestfs___cmd_add_arg (cmd, "-b");
guestfs___cmd_add_arg (cmd, backing_drive);
if (drv->src.format) {
guestfs___cmd_add_arg (cmd, "-o");
guestfs___cmd_add_arg_format (cmd, "backing_fmt=%s", drv->src.format);
}
guestfs___cmd_add_arg (cmd, overlay);
r = guestfs___cmd_run (cmd);
if (r == -1)
goto error;
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
guestfs___external_command_failed (g, r, "qemu-img create", backing_drive);
goto error;
}
/* Caller sets g->overlay in the handle to this, and then manages
* the memory.
*/
return overlay;
error:
free (overlay);
return NULL;
}
示例15: guestfs_int_lazy_make_supermin_appliance_dir
/**
* Create the supermin appliance directory under cachedir, if it does
* not exist.
*
* Sanity-check that the permissions on the cachedir are safe, in case
* it has been pre-created maliciously or tampered with.
*
* Returns the directory name which the caller must free.
*/
char *
guestfs_int_lazy_make_supermin_appliance_dir (guestfs_h *g)
{
CLEANUP_FREE char *tmpdir = guestfs_get_cachedir (g);
char *ret = NULL;
struct stat statbuf;
uid_t uid = geteuid ();
ret = safe_asprintf (g, "%s/.guestfs-%ju", tmpdir, (uintmax_t) uid);
ignore_value (mkdir (ret, 0755));
ignore_value (chmod (ret, 0755)); /* RHBZ#921292 */
/* See if the cache directory exists and passes some simple checks
* to make sure it has not been tampered with.
*/
if (lstat (ret, &statbuf) == -1) {
perrorf (g, _("stat: %s"), ret);
free (ret);
return NULL;
}
if (statbuf.st_uid != uid) {
error (g, _("security: cached appliance %s is not owned by UID %ju"),
ret, (uintmax_t) uid);
free (ret);
return NULL;
}
if (!S_ISDIR (statbuf.st_mode)) {
error (g, _("security: cached appliance %s is not a directory (mode %o)"),
ret, statbuf.st_mode);
free (ret);
return NULL;
}
if ((statbuf.st_mode & 0022) != 0) {
error (g, _("security: cached appliance %s is writable by group or other (mode %o)"),
ret, statbuf.st_mode);
free (ret);
return NULL;
}
/* "Touch" the directory. */
ignore_value (utimes (ret, NULL));
return ret;
}