~ubuntu-branches/ubuntu/lucid/vzctl/lucid

« back to all changes in this revision

Viewing changes to src/vzlist.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2008-01-27 10:24:49 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080127102449-ivjpirh4dyshc0cl
Tags: 3.0.22-3
Correction for #459717. Thanks to eyck <eyck.debian@kuszelas.eu> for the
report.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
#include "util.h"
44
44
#include "types.h"
45
45
 
46
 
LOG_DATA
47
 
static struct Cveinfo *veinfo = NULL; 
 
46
static struct Cveinfo *veinfo = NULL;
48
47
static int n_veinfo = 0;
49
48
 
50
49
static char g_outbuffer[4096] = "";
76
75
{
77
76
        int ret;
78
77
        ret = get_run_ve_ioctl(update);
79
 
        if (ret) 
 
78
        if (ret)
80
79
                ret = get_run_ve_proc(update);
81
80
        return ret;
82
81
}
185
184
 
186
185
int laverage_sort_fn(const void *val1, const void *val2)
187
186
{
188
 
        struct Cla *la1 = ((const struct Cveinfo *)val1)->la;
189
 
        struct Cla *la2 = ((const struct Cveinfo *)val2)->la;
 
187
        const struct Cla *la1 = ((const struct Cveinfo *)val1)->la;
 
188
        const struct Cla *la2 = ((const struct Cveinfo *)val2)->la;
190
189
        int res;
191
190
 
192
191
        if ((res = check_empty_param(la1, la2)) == 2)
203
202
int id_sort_fn(const void *val1, const void *val2)
204
203
{
205
204
        int ret;
206
 
        ret = (((const struct Cveinfo*)val1)->veid > 
 
205
        ret = (((const struct Cveinfo*)val1)->veid >
207
206
                ((const struct Cveinfo*)val2)->veid);
208
207
        return ret;
209
208
}
211
210
int status_sort_fn(const void *val1, const void *val2)
212
211
{
213
212
        int res;
214
 
        res = ((const struct Cveinfo*)val1)->status - 
 
213
        res = ((const struct Cveinfo*)val1)->status -
215
214
                ((const struct Cveinfo*)val2)->status;
216
215
        if (!res)
217
216
                res = id_sort_fn(val1, val2);
227
226
        if ((ret = check_empty_param(h1, h2)) == 2)                     \
228
227
                ret = strcmp(h1, h2);                                   \
229
228
        return ret;                                     \
230
 
}                                                                       
 
229
}
231
230
 
232
231
SORT_STR_FN(hostnm_sort_fn, hostname)
233
232
SORT_STR_FN(name_sort_fn, name)
236
235
#define SORT_UL_RES(fn, type, res, name, index)                         \
237
236
int fn(const void *val1, const void *val2)                              \
238
237
{                                                                       \
239
 
        struct type *r1 = ((const struct Cveinfo *)val1)->res;          \
240
 
        struct type *r2 = ((const struct Cveinfo *)val2)->res;          \
 
238
        const struct type *r1 = ((const struct Cveinfo *)val1)->res;    \
 
239
        const struct type *r2 = ((const struct Cveinfo *)val2)->res;    \
241
240
        int ret;                                                        \
242
241
        if ((ret = check_empty_param(r1, r2)) == 2)                     \
243
242
                ret = r1->name[index] > r2->name[index];                \
244
243
        return ret;                                     \
245
 
}                                                                       
 
244
}
246
245
 
247
246
SORT_UL_RES(kmemsize_h_sort_fn, Cubc, ubc, kmemsize, 0)
248
247
SORT_UL_RES(kmemsize_m_sort_fn, Cubc, ubc, kmemsize, 1)
375
374
SORT_UL_RES(cpulimit_sort_fn, Ccpu, cpu, limit, 0)
376
375
SORT_UL_RES(cpuunits_sort_fn, Ccpu, cpu, limit, 1)
377
376
 
378
 
struct Cfield field_names[] = 
 
377
struct Cfield field_names[] =
379
378
{
380
379
/* veid should have index 0 */
381
380
{"veid", "VEID", "%10s", 0,  RES_NONE, print_veid, id_sort_fn},
527
526
        if (p->hostname != NULL)
528
527
                str = p->hostname;
529
528
        r = snprintf(p_outbuffer, e_buf - p_outbuffer, "%-32s", str);
530
 
        if (last_field != NULL && 
 
529
        if (last_field != NULL &&
531
530
                field_names[last_field->order].res_type != RES_HOSTNAME)
532
531
        {
533
532
                r = 32;
559
558
 
560
559
        if (p->ip != NULL)
561
560
                str = p->ip;
562
 
        if (last_field != NULL && 
 
561
        if (last_field != NULL &&
563
562
                field_names[last_field->order].res_type != RES_IP)
564
563
        {
565
564
                /* Fixme: dont destroy original string */
567
566
                        *ch = 0;
568
567
        }
569
568
        r = snprintf(p_outbuffer, e_buf - p_outbuffer, "%-15s", str);
570
 
        if (last_field != NULL && 
 
569
        if (last_field != NULL &&
571
570
                field_names[last_field->order].res_type != RES_IP)
572
571
        {
573
572
                r = 15;
763
762
                return;
764
763
        tmp->quota = x_malloc(sizeof(*quota));
765
764
        memcpy(tmp->quota, quota, sizeof(*quota));
766
 
        return; 
 
765
        return;
767
766
}
768
767
 
769
768
void update_cpu(int veid, unsigned long limit, unsigned long units)
777
776
        cpu->limit[0] = limit;
778
777
        cpu->limit[1] = units;
779
778
        tmp->cpu = cpu;
780
 
        return; 
 
779
        return;
781
780
}
782
781
 
783
782
void update_la(int veid, struct Cla *la)
788
787
                return;
789
788
        tmp->la = x_malloc(sizeof(*la));
790
789
        memcpy(tmp->la, la, sizeof(*la));
791
 
        return; 
 
790
        return;
792
791
}
793
792
 
794
793
char *parse_var(char *var)
811
810
                quota->name[1] = dq.name[0];                    \
812
811
                quota->name[2] = dq.name[1];                    \
813
812
        }                                                       \
814
 
} while(0);                                                     
 
813
} while(0);
815
814
 
816
815
void merge_conf(struct Cveinfo *ve, vps_res *res)
817
816
{
824
823
                break;                                          \
825
824
        ubc->name[2] = res->ub.name[0];                         \
826
825
        ubc->name[3] = res->ub.name[1];                         \
827
 
} while(0);                                                     
 
826
} while(0);
828
827
 
829
828
                MERGE_UBC(kmemsize, ve->ubc, res);
830
829
                MERGE_UBC(lockedpages, ve->ubc, res);
902
901
        if (param->res.fs.private != NULL)
903
902
                ve_private = strdup(param->res.fs.private_orig);
904
903
        free_vps_param(param);
905
 
        for (i = 0; i < n_veinfo; i++) { 
 
904
        for (i = 0; i < n_veinfo; i++) {
906
905
                veid = veinfo[i].veid;
907
906
                param = init_vps_param();
908
907
                snprintf(buf, sizeof(buf), VPS_CONF_DIR "%d.conf", veid);
1092
1091
        return tmp;
1093
1092
}
1094
1093
 
1095
 
static int get_run_ve_proc2(int update)
1096
 
{
1097
 
        FILE *fp;
1098
 
        struct Cveinfo ve;
1099
 
        char buf[16384];
1100
 
        int res, veid;
1101
 
 
1102
 
        if ((fp = fopen(PROCVEINFO, "r")) == NULL) {
1103
 
                fprintf(stderr, "Unable to open %s\n", PROCVEINFO);
1104
 
                return 1;
1105
 
        }
1106
 
        while (!feof(fp)) {
1107
 
                if (fgets(buf, sizeof(buf), fp) == NULL)
1108
 
                        break;
1109
 
                res = sscanf(buf, "%d", &veid);
1110
 
                if (res != 1 || veid == 0)
1111
 
                        continue;
1112
 
                if (!check_veid_restr(veid))
1113
 
                        continue;
1114
 
                memset(&ve, 0, sizeof(struct Cveinfo));
1115
 
                ve.veid = veid;
1116
 
                ve.status = VE_RUNNING;
1117
 
                if (update)
1118
 
                        update_ve(veid, ve.ip, ve.status);
1119
 
                else
1120
 
                        add_elem(&ve);
1121
 
        }
1122
 
        if (!update)
1123
 
                qsort(veinfo, n_veinfo, sizeof(struct Cveinfo), id_sort_fn);
1124
 
        fclose(fp);
1125
 
        return 0;
1126
 
}
1127
 
 
1128
1094
static int get_run_ve_proc(int update)
1129
1095
{
1130
1096
        FILE *fp;
1134
1100
        int res, veid, classid, nproc;
1135
1101
 
1136
1102
        if ((fp = fopen(PROCVEINFO, "r")) == NULL) {
1137
 
                return get_run_ve_proc2(update);
 
1103
                fprintf(stderr, "Unable to open %s\n", PROCVEINFO);
 
1104
                return 1;
1138
1105
        }
1139
1106
        memset(&ve, 0, sizeof(struct Cveinfo));
1140
1107
        while (!feof(fp)) {
1149
1116
                memset(&ve, 0, sizeof(struct Cveinfo));
1150
1117
                if (res == 4) {
1151
1118
                        ve.ip = invert_ip(ips);
1152
 
                        
1153
 
                } else if (res == 3)
1154
 
                        ve.ip = strdup("");
 
1119
 
 
1120
                }
1155
1121
                ve.veid = veid;
1156
1122
                ve.status = VE_RUNNING;
1157
1123
                if (update)
1178
1144
        for (;;) {
1179
1145
                veip.ip = addr;
1180
1146
                ret = ioctl(vzctlfd, VZCTL_GET_VEIPS, &veip);
1181
 
                if (ret < 0) 
 
1147
                if (ret < 0)
1182
1148
                        goto out;
1183
1149
                else if (ret <= veip.num)
1184
1150
                        break;
1191
1157
 
1192
1158
                buf = x_malloc(ret * (16 * sizeof(char)) + 1);
1193
1159
                cp = buf;
1194
 
                for (i = ret - 1; i >= 0; i--) 
 
1160
                for (i = ret - 1; i >= 0; i--)
1195
1161
                        cp += sprintf(cp, "%d.%d.%d.%d ", NIPQUAD(addr[i]));
1196
1162
                *cp = '\0';
1197
1163
                *str = buf;
1208
1174
        struct vzlist_veidctl veid;
1209
1175
        int nves;
1210
1176
        void *buf = NULL;
1211
 
        int i; 
 
1177
        int i;
1212
1178
 
1213
1179
        vzctlfd = open(VZCTLDEV, O_RDWR);
1214
 
        if (vzctlfd < 0) 
 
1180
        if (vzctlfd < 0)
1215
1181
                goto error;
1216
1182
        veid.num = 256;
1217
1183
        buf = x_malloc(veid.num * sizeof(envid_t));
1218
1184
        while (1) {
1219
1185
                veid.id = buf;
1220
1186
                ret = ioctl(vzctlfd, VZCTL_GET_VEIDS, &veid);
1221
 
                if (ret < 0) 
 
1187
                if (ret < 0)
1222
1188
                        goto out;
1223
1189
                if (ret <= veid.num)
1224
1190
                        break;
1225
1191
                veid.num = ret + 20;
1226
1192
                buf = x_realloc(buf, veid.num * sizeof(envid_t));
1227
 
        } 
 
1193
        }
1228
1194
        nves = ret;
1229
1195
        for (i = 0; i < nves; i++) {
1230
1196
                struct Cveinfo ve;
1241
1207
                        if (errno != ESRCH)
1242
1208
                                goto out;
1243
1209
                        continue;
1244
 
                } 
 
1210
                }
1245
1211
                if (update)
1246
1212
                        update_ve(id, ve.ip, ve.status);
1247
1213
                else
1282
1248
                                update_quota(prev_veid, &quota);
1283
1249
                        memset(&quota, 0, sizeof(quota));
1284
1250
                        continue;
1285
 
                        
 
1251
 
1286
1252
                }
1287
1253
                if (sscanf(buf, "%10s %lu %lu %lu %lu %lu", str, &usage,
1288
1254
                        &softlimit, &hardlimit, &time, &exp) == 6)
1373
1339
int get_ves_la()
1374
1340
{
1375
1341
        int i;
1376
 
        
 
1342
 
1377
1343
        if ((vzctlfd = open(VZCTLDEV, O_RDWR)) < 0)
1378
1344
                return 1;
1379
1345
        for (i = 0; i < n_veinfo; i++) {
1408
1374
int get_ves_cpu()
1409
1375
{
1410
1376
        unsigned long tmp;
1411
 
        int veid, id, weight, rate; 
 
1377
        int veid, id, weight, rate;
1412
1378
        FILE *fp;
1413
1379
        char buf[128];
1414
1380
 
1489
1455
                sp = default_field_order;
1490
1456
        ep = sp + strlen(sp);
1491
1457
        do {
1492
 
                if ((p = strchr(sp, ',')) == NULL) 
 
1458
                if ((p = strchr(sp, ',')) == NULL)
1493
1459
                        p = ep;
1494
1460
                nm_len = p - sp + 1;
1495
1461
                if (nm_len > sizeof(name) - 1) {
1504
1470
                }
1505
1471
                tmp = x_malloc(sizeof(struct Cfield_order));
1506
1472
                tmp->order = order;
1507
 
                tmp->next = NULL;       
 
1473
                tmp->next = NULL;
1508
1474
                if (prev == NULL)
1509
1475
                        g_field_order = tmp;
1510
1476
                else
1543
1509
                fprintf(stderr, "VE not found\n");
1544
1510
                return 1;
1545
1511
        }
1546
 
        if (check_param(RES_QUOTA)) 
 
1512
        if (check_param(RES_QUOTA))
1547
1513
                get_run_quota_stat();
1548
1514
        if (check_param(RES_LA))
1549
1515
                get_ves_la();
1557
1523
        if (name_pattern != NULL)
1558
1524
                filter_by_name();
1559
1525
        return 0;
1560
 
}       
 
1526
}
1561
1527
 
1562
1528
void print_names()
1563
1529
{
1564
1530
        int i;
1565
1531
 
1566
 
        for (i = 0; i < sizeof(field_names) / sizeof(*field_names); i++) 
 
1532
        for (i = 0; i < sizeof(field_names) / sizeof(*field_names); i++)
1567
1533
                printf("%-15s %-15s\n", field_names[i].name,
1568
1534
                                        field_names[i].hdr);
1569
1535
        return;
1623
1589
                                &option_index);
1624
1590
                if (c == -1)
1625
1591
                        break;
1626
 
                
 
1592
 
1627
1593
                switch(c) {
1628
1594
                case 'S'        :
1629
1595
                        only_stopped_ve = 1;
1659
1625
                        veid_only = 1;
1660
1626
                        break;
1661
1627
                case 'n'        :
1662
 
                        f_order = strdup(default_nm_field_order);       
 
1628
                        f_order = strdup(default_nm_field_order);
1663
1629
                        with_names = 1;
1664
1630
                        break;
1665
1631
                case 'N'        :
1672
1638
        }
1673
1639
        if (optind < argc) {
1674
1640
                while (optind < argc) {
1675
 
                        veid =  strtol(argv[optind++], &ep, 10);
 
1641
                        veid =  strtol(argv[optind], &ep, 10);
1676
1642
                        if (*ep != 0 || !veid) {
1677
 
                                fprintf(stderr, "Invalid veid: %s\n",
1678
 
                                argv[optind - 1]);
1679
 
                                return 1;
 
1643
                                char name[STR_SIZE];
 
1644
 
 
1645
                                veid = get_veid_by_name(argv[optind]);
 
1646
                                if (veid < 0) {
 
1647
                                        fprintf(stderr,
 
1648
                                                "VE ID %s is invalid.\n",
 
1649
                                                argv[optind]);
 
1650
                                        return 1;
 
1651
                                }
1680
1652
                        }
 
1653
                        optind++;
1681
1654
                        g_ve_list = x_realloc(g_ve_list,
1682
1655
                                sizeof(*g_ve_list) * ++n_ve_list);
1683
1656
                        g_ve_list[n_ve_list - 1] = veid;
1685
1658
                qsort(g_ve_list, n_ve_list, sizeof(*g_ve_list), id_sort_fn);
1686
1659
        }
1687
1660
        init_log(NULL, 0, 0, 0, 0, NULL);
1688
 
        if (build_field_order(f_order)) 
 
1661
        if (build_field_order(f_order))
1689
1662
                return 1;
1690
1663
        if (getuid()) {
1691
1664
                fprintf(stderr, "This program can only be run under root.\n");