@@ -2035,61 +2035,56 @@ static long sysconf_exact(int name, const char *name_str, Error **errp)
2035
2035
* Written members remain unmodified on error.
2036
2036
*/
2037
2037
static void transfer_vcpu (GuestLogicalProcessor * vcpu , bool sys2vcpu ,
2038
- Error * * errp )
2038
+ char * dirpath , Error * * errp )
2039
2039
{
2040
- char * dirpath ;
2040
+ int fd ;
2041
+ int res ;
2041
2042
int dirfd ;
2043
+ static const char fn [] = "online" ;
2042
2044
2043
- dirpath = g_strdup_printf ("/sys/devices/system/cpu/cpu%" PRId64 "/" ,
2044
- vcpu -> logical_id );
2045
2045
dirfd = open (dirpath , O_RDONLY | O_DIRECTORY );
2046
2046
if (dirfd == -1 ) {
2047
2047
error_setg_errno (errp , errno , "open(\"%s\")" , dirpath );
2048
- } else {
2049
- static const char fn [] = "online" ;
2050
- int fd ;
2051
- int res ;
2052
-
2053
- fd = openat (dirfd , fn , sys2vcpu ? O_RDONLY : O_RDWR );
2054
- if (fd == -1 ) {
2055
- if (errno != ENOENT ) {
2056
- error_setg_errno (errp , errno , "open(\"%s/%s\")" , dirpath , fn );
2057
- } else if (sys2vcpu ) {
2058
- vcpu -> online = true;
2059
- vcpu -> can_offline = false;
2060
- } else if (!vcpu -> online ) {
2061
- error_setg (errp , "logical processor #%" PRId64 " can't be "
2062
- "offlined" , vcpu -> logical_id );
2063
- } /* otherwise pretend successful re-onlining */
2064
- } else {
2065
- unsigned char status ;
2066
-
2067
- res = pread (fd , & status , 1 , 0 );
2068
- if (res == -1 ) {
2069
- error_setg_errno (errp , errno , "pread(\"%s/%s\")" , dirpath , fn );
2070
- } else if (res == 0 ) {
2071
- error_setg (errp , "pread(\"%s/%s\"): unexpected EOF" , dirpath ,
2072
- fn );
2073
- } else if (sys2vcpu ) {
2074
- vcpu -> online = (status != '0' );
2075
- vcpu -> can_offline = true;
2076
- } else if (vcpu -> online != (status != '0' )) {
2077
- status = '0' + vcpu -> online ;
2078
- if (pwrite (fd , & status , 1 , 0 ) == -1 ) {
2079
- error_setg_errno (errp , errno , "pwrite(\"%s/%s\")" , dirpath ,
2080
- fn );
2081
- }
2082
- } /* otherwise pretend successful re-(on|off)-lining */
2048
+ return ;
2049
+ }
2083
2050
2084
- res = close (fd );
2085
- g_assert (res == 0 );
2086
- }
2051
+ fd = openat (dirfd , fn , sys2vcpu ? O_RDONLY : O_RDWR );
2052
+ if (fd == -1 ) {
2053
+ if (errno != ENOENT ) {
2054
+ error_setg_errno (errp , errno , "open(\"%s/%s\")" , dirpath , fn );
2055
+ } else if (sys2vcpu ) {
2056
+ vcpu -> online = true;
2057
+ vcpu -> can_offline = false;
2058
+ } else if (!vcpu -> online ) {
2059
+ error_setg (errp , "logical processor #%" PRId64 " can't be "
2060
+ "offlined" , vcpu -> logical_id );
2061
+ } /* otherwise pretend successful re-onlining */
2062
+ } else {
2063
+ unsigned char status ;
2064
+
2065
+ res = pread (fd , & status , 1 , 0 );
2066
+ if (res == -1 ) {
2067
+ error_setg_errno (errp , errno , "pread(\"%s/%s\")" , dirpath , fn );
2068
+ } else if (res == 0 ) {
2069
+ error_setg (errp , "pread(\"%s/%s\"): unexpected EOF" , dirpath ,
2070
+ fn );
2071
+ } else if (sys2vcpu ) {
2072
+ vcpu -> online = (status != '0' );
2073
+ vcpu -> can_offline = true;
2074
+ } else if (vcpu -> online != (status != '0' )) {
2075
+ status = '0' + vcpu -> online ;
2076
+ if (pwrite (fd , & status , 1 , 0 ) == -1 ) {
2077
+ error_setg_errno (errp , errno , "pwrite(\"%s/%s\")" , dirpath ,
2078
+ fn );
2079
+ }
2080
+ } /* otherwise pretend successful re-(on|off)-lining */
2087
2081
2088
- res = close (dirfd );
2082
+ res = close (fd );
2089
2083
g_assert (res == 0 );
2090
2084
}
2091
2085
2092
- g_free (dirpath );
2086
+ res = close (dirfd );
2087
+ g_assert (res == 0 );
2093
2088
}
2094
2089
2095
2090
GuestLogicalProcessorList * qmp_guest_get_vcpus (Error * * errp )
@@ -2107,17 +2102,21 @@ GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
2107
2102
while (local_err == NULL && current < sc_max ) {
2108
2103
GuestLogicalProcessor * vcpu ;
2109
2104
GuestLogicalProcessorList * entry ;
2110
-
2111
- vcpu = g_malloc0 (sizeof * vcpu );
2112
- vcpu -> logical_id = current ++ ;
2113
- vcpu -> has_can_offline = true; /* lolspeak ftw */
2114
- transfer_vcpu (vcpu , true, & local_err );
2115
-
2116
- entry = g_malloc0 (sizeof * entry );
2117
- entry -> value = vcpu ;
2118
-
2119
- * link = entry ;
2120
- link = & entry -> next ;
2105
+ int64_t id = current ++ ;
2106
+ char * path = g_strdup_printf ("/sys/devices/system/cpu/cpu%" PRId64 "/" ,
2107
+ id );
2108
+
2109
+ if (g_file_test (path , G_FILE_TEST_EXISTS )) {
2110
+ vcpu = g_malloc0 (sizeof * vcpu );
2111
+ vcpu -> logical_id = id ;
2112
+ vcpu -> has_can_offline = true; /* lolspeak ftw */
2113
+ transfer_vcpu (vcpu , true, path , & local_err );
2114
+ entry = g_malloc0 (sizeof * entry );
2115
+ entry -> value = vcpu ;
2116
+ * link = entry ;
2117
+ link = & entry -> next ;
2118
+ }
2119
+ g_free (path );
2121
2120
}
2122
2121
2123
2122
if (local_err == NULL ) {
@@ -2138,7 +2137,11 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)
2138
2137
2139
2138
processed = 0 ;
2140
2139
while (vcpus != NULL ) {
2141
- transfer_vcpu (vcpus -> value , false, & local_err );
2140
+ char * path = g_strdup_printf ("/sys/devices/system/cpu/cpu%" PRId64 "/" ,
2141
+ vcpus -> value -> logical_id );
2142
+
2143
+ transfer_vcpu (vcpus -> value , false, path , & local_err );
2144
+ g_free (path );
2142
2145
if (local_err != NULL ) {
2143
2146
break ;
2144
2147
}
0 commit comments