예제 #1
0
파일: uuid_libc.c 프로젝트: openpts/openpts
/**
 * String -> UUID 
 */
PTS_UUID *getUuidFromString(char *str) {
    PTS_UUID *uuid;
    uuid_t uu;
    unsigned32 status;

    /* check */
    if (str == NULL) {
        LOG(LOG_ERR, "null input");
        return NULL;
    }

    uuid = xmalloc(sizeof(PTS_UUID));
    if (uuid == NULL) {
        LOG(LOG_ERR, "no memory");
        return NULL;
    }
    memset(uuid, 0, UUIDSIZE);

    /* cast is ok since there are only hex digits (<128) */
    uuid_from_string((unsigned char *)str, (uuid_p_t)uuid, &status);
    if (uuid_s_ok != status) {
        LOG(LOG_ERR, "getUuidFromString() - uuid_from_string failed UUID='%s': %s\n",
            str, uuid_s_message[status]);
        xfree(uuid);
        return NULL;
    }

    return uuid;
}
예제 #2
0
static int
command_efi_set(int argc, char *argv[])
{
	char *uuid, *var, *val;
	CHAR16 wvar[128];
	EFI_GUID guid;
	uint32_t status;
	EFI_STATUS err;

	if (argc != 4) {
		printf("efi-set uuid var new-value\n");
		return (CMD_ERROR);
	}
	uuid = argv[1];
	var = argv[2];
	val = argv[3];
	uuid_from_string(uuid, (uuid_t *)&guid, &status);
	if (status != uuid_s_ok) {
		printf("Invalid uuid %s %d\n", uuid, status);
		return (CMD_ERROR);
	}
	cpy8to16(var, wvar, sizeof(wvar));
	err = RS->SetVariable(wvar, &guid,
	    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
	    strlen(val) + 1, val);
	if (EFI_ERROR(err)) {
		printf("Failed to set variable: error %lu\n", EFI_ERROR_CODE(err));
		return (CMD_ERROR);
	}
	return (CMD_OK);
}
예제 #3
0
/*
 * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
 * CRYPT-PLAIN-name
 * CRYPT-LUKS1-00000000000000000000000000000000-name
 * CRYPT-TEMP-name
 */
static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
{
	char *ptr, uuid2[UUID_LEN] = {0};
	uuid_t uu;
	int i = 0;
	uint32_t ret;

	/* Remove '-' chars */
	if (uuid) {
		uuid_from_string(uuid, &uu, &ret);
		if (ret != uuid_s_ok) {
			printf("error in uuid_from_string(%s), err = %d\n", uuid, ret);
			for (ptr = uuid2, i = 0; i < UUID_LEN; i++) {
				if (uuid[i] != '-') {
					*ptr = uuid[i];
					ptr++;
				}
			}
		}
	}

	i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
		type ?: "", type ? "-" : "",
		uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
		name);

	log_dbg("DM-UUID is %s", buf);
	if (i >= buflen)
		log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
}
예제 #4
0
static int
command_efi_unset(int argc, char *argv[])
{
	char *uuid, *var;
	CHAR16 wvar[128];
	EFI_GUID guid;
	uint32_t status;
	EFI_STATUS err;

	if (argc != 3) {
		printf("efi-unset uuid var\n");
		return (CMD_ERROR);
	}
	uuid = argv[1];
	var = argv[2];
	uuid_from_string(uuid, (uuid_t *)&guid, &status);
	if (status != uuid_s_ok) {
		printf("Invalid uuid %s\n", uuid);
		return (CMD_ERROR);
	}
	cpy8to16(var, wvar, sizeof(wvar));
	err = RS->SetVariable(wvar, &guid, 0, 0, NULL);
	if (EFI_ERROR(err)) {
		printf("Failed to unset variable: error %lu\n", EFI_ERROR_CODE(err));
		return (CMD_ERROR);
	}
	return (CMD_OK);
}
예제 #5
0
int
cmd_pfs_create(const char *sel_path, const char *name,
	       uint8_t pfs_type, const char *uuid_str)
{
	hammer2_ioc_pfs_t pfs;
	int ecode = 0;
	int fd;
	uint32_t status;

	/*
	 * Default to MASTER if no uuid was specified.
	 * Default to SLAVE if a uuid was specified.
	 *
	 * When adding masters to a cluster, the new PFS must be added as
	 * a slave and then upgraded to ensure proper synchronization.
	 */
	if (pfs_type == HAMMER2_PFSTYPE_NONE) {
		if (uuid_str)
			pfs_type = HAMMER2_PFSTYPE_SLAVE;
		else
			pfs_type = HAMMER2_PFSTYPE_MASTER;
	}

	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
		return(1);
	bzero(&pfs, sizeof(pfs));
	snprintf(pfs.name, sizeof(pfs.name), "%s", name);
	pfs.pfs_type = pfs_type;
	if (uuid_str) {
		uuid_from_string(uuid_str, &pfs.pfs_clid, &status);
	} else {
		uuid_create(&pfs.pfs_clid, &status);
	}
	if (status == uuid_s_ok)
		uuid_create(&pfs.pfs_fsid, &status);
	if (status == uuid_s_ok) {
		if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
			if (errno == EEXIST) {
				fprintf(stderr,
					"NOTE: Typically the same name is "
					"used for cluster elements on "
					"different mounts,\n"
					"      but cluster elements on the "
					"same mount require unique names.\n"
					"pfs-create %s: already present\n",
					name);
			} else {
				perror("ioctl");
			}
			ecode = 1;
		}
	} else {
		fprintf(stderr, "hammer2: pfs_create: badly formed uuid\n");
		ecode = 1;
	}
	close(fd);
	return (ecode);
}
예제 #6
0
파일: libxl_uuid.c 프로젝트: CPFL/xen
int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
{
    uint32_t status;

    uuid_from_string(in, &uuid->uuid, &status);
    if (status != uuid_s_ok)
        return -1;
    return 0;
}
예제 #7
0
static int
smbios_type1_initializer(struct smbios_structure *template_entry,
    const char **template_strings, char *curaddr, char **endaddr,
    uint16_t *n, uint16_t *size)
{
	struct smbios_table_type1 *type1;

	smbios_generic_initializer(template_entry, template_strings,
	    curaddr, endaddr, n, size);
	type1 = (struct smbios_table_type1 *)curaddr;

	if (guest_uuid_str != NULL) {
		uuid_t		uuid;
		uint32_t	status;

		uuid_from_string(guest_uuid_str, &uuid, &status);
		if (status != uuid_s_ok)
			return (-1);

		uuid_enc_le(&type1->uuid, &uuid);
	} else {
		MD5_CTX		mdctx;
		u_char		digest[16];
		char		hostname[MAXHOSTNAMELEN];

		/*
		 * Universally unique and yet reproducible are an
		 * oxymoron, however reproducible is desirable in
		 * this case.
		 */
		if (gethostname(hostname, sizeof(hostname)))
			return (-1);

		MD5Init(&mdctx);
		MD5Update(&mdctx, vmname, strlen(vmname));
		MD5Update(&mdctx, hostname, sizeof(hostname));
		MD5Final(digest, &mdctx);

		/*
		 * Set the variant and version number.
		 */
		digest[6] &= 0x0F;
		digest[6] |= 0x30;	/* version 3 */
		digest[8] &= 0x3F;
		digest[8] |= 0x80;

		memcpy(&type1->uuid, digest, sizeof (digest));
	}

	return (0);
}
void get_bind (int server_type)
{
   uuid_t obj_uuid;
   unsigned32 status;
   rpc_ns_handle_t imp_ctxt;
   char *obj_uuid_string;
   char *entry_name;
   rpc_binding_handle_t *binding_handle;

   if (server_type == 1) {
      printf ( "Obtaining phone server's binding handle\n" );
      obj_uuid_string = PHON_OBJ_UUID;
      entry_name =      PHON_ENTRY_NAME;
      binding_handle =  &phon_bh;
   } else {
      printf ( "Obtaining address server's binding handle\n" );
      obj_uuid_string = ADDR_OBJ_UUID;
      entry_name =      ADDR_ENTRY_NAME;
      binding_handle =  &addr_bh;
   } /* endif */


   /* Create object UUID for phone directory from string.                     */
   uuid_from_string ( obj_uuid_string, &obj_uuid, &status );
   ERRCHK ( status );

   /* Set up context to import the bindings of the phone server.              */
   rpc_ns_binding_import_begin ( rpc_c_ns_syntax_dce, entry_name,
                                 look_v1_0_c_ifspec, &obj_uuid,
                                 &imp_ctxt, &status );
   ERRCHK ( status );

   /* Get the first binding handle of the phone server.                       */
   rpc_ns_binding_import_next ( imp_ctxt, binding_handle, &status );
   ERRCHK ( status );

   /* Release the context.                                                    */
   rpc_ns_binding_import_done ( &imp_ctxt, &status );
   ERRCHK ( status );

   if (server_type == 1) {
      printf ( "Phone server's binding done\n" );
   } else {
      printf ( "Address server's binding done\n" );
   } /* endif */
}
예제 #9
0
static void
test_uuid_main(int argc, char *argv[])
{
    struct uuid uuid;

    if (argc == 1) {
        uuid_generate(&uuid);
    } else if (argc == 2) {
        if (!uuid_from_string(&uuid, argv[1])) {
            ovs_fatal(0, "\"%s\" is not a valid UUID", argv[1]);
        }
    } else {
        ovs_fatal(0, "usage: %s [UUID]", argv[0]);
    }

    printf(UUID_FMT"\n", UUID_ARGS(&uuid));
}
예제 #10
0
void uuuid_from_string(const char* in, struct uuuid_t** uuuid, int* status)
{
	uint32_t st;
	struct uuuid_t* u;

	u = uuuid_new();

	uuid_from_string(in, &u->uuid, &st);

	if (st != uuid_s_ok) {
		uuuid_free(u);
		*status = UUUID_ERR;
		return;
	}

	*uuuid = u;
	*status = UUUID_OK;
}
예제 #11
0
static int
vmnet_get_mac_address_from_uuid(char *guest_uuid_str) {
/*
 * from vmn_create() in https://github.com/mist64/xhyve/blob/master/src/pci_virtio_vmnet.c
 */
  xpc_object_t interface_desc;
  uuid_t uuid;
  __block interface_ref iface;
  __block vmnet_return_t iface_status;
  dispatch_semaphore_t iface_created;
  dispatch_queue_t if_create_q;
  uint32_t uuid_status;

  interface_desc = xpc_dictionary_create(NULL, NULL, 0);
  xpc_dictionary_set_uint64(interface_desc, vmnet_operation_mode_key, VMNET_SHARED_MODE);

  uuid_from_string(guest_uuid_str, &uuid, &uuid_status);
  if (uuid_status != uuid_s_ok) {
    fprintf(stderr, "Invalid UUID\n");
    return -1;
  }

  xpc_dictionary_set_uuid(interface_desc, vmnet_interface_id_key, uuid);
  iface = NULL;
  iface_status = 0;

  if_create_q = dispatch_queue_create("org.xhyve.vmnet.create", DISPATCH_QUEUE_SERIAL);

  iface_created = dispatch_semaphore_create(0);

  iface = vmnet_start_interface(interface_desc, if_create_q,
    ^(vmnet_return_t status, xpc_object_t interface_param)
  {
    iface_status = status;
    if (status != VMNET_SUCCESS || !interface_param) {
      dispatch_semaphore_signal(iface_created);
      return;
    }

    printf("%s\n", xpc_dictionary_get_string(interface_param, vmnet_mac_address_key));

    dispatch_semaphore_signal(iface_created);
  });
예제 #12
0
void
ficlUuidFromString(FICL_VM *pVM)
{
#ifndef	TESTMAIN
	char	*uuid;
	uint32_t status;
#endif
	char	*uuidp;
	int	uuids;
	uuid_t	*u;

#if FICL_ROBUST > 1
	vmCheckStack(pVM, 2, 0);
#endif

	uuids = stackPopINT(pVM->pStack);
	uuidp = (char *) stackPopPtr(pVM->pStack);

#ifndef	TESTMAIN
	uuid = (char *)ficlMalloc(uuids + 1);
	if (!uuid)
		vmThrowErr(pVM, "Error: out of memory");
	strncpy(uuid, uuidp, uuids);
	uuid[uuids] = '\0';

	u = (uuid_t *)ficlMalloc(sizeof (*u));

	uuid_from_string(uuid, u, &status);
	ficlFree(uuid);
	if (status != uuid_s_ok) {
		ficlFree(u);
		u = NULL;
	}
#else
	u = NULL;
#endif
	stackPushPtr(pVM->pStack, u);


	return;
}
예제 #13
0
static void search_by_uuid(const char* uuid_string,
                           void (*cb)(void*, struct device_match*),
                           void* ctx)
{
	unsigned char uuid[16];
	uuid_from_string(uuid, uuid_string);
	for ( size_t i = 0; i < hds_used; i++ )
	{
		struct blockdevice* bdev = &hds[i]->bdev;
		if ( bdev->fs )
		{
			struct filesystem* fs = bdev->fs;
			if ( !(fs->flags & FILESYSTEM_FLAG_UUID) )
				continue;
			if ( memcmp(uuid, fs->uuid, 16) != 0 )
				continue;
			struct device_match match;
			match.path = hds[i]->path;
			match.bdev = bdev;
			cb(ctx, &match);
		}
		else if ( bdev->pt )
		{
			for ( size_t j = 0; j < bdev->pt->partitions_count; j++ )
			{
				struct partition* p = bdev->pt->partitions[j];
				if ( !p->bdev.fs )
					continue;
				struct filesystem* fs = p->bdev.fs;
				if ( !(fs->flags & FILESYSTEM_FLAG_UUID) )
					continue;
				if ( memcmp(uuid, fs->uuid, 16) != 0 )
					continue;
				struct device_match match;
				match.path = p->path;
				match.bdev = &p->bdev;
				cb(ctx, &match);
			}
		}
	}
}
예제 #14
0
int
cmd_pfs_create(const char *sel_path, const char *name,
	       uint8_t pfs_type, const char *uuid_str)
{
	hammer2_ioc_pfs_t pfs;
	int ecode = 0;
	int fd;
	uint32_t status;

	/*
	 * Default to MASTER
	 */
	if (pfs_type == DMSG_PFSTYPE_NONE) {
		pfs_type = HAMMER2_PFSTYPE_MASTER;
	}

	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
		return(1);
	bzero(&pfs, sizeof(pfs));
	snprintf(pfs.name, sizeof(pfs.name), "%s", name);
	pfs.pfs_type = pfs_type;
	if (uuid_str) {
		uuid_from_string(uuid_str, &pfs.pfs_clid, &status);
	} else {
		uuid_create(&pfs.pfs_clid, &status);
	}
	if (status == uuid_s_ok)
		uuid_create(&pfs.pfs_fsid, &status);
	if (status == uuid_s_ok) {
		if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
			perror("ioctl");
			ecode = 1;
		}
	} else {
		fprintf(stderr, "hammer2: pfs_create: badly formed uuid\n");
		ecode = 1;
	}
	close(fd);
	return (ecode);
}
예제 #15
0
int
main(int argc, char **argv)
{
    char str[1000];
    afsUUID u1, u2;

    uuid_create(&u1);

    uuid_to_string(&u1, str, sizeof(str));

    printf("u: %s\n", str);

    if (uuid_from_string(str, &u2)) {
	printf("failed to parse\n");
	return 0;
    }

    if (bcmp(&u1, &u2, sizeof(u1)) != 0)
	printf("u1 != u2\n");

    return 0;
}
예제 #16
0
파일: testmain.c 프로젝트: jkkm/xfsdump
int
sess_queries_byuuid(char *uu)
{
	u_int stat;
	uuid_t uuid;
	inv_session_t *ses;
	invt_pr_ctx_t prctx;

	uuid_from_string (uu, &uuid, &stat);
	printf("uuid = %s\n", uu);
	if (inv_get_session_byuuid(&uuid, &ses)) {
		if (!ses)
			return -1;
		prctx.index = 0;
		prctx.depth = PR_ALL;
		prctx.mobj.type = INVT_NULLTYPE;
		DEBUG_sessionprint(ses, 99, &prctx);
		inv_free_session(&ses);
		return 1;
	}

	return -1;
}
예제 #17
0
파일: replication.c 프로젝트: M3S/ovs-rstp
void
replication_run(void)
{
    if (!session) {
        return;
    }

    jsonrpc_session_run(session);

    for (int i = 0; jsonrpc_session_is_connected(session) && i < 50; i++) {
        struct jsonrpc_msg *msg;
        unsigned int seqno;

        seqno = jsonrpc_session_get_seqno(session);
        if (seqno != session_seqno || state == RPL_S_INIT) {
            session_seqno = seqno;
            request_ids_clear();
            struct jsonrpc_msg *request;
            request = jsonrpc_create_request("get_server_id",
                                             json_array_create_empty(), NULL);
            request_ids_add(request->id, NULL);
            jsonrpc_session_send(session, request);

            state = RPL_S_SERVER_ID_REQUESTED;
            VLOG_DBG("send server ID request.");
        }

        msg = jsonrpc_session_recv(session);
        if (!msg) {
            continue;
        }

        if (msg->type == JSONRPC_NOTIFY && state != RPL_S_ERR
            && !strcmp(msg->method, "update")) {
            if (msg->params->type == JSON_ARRAY
                && msg->params->u.array.n == 2
                && msg->params->u.array.elems[0]->type == JSON_STRING) {
                char *db_name = msg->params->u.array.elems[0]->u.string;
                struct ovsdb *db = find_db(db_name);
                if (db) {
                    struct ovsdb_error *error;
                    error = process_notification(msg->params->u.array.elems[1],
                                                 db);
                    if (error) {
                        ovsdb_error_assert(error);
                        state = RPL_S_ERR;
                    }
                }
            }
        } else if (msg->type == JSONRPC_REPLY) {
            struct ovsdb *db;
            if (!request_ids_lookup_and_free(msg->id, &db)) {
                VLOG_WARN("received unexpected reply");
                goto next;
            }

            switch (state) {
            case RPL_S_SERVER_ID_REQUESTED: {
                struct uuid uuid;
                if (msg->result->type != JSON_STRING ||
                    !uuid_from_string(&uuid, json_string(msg->result))) {
                    struct ovsdb_error *error;
                    error = ovsdb_error("get_server_id failed",
                                        "Server ID is not valid UUID");

                    ovsdb_error_assert(error);
                    state = RPL_S_ERR;
                    break;
                }

                if (uuid_equals(&uuid, &server_uuid)) {
                    struct ovsdb_error *error;
                    error = ovsdb_error("Server ID check failed",
                                        "Self replicating is not allowed");

                    ovsdb_error_assert(error);
                    state = RPL_S_ERR;
                    break;
                }

                struct jsonrpc_msg *request;
                request = jsonrpc_create_request("list_dbs",
                                                 json_array_create_empty(),
                                                 NULL);
                request_ids_add(request->id, NULL);
                jsonrpc_session_send(session, request);

                replication_dbs_destroy();
                replication_dbs = replication_db_clone(&local_dbs);
                state = RPL_S_DB_REQUESTED;
                break;
            }
            case RPL_S_DB_REQUESTED:
                if (msg->result->type != JSON_ARRAY) {
                    struct ovsdb_error *error;
                    error = ovsdb_error("list_dbs failed",
                                        "list_dbs response is not array");
                    ovsdb_error_assert(error);
                    state = RPL_S_ERR;
                } else {
                    size_t i;
                    for (i = 0; i < msg->result->u.array.n; i++) {
                        const struct json *name = msg->result->u.array.elems[i];
                        if (name->type == JSON_STRING) {
                            /* Send one schema request for each remote DB. */
                            const char *db_name = json_string(name);
                            struct ovsdb *db = find_db(db_name);
                            if (db) {
                                struct jsonrpc_msg *request =
                                    jsonrpc_create_request(
                                        "get_schema",
                                        json_array_create_1(
                                            json_string_create(db_name)),
                                        NULL);

                                request_ids_add(request->id, db);
                                jsonrpc_session_send(session, request);
                            }
                        }
                    }
                    VLOG_DBG("Send schema requests");
                    state = RPL_S_SCHEMA_REQUESTED;
                }
                break;

            case RPL_S_SCHEMA_REQUESTED: {
                struct ovsdb_schema *schema;
                struct ovsdb_error *error;

                error = ovsdb_schema_from_json(msg->result, &schema);
                if (error) {
                    ovsdb_error_assert(error);
                    state = RPL_S_ERR;
                }

                if (db != find_db(schema->name)) {
                    /* Unexpected schema. */
                    VLOG_WARN("unexpected schema %s", schema->name);
                    state = RPL_S_ERR;
                } else if (!ovsdb_schema_equal(schema, db->schema)) {
                    /* Schmea version mismatch. */
                    VLOG_INFO("Schema version mismatch, %s not replicated",
                              schema->name);
                    shash_find_and_delete(replication_dbs, schema->name);
                }
                ovsdb_schema_destroy(schema);

                /* After receiving schemas, reset the local databases that
                 * will be monitored and send out monitor requests for them. */
                if (hmap_is_empty(&request_ids)) {
                    struct shash_node *node, *next;

                    SHASH_FOR_EACH_SAFE (node, next, replication_dbs) {
                        db = node->data;
                        struct ovsdb_error *error = reset_database(db);
                        if (error) {
                            const char *db_name = db->schema->name;
                            shash_find_and_delete(replication_dbs, db_name);
                            ovsdb_error_assert(error);
                            VLOG_WARN("Failed to reset database, "
                                      "%s not replicated.", db_name);
                        }
                    }

                    if (shash_is_empty(replication_dbs)) {
                        VLOG_WARN("Nothing to replicate.");
                        state = RPL_S_ERR;
                    } else {
                        SHASH_FOR_EACH (node, replication_dbs) {
                            db = node->data;
                            struct ovsdb *db = node->data;
                            struct jsonrpc_msg *request =
                                create_monitor_request(db);

                            request_ids_add(request->id, db);
                            jsonrpc_session_send(session, request);
                            VLOG_DBG("Send monitor requests");
                            state = RPL_S_MONITOR_REQUESTED;
                        }
                    }
                }
                break;
            }

            case RPL_S_MONITOR_REQUESTED: {
                /* Reply to monitor requests. */
                struct ovsdb_error *error;
                error = process_notification(msg->result, db);
                if (error) {
                    ovsdb_error_assert(error);
                    state = RPL_S_ERR;
                } else {
                    /* Transition to replicating state after receiving
                     * all replies of "monitor" requests. */
                    if (hmap_is_empty(&request_ids)) {
                        VLOG_DBG("Listening to monitor updates");
                        state = RPL_S_REPLICATING;
                    }
                }
                break;
            }

            case RPL_S_ERR:
                /* Ignore all messages */
                break;

            case RPL_S_INIT:
            case RPL_S_REPLICATING:
            default:
                OVS_NOT_REACHED();
            }
        }
예제 #18
0
static void
parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
{
	char *cmd;
	char *ptr;
	int len;
	uint32_t status;

	while (ac) {
		cmd = *av;
		if ((ptr = strchr(cmd, '=')) != NULL)
			*ptr++ = 0;

		/*
		 * Basic assignment value test
		 */
		if (ptr == NULL) {
			fprintf(stderr,
				"option %s requires an assignment\n",
				cmd);
			exit(1);
		}

		status = uuid_s_ok;
		if (strcmp(cmd, "sync-beg-tid") == 0) {
			pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
		} else if (strcmp(cmd, "sync-end-tid") == 0) {
			pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
		} else if (strcmp(cmd, "shared-uuid") == 0) {
			uuid_from_string(ptr, &pfsd->shared_uuid, &status);
		} else if (strcmp(cmd, "unique-uuid") == 0) {
			uuid_from_string(ptr, &pfsd->unique_uuid, &status);
		} else if (strcmp(cmd, "label") == 0) {
			len = strlen(ptr);
			if (ptr[0] == '"' && ptr[len-1] == '"') {
				ptr[len-1] = 0;
				++ptr;
			} else if (ptr[0] == '"') {
				fprintf(stderr,
					"option %s: malformed string\n",
					cmd);
				exit(1);
			}
			snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
		} else if (strcmp(cmd, "snapshots") == 0) {
			len = strlen(ptr);
			if (ptr[0] != '/') {
				fprintf(stderr,
					"option %s: '%s' must be an "
					"absolute path\n", cmd, ptr);
				if (ptr[0] == 0) {
					fprintf(stderr, 
						"use 'snapshots-clear' "
						"to unset snapshots dir\n");
				}
				exit(1);
			}
			if (len >= (int)sizeof(pfsd->snapshots)) {
				fprintf(stderr,
					"option %s: path too long, %d "
					"character limit\n", cmd, len);
				exit(1);
			}
			snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
				 "%s", ptr);
		} else if (strcmp(cmd, "snapshots-clear") == 0) {
			pfsd->snapshots[0] = 0;
		} else if (strcmp(cmd, "prune-min") == 0) {
			pfsd->prune_min = timetosecs(ptr);
			if (pfsd->prune_min < 0) {
				fprintf(stderr,
					"option %s: illegal time spec, "
					"use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
				exit(1);
			}
		} else {
			fprintf(stderr, "invalid option: %s\n", cmd);
			exit(1);
		}
		if (status != uuid_s_ok) {
			fprintf(stderr, "option %s: error parsing uuid %s\n",
				cmd, ptr);
			exit(1);
		}
		--ac;
		++av;
	}
}
예제 #19
0
int
main(int ac, char **av)
{
	uint32_t status;
	hammer2_off_t total_space;
	hammer2_off_t free_space;
	hammer2_off_t reserved_space;
	int ch;
	int fd = -1;
	char *fsidstr;
	char *spfsidstr;
	char *rpfsidstr;

	/*
	 * Sanity check basic filesystem structures.  No cookies for us
	 * if it gets broken!
	 */
	assert(sizeof(hammer2_volume_data_t) == HAMMER2_VOLUME_BYTES);
	assert(sizeof(hammer2_inode_data_t) == HAMMER2_INODE_BYTES);
	assert(sizeof(hammer2_blockref_t) == HAMMER2_BLOCKREF_BYTES);

	/*
	 * Generate a filesystem id and lookup the filesystem type
	 */
	srandomdev();
	uuidgen(&Hammer2_FSId, 1);
	uuidgen(&Hammer2_SPFSId, 1);
	uuidgen(&Hammer2_RPFSId, 1);
	uuid_from_string(HAMMER2_UUID_STRING, &Hammer2_FSType, &status);
	/*uuid_name_lookup(&Hammer2_FSType, "DragonFly HAMMER2", &status);*/
	if (status != uuid_s_ok) {
		errx(1, "uuids file does not have the DragonFly "
			"HAMMER filesystem type");
	}

	/*
	 * Parse arguments
	 */
	while ((ch = getopt(ac, av, "fL:b:m:r:V:")) != -1) {
		switch(ch) {
		case 'f':
			ForceOpt = 1;
			break;
		case 'L':
			Label = optarg;
			if (strlen(Label) > HAMMER2_INODE_MAXNAME) {
				errx(1, "Root directory label too long "
					"(64 chars max)\n");
			}
			break;
		case 'b':
			BootAreaSize = getsize(optarg,
					 HAMMER2_NEWFS_ALIGN,
					 HAMMER2_BOOT_MAX_BYTES, 2);
			break;
		case 'r':
			AuxAreaSize = getsize(optarg,
					 HAMMER2_NEWFS_ALIGN,
					 HAMMER2_REDO_MAX_BYTES, 2);
			break;
		case 'V':
			Hammer2Version = strtol(optarg, NULL, 0);
			if (Hammer2Version < HAMMER2_VOL_VERSION_MIN ||
			    Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
				errx(1,
				     "I don't understand how to format "
				     "HAMMER2 version %d\n",
				     Hammer2Version);
			}
			break;
		default:
			usage();
			break;
		}
	}

	if (Hammer2Version < 0) {
		size_t olen = sizeof(Hammer2Version);
		Hammer2Version = HAMMER2_VOL_VERSION_DEFAULT;
		if (sysctlbyname("vfs.hammer2.supported_version",
				 &Hammer2Version, &olen, NULL, 0) == 0) {
			if (Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
				Hammer2Version = HAMMER2_VOL_VERSION_WIP - 1;
				fprintf(stderr,
					"newfs_hammer: WARNING: HAMMER2 VFS "
					"supports higher version than I "
					"understand,\n"
					"using version %d\n",
					Hammer2Version);
			}
		} else {
			fprintf(stderr,
				"newfs_hammer: WARNING: HAMMER2 VFS not "
				"loaded, cannot get version info.\n"
				"Using version %d\n",
				HAMMER2_VOL_VERSION_DEFAULT);
		}
	}

	/*
	 * Collect volume information.
	 */
	ac -= optind;
	av += optind;

	if (ac != 1) {
		fprintf(stderr, "Exactly one disk device must be specified\n");
		exit(1);
	}
	total_space = check_volume(av[0], &fd);

	/*
	 * ~typically 8MB alignment to avoid edge cases for reserved blocks
	 * and so raid stripes (if any) operate efficiently.
	 */
	total_space &= ~HAMMER2_VOLUME_ALIGNMASK64;

	/*
	 * Calculate defaults for the boot area size and round to the
	 * volume alignment boundary.
	 */
	if (BootAreaSize == 0) {
		BootAreaSize = HAMMER2_BOOT_NOM_BYTES;
		while (BootAreaSize > total_space / 20)
			BootAreaSize >>= 1;
		if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES)
			BootAreaSize = HAMMER2_BOOT_MIN_BYTES;
	} else if (BootAreaSize < HAMMER2_BOOT_MIN_BYTES) {
예제 #20
0
/*
 * Create an interface for the guest using Apple's vmnet framework.
 *
 * The interface works in VMNET_SHARED_MODE which allows for packets
 * of the guest to reach other guests and the Internet.
 *
 * See also: https://developer.apple.com/library/mac/documentation/vmnet/Reference/vmnet_Reference/index.html
 */
static int
vmn_create(struct pci_vtnet_softc *sc)
{
	xpc_object_t interface_desc;
	uuid_t uuid;
	__block interface_ref iface;
	__block vmnet_return_t iface_status;
	dispatch_semaphore_t iface_created;
	dispatch_queue_t if_create_q;
	dispatch_queue_t if_q;
	struct vmnet_state *vms;
	uint32_t uuid_status;

	interface_desc = xpc_dictionary_create(NULL, NULL, 0);
	xpc_dictionary_set_uint64(interface_desc, vmnet_operation_mode_key,
		VMNET_SHARED_MODE);

	if (guest_uuid_str != NULL) {
		uuid_from_string(guest_uuid_str, &uuid, &uuid_status);
		if (uuid_status != uuid_s_ok) {
			return (-1);
		}
	} else {
		uuid_generate_random(uuid);
	}

	xpc_dictionary_set_uuid(interface_desc, vmnet_interface_id_key, uuid);
	iface = NULL;
	iface_status = 0;

	vms = malloc(sizeof(struct vmnet_state));

	if (!vms) {
		return (-1);
	}

	if_create_q = dispatch_queue_create("org.xhyve.vmnet.create",
		DISPATCH_QUEUE_SERIAL);

	iface_created = dispatch_semaphore_create(0);

	iface = vmnet_start_interface(interface_desc, if_create_q,
		^(vmnet_return_t status, xpc_object_t interface_param)
	{
		iface_status = status;
		if (status != VMNET_SUCCESS || !interface_param) {
			dispatch_semaphore_signal(iface_created);
			return;
		}

		if (sscanf(xpc_dictionary_get_string(interface_param,
			vmnet_mac_address_key),
			"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
			&vms->mac[0], &vms->mac[1], &vms->mac[2], &vms->mac[3],
			&vms->mac[4], &vms->mac[5]) != 6)
		{
			assert(0);
		}

		vms->mtu = (unsigned)
			xpc_dictionary_get_uint64(interface_param, vmnet_mtu_key);
		vms->max_packet_size = (unsigned)
			xpc_dictionary_get_uint64(interface_param,
				vmnet_max_packet_size_key);
		dispatch_semaphore_signal(iface_created);
	});
예제 #21
0
파일: hdist-test.c 프로젝트: michals/sx
int main(int argc, char **argv)
{
    sxi_hdist_t *hdist = NULL, *hdist2 = NULL;
    unsigned int i, j, cfg_len;
    sx_uuid_t uuid;
    const sx_nodelist_t *nodelist;
    const sx_node_t *node;
    const void *cfg;
    int ret = 1;
    sxc_client_t *sx = server_init(NULL, NULL, NULL, 0, argc, argv);

    if(argc == 2 && !strcmp(argv[1], "--debug")) {
	log_setminlevel(sx, SX_LOG_DEBUG);
	dbg = 1;
    }

    if(!(hdist = sxi_hdist_new(1337, MAXBUILDS, NULL))) {
	CRIT("Can't build hdist");
	return 1;
    }

    for(i = 0; i < 5; i++) {
	uuid_from_string(&uuid, nodes[i].uuid);
	if(sxi_hdist_addnode(hdist, &uuid, nodes[i].addr, nodes[i].int_addr, nodes[i].capacity)) {
	    CRIT("addnode failed (1)");
	    goto main_err;
	}
    }

    if(sxi_hdist_build(hdist) != OK) {
	CRIT("Can't build distribution model (1)");
	goto main_err;
    }

    if(sxi_hdist_newbuild(hdist) != OK) {
	CRIT("Can't create new build (1)");
	goto main_err;
    }

    /* get nodes from build 1 (previous 0) */
    nodelist = sxi_hdist_nodelist(hdist, 1);
    if(!nodelist) {
	CRIT("sxi_hdist_nodelist failed");
	goto main_err;
    }
    DEBUG("Re-adding %d nodes from previous build", sx_nodelist_count(nodelist));
    for(i = 0; i < sx_nodelist_count(nodelist); i++) {
	node = sx_nodelist_get(nodelist, i);
	if(sxi_hdist_addnode(hdist, sx_node_uuid(node), sx_node_addr(node), sx_node_internal_addr(node), sx_node_capacity(node))) {
	    CRIT("addnode failed (2)");
	    goto main_err;
	}
    }

    DEBUG("Adding 3 new nodes");
    /* add 3 new nodes */
    for(i = 5; i < 8; i++) {
	uuid_from_string(&uuid, nodes[i].uuid);
	if(sxi_hdist_addnode(hdist, &uuid, nodes[i].addr, nodes[i].int_addr, nodes[i].capacity)) {
	    CRIT("addnode failed (3)");
	    goto main_err;
	}
    }

    if(sxi_hdist_build(hdist) != OK) {
	CRIT("Can't build distribution model (2)");
	goto main_err;
    }

    DEBUG("Number of builds: %d", sxi_hdist_buildcnt(hdist));

    DEBUG("Assuming the cluster was rebalanced");
    if(sxi_hdist_rebalanced(hdist) != OK) {
	CRIT("sxi_hdist_rebalanced failed");
	goto main_err;
    }

    DEBUG("Creating new build");
    if(sxi_hdist_newbuild(hdist) != OK) {
	CRIT("Can't create new build (2)");
	goto main_err;
    }

    /* get nodes from build 1 (previous 0) */
    nodelist = sxi_hdist_nodelist(hdist, 1);
    if(!nodelist) {
	CRIT("sxi_hdist_nodelist failed");
	goto main_err;
    }
    DEBUG("Re-adding %d nodes from previous build", sx_nodelist_count(nodelist));
    for(i = 0; i < sx_nodelist_count(nodelist); i++) {
	node = sx_nodelist_get(nodelist, i);
	if(sxi_hdist_addnode(hdist, sx_node_uuid(node), sx_node_addr(node), sx_node_internal_addr(node), sx_node_capacity(node))) {
	    CRIT("addnode failed (4)");
	    goto main_err;
	}
    }

    DEBUG("Adding 2 new nodes");
    /* add 2 new nodes */
    for(i = 8; i < 10; i++) {
	uuid_from_string(&uuid, nodes[i].uuid);
	if(sxi_hdist_addnode(hdist, &uuid, nodes[i].addr, nodes[i].int_addr, nodes[i].capacity)) {
	    CRIT("addnode failed (4)");
	    goto main_err;
	}
    }

    if(sxi_hdist_build(hdist) != OK) {
	CRIT("Can't build distribution model (3)");
	goto main_err;
    }
    DEBUG("Number of builds: %d", sxi_hdist_buildcnt(hdist));

    if(dbg) {
	print_nodes(hdist, 0);
	print_nodes(hdist, 1);
    }

    if((uint64_t) FINAL_CHECKSUM != sxi_hdist_checksum(hdist)) {
	CRIT("Unexpected checksum: %lld", (long long int) sxi_hdist_checksum(hdist));
	goto main_err;
    }

    DEBUG("*** Creating exact copy of HDIST based on existing config ***");
    if(sxi_hdist_get_cfg(hdist, &cfg, &cfg_len)) {
	CRIT("Can't get config");
	goto main_err;
    } else {
	DEBUG("Compressed config size: %u", (unsigned int) cfg_len);
    }

    if(!(hdist2 = sxi_hdist_from_cfg(cfg, cfg_len))) {
	CRIT("Can't build HDIST from config");
	goto main_err;
    }

    if(!sxi_hdist_same_origin(hdist, hdist2)) {
	CRIT("UUIDs are different for old and new model");
	goto main_err;
    }

    if(sxi_hdist_checksum(hdist) != sxi_hdist_checksum(hdist2)) {
	CRIT("Checksums don't match for original and copied build");
	goto main_err;
    } else {
	DEBUG("Models' checksums OK");
    }

    /* test bidx 0 (10 nodes) */
    for(i = 0; i < sizeof(hashes) / 8; i++)
	for(j = 1; j <= 10; j++)
	    if(locate_cmp(hdist, hdist2, hashes[i], j, 0))
		goto main_err;

    /* test bidx 1 (8 nodes) */
    for(i = 0; i < sizeof(hashes) / 8; i++)
	for(j = 1; j <= 8; j++)
	    if(locate_cmp(hdist, hdist2, hashes[i], j, 1))
		goto main_err;

    ret = 0;

main_err:

    sxi_hdist_free(hdist);
    sxi_hdist_free(hdist2);
    server_done(&sx);
    return ret;
}
예제 #22
0
int linda_rm(hlinda *h, const char *s)
{
	json *j = json_open(s);
	json *j1 = json_get_object(j);
	int is_int = 0, is_string = 0;
	const char *string_id = NULL;
	long long int_id = 0;
	uuid u;

	json *joid = json_find(j1, LINDA_OID);

	if (!joid)
	{
		json_close(j);
		return 0;
	}

	uuid_from_string(json_get_string(joid), &u);
	json *jid = json_find(j1, LINDA_ID);

	if (jid)
	{
		if (h->l->is_int && !json_is_integer(jid))
		{
			printf("linda_read: expected integer id\n");
			json_close(j);
			return 0;
		}
		else if (h->l->is_string && !json_is_string(jid))
		{
			printf("linda_read: expected string id\n");
			json_close(j);
			return 0;
		}

		if (json_is_integer(jid))
		{
			int_id = json_get_integer(jid);
			is_int = 1;
		}
		else if (json_is_string(jid))
		{
			string_id = json_get_string(jid);
			json_close(h->jquery);
			is_string = 1;
		}
		else
		{
			json_close(j);
			return 0;
		}
	}

	json_close(j);

	if (is_int)
	{
		char tmpbuf[1024];
		int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, int_id);
		store_hrem2(h->hst, &u, tmpbuf, tmplen);
		sb_int_uuid_erase(h->l->sl, int_id, &u);
	}
	else if (is_string)
	{
		char tmpbuf[1024], tmpbuf2[1024];
		json_format_string(string_id, tmpbuf2, sizeof(tmpbuf2));
		int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2);
		store_hrem2(h->hst, &u, tmpbuf, tmplen);
		sb_string_uuid_erase(h->l->sl, string_id, &u);
	}
	else
	{
		store_rem(h->l->st, &u);
		sb_uuid_efface(h->l->sl, &u);
	}

	return 1;
}
예제 #23
0
int linda_out(hlinda *h, const char *s)
{
	if (!h)
		return 0;

	json *j = json_open(s);
	json *j1 = json_get_object(j);
	json *joid = json_find(j1, LINDA_OID);
	uuid u;

	if (joid)
	{
		uuid_from_string(json_get_string(joid), &u);
	}
	else
		uuid_gen(&u);

	json *jid = json_find(j1, LINDA_ID);

	if (jid)
	{
		if (json_is_integer(jid))
		{
			long long k = json_get_integer(jid);

			if (h->l->sl && !h->l->is_int)
			{
				printf("linda_out: expected integer id\n");
				return 0;
			}

			if (!h->l->sl)
			{
				h->l->sl = sb_int_uuid_create2();
				h->l->is_int = 1;
			}

			sb_int_uuid_set(h->l->sl, k, &u);
		}
		else if (json_is_string(jid))
		{
			const char *k = json_get_string(jid);

			if (h->l->sl && !h->l->is_string)
			{
				printf("linda_out: expected string id\n");
				return 0;
			}

			if (!h->l->sl)
			{
				h->l->sl = sb_string_uuid_create2();
				h->l->is_string = 1;
			}

			sb_string_uuid_set(h->l->sl, k, &u);
		}
	}

	store_hadd(h->hst, &u, s, strlen(s));
	h->last_oid = u;
	json_close(j);
	return 0;
}
예제 #24
0
static int
command_efi_show(int argc, char *argv[])
{
	/*
	 * efi-show [-a]
	 *	print all the env
	 * efi-show -u UUID
	 *	print all the env vars tagged with UUID
	 * efi-show -v var
	 *	search all the env vars and print the ones matching var
	 * eif-show -u UUID -v var
	 * eif-show UUID var
	 *	print all the env vars that match UUID and var
	 */
	/* NB: We assume EFI_GUID is the same as uuid_t */
	int		aflag = 0, gflag = 0, lflag = 0, vflag = 0;
	int		ch, rv;
	unsigned	i;
	EFI_STATUS	status;
	EFI_GUID	varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
	EFI_GUID	matchguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
	uint32_t	uuid_status;
	CHAR16		*varname;
	CHAR16		*newnm;
	CHAR16		varnamearg[128];
	UINTN		varalloc;
	UINTN		varsz;

	while ((ch = getopt(argc, argv, "ag:lv:")) != -1) {
		switch (ch) {
		case 'a':
			aflag = 1;
			break;
		case 'g':
			gflag = 1;
			uuid_from_string(optarg, (uuid_t *)&matchguid,
			    &uuid_status);
			if (uuid_status != uuid_s_ok) {
				printf("uid %s could not be parsed\n", optarg);
				return (CMD_ERROR);
			}
			break;
		case 'l':
			lflag = 1;
			break;
		case 'v':
			vflag = 1;
			if (strlen(optarg) >= nitems(varnamearg)) {
				printf("Variable %s is longer than %zd characters\n",
				    optarg, nitems(varnamearg));
				return (CMD_ERROR);
			}
			for (i = 0; i < strlen(optarg); i++)
				varnamearg[i] = optarg[i];
			varnamearg[i] = 0;
			break;
		default:
			printf("Invalid argument %c\n", ch);
			return (CMD_ERROR);
		}
	}

	if (aflag && (gflag || vflag)) {
		printf("-a isn't compatible with -v or -u\n");
		return (CMD_ERROR);
	}

	if (aflag && optind < argc) {
		printf("-a doesn't take any args");
		return (CMD_ERROR);
	}

	if (optind == argc)
		aflag = 1;

	argc -= optind;
	argv += optind;

	pager_open();
	if (vflag && gflag) {
		rv = efi_print_var(varnamearg, &matchguid, lflag);
		pager_close();
		return (rv);
	}

	if (argc == 2) {
		optarg = argv[0];
		if (strlen(optarg) >= nitems(varnamearg)) {
			printf("Variable %s is longer than %zd characters\n",
			    optarg, nitems(varnamearg));
			pager_close();
			return (CMD_ERROR);
		}
		for (i = 0; i < strlen(optarg); i++)
			varnamearg[i] = optarg[i];
		varnamearg[i] = 0;
		optarg = argv[1];
		uuid_from_string(optarg, (uuid_t *)&matchguid,
		    &uuid_status);
		if (uuid_status != uuid_s_ok) {
			printf("uid %s could not be parsed\n", optarg);
			pager_close();
			return (CMD_ERROR);
		}
		rv = efi_print_var(varnamearg, &matchguid, lflag);
		pager_close();
		return (rv);
	}

	if (argc > 0) {
		printf("Too many args %d\n", argc);
		pager_close();
		return (CMD_ERROR);
	}

	/*
	 * Initiate the search -- note the standard takes pain
	 * to specify the initial call must be a poiner to a NULL
	 * character.
	 */
	varalloc = 1024;
	varname = malloc(varalloc);
	if (varname == NULL) {
		printf("Can't allocate memory to get variables\n");
		pager_close();
		return (CMD_ERROR);
	}
	varname[0] = 0;
	while (1) {
		varsz = varalloc;
		status = RS->GetNextVariableName(&varsz, varname, &varguid);
		if (status == EFI_BUFFER_TOO_SMALL) {
			varalloc = varsz;
			newnm = malloc(varalloc);
			if (newnm == NULL) {
				printf("Can't allocate memory to get variables\n");
				free(varname);
				pager_close();
				return (CMD_ERROR);
			}
			memcpy(newnm, varname, varsz);
			free(varname);
			varname = newnm;
			continue; /* Try again with bigger buffer */
		}
		if (status != EFI_SUCCESS)
			break;
		if (aflag) {
			if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
				break;
			continue;
		}
		if (vflag) {
			if (wcscmp(varnamearg, varname) == 0) {
				if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
					break;
				continue;
			}
		}
		if (gflag) {
			if (memcmp(&varguid, &matchguid, sizeof(varguid)) == 0) {
				if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
					break;
				continue;
			}
		}
	}
	free(varname);
	pager_close();

	return (CMD_OK);
}