Revision e446f70d monitor.c

b/monitor.c
2135 2135
{
2136 2136
    int fd;
2137 2137
    Monitor *mon = cur_mon;
2138
    MonFdset *mon_fdset = NULL;
2139
    MonFdsetFd *mon_fdset_fd;
2140 2138
    AddfdInfo *fdinfo;
2141 2139

  
2142 2140
    fd = qemu_chr_fe_get_msgfd(mon->chr);
......
2145 2143
        goto error;
2146 2144
    }
2147 2145

  
2148
    if (has_fdset_id) {
2149
        QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2150
            /* Break if match found or match impossible due to ordering by ID */
2151
            if (fdset_id <= mon_fdset->id) {
2152
                if (fdset_id < mon_fdset->id) {
2153
                    mon_fdset = NULL;
2154
                }
2155
                break;
2156
            }
2157
        }
2146
    fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2147
                                  has_opaque, opaque, errp);
2148
    if (fdinfo) {
2149
        return fdinfo;
2158 2150
    }
2159 2151

  
2160
    if (mon_fdset == NULL) {
2161
        int64_t fdset_id_prev = -1;
2162
        MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2163

  
2164
        if (has_fdset_id) {
2165
            if (fdset_id < 0) {
2166
                error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2167
                          "a non-negative value");
2168
                goto error;
2169
            }
2170
            /* Use specified fdset ID */
2171
            QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2172
                mon_fdset_cur = mon_fdset;
2173
                if (fdset_id < mon_fdset_cur->id) {
2174
                    break;
2175
                }
2176
            }
2177
        } else {
2178
            /* Use first available fdset ID */
2179
            QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2180
                mon_fdset_cur = mon_fdset;
2181
                if (fdset_id_prev == mon_fdset_cur->id - 1) {
2182
                    fdset_id_prev = mon_fdset_cur->id;
2183
                    continue;
2184
                }
2185
                break;
2186
            }
2187
        }
2188

  
2189
        mon_fdset = g_malloc0(sizeof(*mon_fdset));
2190
        if (has_fdset_id) {
2191
            mon_fdset->id = fdset_id;
2192
        } else {
2193
            mon_fdset->id = fdset_id_prev + 1;
2194
        }
2195

  
2196
        /* The fdset list is ordered by fdset ID */
2197
        if (!mon_fdset_cur) {
2198
            QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2199
        } else if (mon_fdset->id < mon_fdset_cur->id) {
2200
            QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2201
        } else {
2202
            QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2203
        }
2204
    }
2205

  
2206
    mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2207
    mon_fdset_fd->fd = fd;
2208
    mon_fdset_fd->removed = false;
2209
    if (has_opaque) {
2210
        mon_fdset_fd->opaque = g_strdup(opaque);
2211
    }
2212
    QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2213

  
2214
    fdinfo = g_malloc0(sizeof(*fdinfo));
2215
    fdinfo->fdset_id = mon_fdset->id;
2216
    fdinfo->fd = mon_fdset_fd->fd;
2217

  
2218
    return fdinfo;
2219

  
2220 2152
error:
2221 2153
    if (fd != -1) {
2222 2154
        close(fd);
......
2301 2233
    return fdset_list;
2302 2234
}
2303 2235

  
2236
AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2237
                                bool has_opaque, const char *opaque,
2238
                                Error **errp)
2239
{
2240
    MonFdset *mon_fdset = NULL;
2241
    MonFdsetFd *mon_fdset_fd;
2242
    AddfdInfo *fdinfo;
2243

  
2244
    if (has_fdset_id) {
2245
        QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2246
            /* Break if match found or match impossible due to ordering by ID */
2247
            if (fdset_id <= mon_fdset->id) {
2248
                if (fdset_id < mon_fdset->id) {
2249
                    mon_fdset = NULL;
2250
                }
2251
                break;
2252
            }
2253
        }
2254
    }
2255

  
2256
    if (mon_fdset == NULL) {
2257
        int64_t fdset_id_prev = -1;
2258
        MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2259

  
2260
        if (has_fdset_id) {
2261
            if (fdset_id < 0) {
2262
                error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2263
                          "a non-negative value");
2264
                return NULL;
2265
            }
2266
            /* Use specified fdset ID */
2267
            QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2268
                mon_fdset_cur = mon_fdset;
2269
                if (fdset_id < mon_fdset_cur->id) {
2270
                    break;
2271
                }
2272
            }
2273
        } else {
2274
            /* Use first available fdset ID */
2275
            QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2276
                mon_fdset_cur = mon_fdset;
2277
                if (fdset_id_prev == mon_fdset_cur->id - 1) {
2278
                    fdset_id_prev = mon_fdset_cur->id;
2279
                    continue;
2280
                }
2281
                break;
2282
            }
2283
        }
2284

  
2285
        mon_fdset = g_malloc0(sizeof(*mon_fdset));
2286
        if (has_fdset_id) {
2287
            mon_fdset->id = fdset_id;
2288
        } else {
2289
            mon_fdset->id = fdset_id_prev + 1;
2290
        }
2291

  
2292
        /* The fdset list is ordered by fdset ID */
2293
        if (!mon_fdset_cur) {
2294
            QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2295
        } else if (mon_fdset->id < mon_fdset_cur->id) {
2296
            QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2297
        } else {
2298
            QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2299
        }
2300
    }
2301

  
2302
    mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2303
    mon_fdset_fd->fd = fd;
2304
    mon_fdset_fd->removed = false;
2305
    if (has_opaque) {
2306
        mon_fdset_fd->opaque = g_strdup(opaque);
2307
    }
2308
    QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2309

  
2310
    fdinfo = g_malloc0(sizeof(*fdinfo));
2311
    fdinfo->fdset_id = mon_fdset->id;
2312
    fdinfo->fd = mon_fdset_fd->fd;
2313

  
2314
    return fdinfo;
2315
}
2316

  
2304 2317
int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2305 2318
{
2306 2319
#ifndef _WIN32

Also available in: Unified diff