dm: Fix some issues from string operations
The patch fix some string operations issues and also improve readability of several snippet. Tracked-On: #2133 Signed-off-by: Shuo A Liu <shuo.a.liu@intel.com> Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
This commit is contained in:
parent
20d0e666ff
commit
3e0b06cfd6
|
@ -185,8 +185,10 @@ static int
|
|||
pincpu_parse(const char *opt)
|
||||
{
|
||||
int vcpu, pcpu;
|
||||
char *cp;
|
||||
|
||||
if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
|
||||
if (dm_strtoi(opt, &cp, 10, &vcpu) || *cp != ':' ||
|
||||
dm_strtoi(cp + 1, &cp, 10, &pcpu)) {
|
||||
fprintf(stderr, "invalid format: %s\n", opt);
|
||||
return -1;
|
||||
}
|
||||
|
@ -804,7 +806,7 @@ dm_run(int argc, char *argv[])
|
|||
}
|
||||
break;
|
||||
case 'c':
|
||||
guest_ncpus = atoi(optarg);
|
||||
dm_strtoi(optarg, NULL, 0, &guest_ncpus);
|
||||
break;
|
||||
case 'E':
|
||||
if (acrn_parse_elf(optarg) != 0)
|
||||
|
@ -1060,10 +1062,10 @@ int main(int argc, char *argv[])
|
|||
switch (c) {
|
||||
case CMD_OPT_VMCFG:
|
||||
vmcfg = 1;
|
||||
index = atoi(optarg);
|
||||
dm_strtoi(optarg, NULL, 0, &index);
|
||||
break;
|
||||
case CMD_OPT_DUMP:
|
||||
index = atoi(optarg);
|
||||
dm_strtoi(optarg, NULL, 0, &index);
|
||||
vmcfg_dump(index, long_options, optstr);
|
||||
return 0;
|
||||
default:
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "dm.h"
|
||||
#include "vmmapi.h"
|
||||
#include "acpi.h"
|
||||
#include "inout.h"
|
||||
|
@ -155,41 +156,34 @@ pci_parse_slot_usage(char *aopt)
|
|||
int
|
||||
parse_bdf(char *s, int *bus, int *dev, int *func, int base)
|
||||
{
|
||||
int i;
|
||||
int nums[3] = {-1, -1, -1};
|
||||
char *str;
|
||||
char *s_bus, *s_dev, *s_func;
|
||||
char *str, *cp;
|
||||
int ret = 0;
|
||||
|
||||
if (bus) *bus = 0;
|
||||
if (dev) *dev = 0;
|
||||
if (func) *func = 0;
|
||||
str = s;
|
||||
for (i = 0, errno = 0; i < 3; i++) {
|
||||
nums[i] = (int)strtol(str, &str, base);
|
||||
if (errno == ERANGE || *str == '\0' || s == str)
|
||||
break;
|
||||
str++;
|
||||
str = cp = strdup(s);
|
||||
bus ? *bus = 0 : 0;
|
||||
dev ? *dev = 0 : 0;
|
||||
func ? *func = 0 : 0;
|
||||
s_bus = s_dev = s_func = NULL;
|
||||
s_dev = strsep(&cp, ":/.");
|
||||
if (cp) {
|
||||
s_func = strsep(&cp, ":/.");
|
||||
if (cp) {
|
||||
s_bus = s_dev;
|
||||
s_dev = s_func;
|
||||
s_func = strsep(&cp, ":/.");
|
||||
}
|
||||
}
|
||||
|
||||
if (s == str || errno == ERANGE)
|
||||
{
|
||||
printf("%s: parse_bdf error!\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
switch (i) {
|
||||
case 0:
|
||||
if (dev) *dev = nums[0];
|
||||
break;
|
||||
case 1:
|
||||
if (dev) *dev = nums[0];
|
||||
if (func) *func = nums[1];
|
||||
break;
|
||||
case 2:
|
||||
if (bus) *bus = nums[0];
|
||||
if (dev) *dev = nums[1];
|
||||
if (func) *func = nums[2];
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
if (s_dev && dev)
|
||||
ret |= dm_strtoi(s_dev, &s_dev, base, dev);
|
||||
if (s_func && func)
|
||||
ret |= dm_strtoi(s_func, &s_func, base, func);
|
||||
if (s_bus && bus)
|
||||
ret |= dm_strtoi(s_bus, &s_bus, base, bus);
|
||||
free(str);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -208,35 +202,22 @@ pci_parse_slot(char *opt)
|
|||
}
|
||||
|
||||
emul = config = NULL;
|
||||
cp = strchr(str, ',');
|
||||
if (cp != NULL) {
|
||||
*cp = '\0';
|
||||
emul = cp + 1;
|
||||
cp = strchr(emul, ',');
|
||||
if (cp != NULL) {
|
||||
*cp = '\0';
|
||||
config = cp + 1;
|
||||
if (*config == 'b') {
|
||||
b = config;
|
||||
cp = config + 1;
|
||||
if (*cp == ',') {
|
||||
*cp = '\0';
|
||||
config = cp + 1;
|
||||
} else {
|
||||
b = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
cp = str;
|
||||
str = strsep(&cp, ",");
|
||||
if (cp) {
|
||||
emul = strsep(&cp, ",");
|
||||
/* for boot device */
|
||||
if (cp && *cp == 'b' && *(cp+1) == ',')
|
||||
b = strsep(&cp, ",");
|
||||
config = cp;
|
||||
} else {
|
||||
pci_parse_slot_usage(opt);
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* <bus>:<slot>:<func> */
|
||||
if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0) {
|
||||
fprintf(stderr, "pci bdf parse fail\n");
|
||||
if (parse_bdf(str, &bnum, &snum, &fnum, 10) != 0)
|
||||
snum = -1;
|
||||
}
|
||||
|
||||
if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
|
||||
fnum < 0 || fnum >= MAXFUNCS) {
|
||||
|
|
|
@ -52,10 +52,11 @@ int guest_domid = 1;
|
|||
int
|
||||
acrn_parse_gvtargs(char *arg)
|
||||
{
|
||||
if (parse_bdf(arg, &gvt_low_gm_sz, &gvt_high_gm_sz,
|
||||
&gvt_fence_sz, 10) != 0) {
|
||||
if (dm_strtoi(arg, &arg, 10, &gvt_low_gm_sz) != 0 ||
|
||||
dm_strtoi(arg, &arg, 10, &gvt_high_gm_sz) != 0 ||
|
||||
dm_strtoi(arg, &arg, 10, &gvt_fence_sz) != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("passed gvt-g optargs low_gm %d, high_gm %d, fence %d\n",
|
||||
gvt_low_gm_sz, gvt_high_gm_sz, gvt_fence_sz);
|
||||
|
||||
|
|
|
@ -186,6 +186,7 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
|
|||
uint8_t h_cfg[PCI_REGMAX + 1];
|
||||
uint32_t m_off, m_num;
|
||||
struct npk_reg_default_val *d;
|
||||
char *cp;
|
||||
|
||||
if (npk_in_use) {
|
||||
WPRINTF(("NPK is already in use\n"));
|
||||
|
@ -212,8 +213,8 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
|
|||
*/
|
||||
|
||||
/* get the master offset and the number for this guest */
|
||||
if ((opts == NULL) || (sscanf(opts, "%u/%u", &m_off, &m_num) != 2)
|
||||
|| !valid_param(m_off, m_num)) {
|
||||
if ((opts == NULL) || dm_strtoui(opts, &cp, 10, &m_off) || *cp != '/' ||
|
||||
dm_strtoui(cp + 1, &cp, 10, &m_num) || !valid_param(m_off, m_num)) {
|
||||
m_off = 256;
|
||||
m_num = 256;
|
||||
}
|
||||
|
@ -228,8 +229,7 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
|
|||
/* traverse the driver folder, and try to find the NPK BDF# */
|
||||
while ((dent = readdir(dir)) != NULL) {
|
||||
if (strncmp(dent->d_name, "0000:", 5) != 0 ||
|
||||
parse_bdf((dent->d_name + 5),
|
||||
&b, &s, &f, 10) != 0)
|
||||
parse_bdf((dent->d_name + 5), &b, &s, &f, 16) != 0)
|
||||
continue;
|
||||
else
|
||||
break;
|
||||
|
|
|
@ -208,7 +208,7 @@ connect_coreu_daemon()
|
|||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, COREU_SERVICE_NAME, sizeof(&COREU_SERVICE_NAME));
|
||||
strncpy(addr.sun_path, COREU_SERVICE_NAME, sizeof(addr.sun_path));
|
||||
|
||||
ret = connect(fd, &addr, sizeof(struct sockaddr_un));
|
||||
if (ret < 0) {
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <stdbool.h>
|
||||
#include "types.h"
|
||||
#include "vmm.h"
|
||||
#include "dm_string.h"
|
||||
|
||||
struct vmctx;
|
||||
extern int guest_ncpus;
|
||||
|
|
|
@ -8,23 +8,19 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "dm_string.h"
|
||||
|
||||
int
|
||||
dm_strtol(const char *s, char **end, unsigned int base, long *val)
|
||||
{
|
||||
if (!s)
|
||||
goto err;
|
||||
return -1;
|
||||
|
||||
*val = strtol(s, end, base);
|
||||
if (*end == s) {
|
||||
printf("ERROR! nothing covert for: %s!\n", s);
|
||||
goto err;
|
||||
}
|
||||
if ((end && *end == s) || errno == ERANGE)
|
||||
return -1;
|
||||
return 0;
|
||||
|
||||
err:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -35,7 +31,8 @@ dm_strtoi(const char *s, char **end, unsigned int base, int *val)
|
|||
|
||||
l_val = 0;
|
||||
ret = dm_strtol(s, end, base, &l_val);
|
||||
*val = (int)l_val;
|
||||
if (ret == 0)
|
||||
*val = (int)l_val;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -43,17 +40,12 @@ int
|
|||
dm_strtoul(const char *s, char **end, unsigned int base, unsigned long *val)
|
||||
{
|
||||
if (!s)
|
||||
goto err;
|
||||
return -1;
|
||||
|
||||
*val = strtoul(s, end, base);
|
||||
if (*end == s) {
|
||||
printf("ERROR! nothing covert for: %s!\n", s);
|
||||
goto err;
|
||||
}
|
||||
if ((end && *end == s) || errno == ERANGE)
|
||||
return -1;
|
||||
return 0;
|
||||
|
||||
err:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -64,6 +56,7 @@ dm_strtoui(const char *s, char **end, unsigned int base, unsigned int *val)
|
|||
|
||||
l_val = 0;
|
||||
ret = dm_strtoul(s, end, base, &l_val);
|
||||
*val = (unsigned int)l_val;
|
||||
if (ret == 0)
|
||||
*val = (unsigned int)l_val;
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue