~ubuntu-branches/ubuntu/karmic/vzctl/karmic

« back to all changes in this revision

Viewing changes to src/lib/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2007-04-10 18:08:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070410180816-0uuzj9fnna7gmzxv
Tags: 3.0.16-4
Etch has been released which means that this version can be uploaded
to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2000-2006 SWsoft. All rights reserved.
 
2
 *  Copyright (C) 2000-2007 SWsoft. All rights reserved.
3
3
 *
4
4
 *  This program is free software; you can redistribute it and/or modify
5
5
 *  it under the terms of the GNU General Public License as published by
29
29
#include <arpa/inet.h>
30
30
#include <stdarg.h>
31
31
#include <linux/limits.h>
 
32
#include <dirent.h>
32
33
 
33
34
#include "util.h"
34
35
#include "logger.h"
54
55
                        p2++;
55
56
                } else {
56
57
                        *p1 = *p2;
57
 
                        p1++; p2++;
 
58
                        p1++; p2++;
58
59
                        fl = 0;
59
60
                }
60
61
        }
123
124
                ps = p + 1;
124
125
                if (!stat_file(buf)) {
125
126
                        if (mkdir(buf, 0755)) {
126
 
                                logger(0, errno, "Can't create directory %s",
 
127
                                logger(-1, errno, "Can't create directory %s",
127
128
                                        buf);
128
129
                                return 1;
129
130
                        }
133
134
                return 0;
134
135
        if (!stat_file(path)) {
135
136
                if (mkdir(path, 0755)) {
136
 
                        logger(0, errno, "Can't create directory %s", path);
 
137
                        logger(-1, errno, "Can't create directory %s", path);
137
138
                        return 1;
138
139
                }
139
140
        }
164
165
 
165
166
void str_tolower(const char *from, char *to)
166
167
{
167
 
        if (from == NULL || to == NULL)
168
 
                return;
169
 
        while ((*to++ = tolower(*from++)));
 
168
        if (from == NULL || to == NULL)
 
169
                return;
 
170
        while ((*to++ = tolower(*from++)));
170
171
}
171
172
 
172
173
void str_toupper(const char *from, char *to)
173
174
{
174
 
        if (from == NULL || to == NULL)
175
 
                return;
176
 
        while ((*to++ = toupper(*from++)));
 
175
        if (from == NULL || to == NULL)
 
176
                return;
 
177
        while ((*to++ = toupper(*from++)));
177
178
}
178
179
 
179
180
int check_var(const void *val, const char *message)
180
181
{
181
 
        if (val != NULL)
182
 
                return 0;
183
 
        logger(0, 0, "%s", message);
 
182
        if (val != NULL)
 
183
                return 0;
 
184
        logger(-1, 0, "%s", message);
184
185
 
185
 
        return 1;
 
186
        return 1;
186
187
}
187
188
 
188
189
int cp_file(char *dst, char *src)
192
193
        char buf[4096];
193
194
 
194
195
        if (stat(src, &st) < 0) {
195
 
                logger(0, errno, "Unable to stat %s", src);
 
196
                logger(-1, errno, "Unable to stat %s", src);
196
197
                return -1;
197
198
        }
198
199
        len = st.st_size;
199
200
        if ((fd_src = open(src, O_RDONLY)) < 0) {
200
 
                logger(0, errno, "Unable to open %s", src);
 
201
                logger(-1, errno, "Unable to open %s", src);
201
202
                return -1;
202
203
        }
203
204
        if ((fd_dst = open(dst, O_CREAT|O_RDWR, st.st_mode)) < 0) {
204
 
                logger(0, errno, "Unable to open %s", dst);
 
205
                logger(-1, errno, "Unable to open %s", dst);
205
206
                close(fd_src);
206
207
                return -1;
207
208
        }
210
211
                if (!ret)
211
212
                        break;
212
213
                else if (ret < 0) {
213
 
                        logger(0, errno, "Unable to read from %s", src);
 
214
                        logger(-1, errno, "Unable to read from %s", src);
214
215
                        ret = -1;
215
216
                        break;
216
217
                }
217
218
                if (write(fd_dst, buf, len) < 0) {
218
 
                        logger(0, errno, "Unable write to %s", dst);
 
219
                        logger(-1, errno, "Unable write to %s", dst);
219
220
                        ret = -1;
220
221
                        break;
221
222
                }
228
229
 
229
230
void get_vps_conf_path(envid_t veid, char *buf, int len)
230
231
{
231
 
        snprintf(buf, len, VPS_CONF_DIR "/%d.conf", veid);
 
232
        snprintf(buf, len, VPS_CONF_DIR "%d.conf", veid);
232
233
}
233
234
 
234
235
char *arg2str(char **arg)
235
236
{
236
 
        char **p;
237
 
        char *str, *sp;
238
 
        int len = 0;
 
237
        char **p;
 
238
        char *str, *sp;
 
239
        int len = 0;
239
240
 
240
241
        if (arg == NULL)
241
242
                return NULL;
242
 
        p = arg;
243
 
        while (*p)
244
 
                len += strlen(*p++) + 1;
245
 
        if ((str = (char *)malloc(len + 1)) == NULL)
246
 
                return NULL;
247
 
        p = arg;
248
 
        sp = str;
249
 
        while (*p)
250
 
                sp += sprintf(sp, "%s ", *p++);
 
243
        p = arg;
 
244
        while (*p)
 
245
                len += strlen(*p++) + 1;
 
246
        if ((str = (char *)malloc(len + 1)) == NULL)
 
247
                return NULL;
 
248
        p = arg;
 
249
        sp = str;
 
250
        while (*p)
 
251
                sp += sprintf(sp, "%s ", *p++);
251
252
 
252
 
        return str;
 
253
        return str;
253
254
}
254
255
 
255
256
void free_arg(char **arg)
281
282
        return -1;
282
283
}
283
284
 
284
 
int get_ipaddr(const char *ip_str, unsigned int *ip)
 
285
int get_netaddr(const char *ip_str, unsigned int *ip)
285
286
{
286
 
        struct in_addr addr;
287
 
 
288
 
        if (!inet_aton(ip_str, &addr))
289
 
                return -1;
290
 
        *ip = addr.s_addr;
291
 
 
292
 
        return 0;
 
287
        if (strchr(ip_str, ':')) {
 
288
                if (inet_pton(AF_INET6, ip_str, ip) <= 0)
 
289
                        return -1;
 
290
                return AF_INET6;
 
291
        }
 
292
        if (inet_pton(AF_INET, ip_str, ip) <= 0)
 
293
                return -1;
 
294
        return AF_INET;
293
295
}
294
296
 
295
 
char *get_ipname(unsigned int ip)
 
297
const char *get_netname(unsigned int *ip, int family)
296
298
{
297
 
        struct in_addr addr;
 
299
        static char buf[64];
298
300
 
299
 
        addr.s_addr = ip;
300
 
        return inet_ntoa(addr);
 
301
        return inet_ntop(family, ip, buf, sizeof(buf) - 1);
301
302
}
302
303
 
303
304
char *subst_VEID(envid_t veid, char *src)
357
358
{
358
359
        long pagesize;
359
360
        if ((*mem = sysconf(_SC_PHYS_PAGES)) == -1) {
360
 
                logger(0, errno, "Unable to get total phys pages");
 
361
                logger(-1, errno, "Unable to get total phys pages");
361
362
                return -1;
362
363
        }
363
364
        if ((pagesize = get_pagesize()) < 0)
374
375
        if (thrmax == NULL)
375
376
                return 1;
376
377
        if ((fd = fopen(PROCTHR, "r")) == NULL) {
377
 
                logger(0, errno, "Unable to open " PROCTHR);
 
378
                logger(-1, errno, "Unable to open " PROCTHR);
378
379
                return 1;
379
380
        }
380
381
        if (fgets(str, sizeof(str), fd) == NULL) {
393
394
        char str[128];
394
395
 
395
396
        if ((fd = fopen(PROCMEM, "r")) == NULL) {
396
 
                logger(0, errno, "Cannot open " PROCMEM);
 
397
                logger(-1, errno, "Cannot open " PROCMEM);
397
398
                return -1;
398
399
        }
399
400
        while (fgets(str, sizeof(str), fd)) {
403
404
                        return 0;
404
405
                }
405
406
        }
406
 
        logger(0, errno, "Swap: is not found in " PROCMEM );
 
407
        logger(-1, errno, "Swap: is not found in " PROCMEM );
407
408
        fclose(fd);
408
409
 
409
410
        return -1;
416
417
        int ncpu = 0;
417
418
 
418
419
        if ((fd = fopen("/proc/cpuinfo", "r")) == NULL) {
419
 
                logger(0, errno, "Cannot open /proc/cpuinfo");
 
420
                logger(-1, errno, "Cannot open /proc/cpuinfo");
420
421
                return 1;
421
422
        }
422
423
        while (fgets(str, sizeof(str), fd)) {
433
434
        char str[128];
434
435
 
435
436
        if ((fd = fopen(PROCMEM, "r")) == NULL) {
436
 
                logger(0, errno, "Cannot open " PROCMEM);
 
437
                logger(-1, errno, "Cannot open " PROCMEM);
437
438
                return -1;
438
439
        }
439
440
        while (fgets(str, sizeof(str), fd)) {
443
444
                        return 0;
444
445
                }
445
446
        }
446
 
        logger(0, errno, "LowTotal: is not found in" PROCMEM);
 
447
        logger(-1, errno, "LowTotal: is not found in" PROCMEM);
447
448
        fclose(fd);
448
449
        return -1;
449
450
}
575
576
 
576
577
        return 0;
577
578
}
 
579
 
 
580
void remove_names(envid_t veid)
 
581
{
 
582
        char buf[STR_SIZE];
 
583
        char content[STR_SIZE];
 
584
        struct stat st;
 
585
        struct dirent *ep;
 
586
        DIR *dp;
 
587
        char *p;
 
588
        int id;
 
589
 
 
590
        if (!(dp = opendir(VENAME_DIR)))
 
591
                return;
 
592
        while ((ep = readdir(dp))) {
 
593
                snprintf(buf, sizeof(buf), VENAME_DIR "/%s", ep->d_name);
 
594
                if (lstat(buf, &st))
 
595
                        continue;
 
596
                if (!S_ISLNK(st.st_mode))
 
597
                        continue;
 
598
                id = readlink(buf, content, sizeof(content) - 1);
 
599
                if (id < 0)
 
600
                        continue;
 
601
                content[id] = 0;
 
602
                if ((p = strrchr(content, '/')) != NULL)
 
603
                        p++;
 
604
                if (sscanf(p, "%d.conf", &id) == 1 && veid == id)
 
605
                        unlink(buf);
 
606
        }
 
607
        closedir(dp);
 
608
}