<commit_before>/*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */

#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>

#include <xenctrl.h>
#include <xenguest.h>
#include <xs.h>
#include <xen/hvm/hvm_info_table.h>
#include <xen/hvm/params.h>
#include <xen/hvm/e820.h>
#include <sys/mman.h>

#define CAML_NAME_SPACE
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/signals.h>
#include <caml/fail.h>

#define _H(__h) ((xc_interface *)(__h))
#define _D(__d) ((uint32_t)Int_val(__d))

#define None_val (Val_int(0))

#ifndef HVM_PARAM_NX_ENABLED
#define XEN_UNSTABLE
#endif 

#ifndef HVM_PARAM_VIRIDIAN
#warning missing viridian parameter
#endif

#include <stdio.h>

/* The following boolean flags are all set by their value
   in the platform area of xenstore. The only value that
   is considered true is the string 'true' */
struct flags {
  int vcpus;
  int vcpus_current;
  const char** vcpu_affinity; /* 0 means unset */
  uint16_t vcpu_weight;   /* 0 means unset (0 is an illegal weight) */
  uint16_t vcpu_cap;      /* 0 is default (no cap) */
  int nx;
  int viridian;
  int pae;
  int acpi;
  int apic;
  int acpi_s3;
  int acpi_s4;
};

static int pasprintf(char **buf, const char *fmt, ...)
{
    va_list ap;
    int ret = 0;

    if (*buf)
        free(*buf);
    va_start(ap, fmt);
    if (vasprintf(buf, fmt, ap) == -1) {
        buf = NULL;
        ret = -1;
    }
    va_end(ap);
    return ret;
}

static char *
xenstore_getsv(int domid, const char *fmt, va_list ap)
{
    char *path = NULL, *s;
    uint64_t value = 0;
    struct xs_handle *xsh = NULL;
	int n, m;
	char key[1024];

	bzero(key, sizeof(key));

    xsh = xs_daemon_open();
    if (xsh == NULL)
           return 0;

    path = xs_get_domain_path(xsh, domid);
    if (path == NULL)
        goto out;

	n = snprintf(key, sizeof(key), "%s/platform/", path);
	if (n < 0)
	  goto out;
	m = vsnprintf(key + n, sizeof(key) - n, fmt, ap);
	if (m < 0)
	  goto out;

    s = xs_read(xsh, XBT_NULL, key, NULL);
  out:
    xs_daemon_close(xsh);
    free(path);
    return s;
}

static char *
xenstore_gets(int domid, const char *fmt, ...)
{
	char *s;
	va_list ap;

	va_start(ap, fmt);
	s = xenstore_getsv(domid, fmt, ap);
	va_end(ap);
	return s;
}

static uint64_t
xenstore_get(int domid, const char *fmt, ...)
{
	char *s;
	uint64_t value;
	va_list ap;

	va_start(ap, fmt);
	s = xenstore_getsv(domid, fmt, ap);
    if (s) {
       if (!strcasecmp(s, "true"))
           value = 1;
       else if (sscanf(s, "%Ld", &value) != 1)
           value = 0;
       free(s);<commit_msg>CP-2257: fix use of uninitialised variable error.<commit_after>/*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */

#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>

#include <xenctrl.h>
#include <xenguest.h>
#include <xs.h>
#include <xen/hvm/hvm_info_table.h>
#include <xen/hvm/params.h>
#include <xen/hvm/e820.h>
#include <sys/mman.h>

#define CAML_NAME_SPACE
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/signals.h>
#include <caml/fail.h>

#define _H(__h) ((xc_interface *)(__h))
#define _D(__d) ((uint32_t)Int_val(__d))

#define None_val (Val_int(0))

#ifndef HVM_PARAM_NX_ENABLED
#define XEN_UNSTABLE
#endif 

#ifndef HVM_PARAM_VIRIDIAN
#warning missing viridian parameter
#endif

#include <stdio.h>

/* The following boolean flags are all set by their value
   in the platform area of xenstore. The only value that
   is considered true is the string 'true' */
struct flags {
  int vcpus;
  int vcpus_current;
  const char** vcpu_affinity; /* 0 means unset */
  uint16_t vcpu_weight;   /* 0 means unset (0 is an illegal weight) */
  uint16_t vcpu_cap;      /* 0 is default (no cap) */
  int nx;
  int viridian;
  int pae;
  int acpi;
  int apic;
  int acpi_s3;
  int acpi_s4;
};

static int pasprintf(char **buf, const char *fmt, ...)
{
    va_list ap;
    int ret = 0;

    if (*buf)
        free(*buf);
    va_start(ap, fmt);
    if (vasprintf(buf, fmt, ap) == -1) {
        buf = NULL;
        ret = -1;
    }
    va_end(ap);
    return ret;
}

static char *
xenstore_getsv(int domid, const char *fmt, va_list ap)
{
    char *path = NULL, *s;
    uint64_t value = 0;
    struct xs_handle *xsh = NULL;
	int n, m;
	char key[1024];

	bzero(key, sizeof(key));

    xsh = xs_daemon_open();
    if (xsh == NULL)
           return 0;

    path = xs_get_domain_path(xsh, domid);
    if (path == NULL)
        goto out;

	n = snprintf(key, sizeof(key), "%s/platform/", path);
	if (n < 0)
	  goto out;
	m = vsnprintf(key + n, sizeof(key) - n, fmt, ap);
	if (m < 0)
	  goto out;

    s = xs_read(xsh, XBT_NULL, key, NULL);
  out:
    xs_daemon_close(xsh);
    free(path);
    return s;
}

static char *
xenstore_gets(int domid, const char *fmt, ...)
{
	char *s;
	va_list ap;

	va_start(ap, fmt);
	s = xenstore_getsv(domid, fmt, ap);
	va_end(ap);
	return s;
}

static uint64_t
xenstore_get(int domid, const char *fmt, ...)
{
	char *s;
	uint64_t value = 0;
	va_list ap;

	va_start(ap, fmt);
	s = xenstore_getsv(domid, fmt, ap);
    if (s) {
       if (!strcasecmp(s, "true"))
           value = 1;
       else if (sscanf(s, "%Ld", &value) != 1)
           value = 0;
       free(s);
{'fraction_non_alphanumeric': 0.12983770287141075, 'fraction_numerical': 0.015137328339575531, 'mean_word_length': 3.1589876703439326, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2623504', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2428, 'n_words': 762}