~serge-hallyn/ubuntu/oneiric/lxc/fix-shutdown

« back to all changes in this revision

Viewing changes to .pc/diff-to-bcbd102cb/src/lxc/arguments.c

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-07-07 13:53:52 UTC
  • mfrom: (1.1.9 upstream) (3.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20110707135352-phbbo2w1jb41r8fh
Tags: 0.7.4.2-0.3ubuntu1
* Sync upstream 0.7.4.2
* Add diff up to git head.
  - Fix interaction with cgroups-bin (LP: #784093)
  - Fix arch support to create i386 containers on amd64 (LP: #798476)
  - Support a bind-mounted $HOME with template (LP: #800482)
* add debootstrap to Recommends (LP: #803745)
* debian/patchs updates:
  - refresh 0002-disable-debian-checkroot-script.patch
  - drop:
    * 0004-add-ubuntu-mirrors.patch
    * 0005-add-netbase-to-templates.patch
    * 0006-fix-template-syntax-error.patch
    * 0007-natty-template-install-lxcguest.patch
    * 0010-templates-use-dpkg.patch
  - renamed and updated:
    * 0008-add-arm-to-supported-archs.patch to
      0004-add-arm-to-supported-archs.patch
    * 0009-templates-dont-use-devpts-in-fstab to
      0005-dont-use-devpts-in-fstab
    * 0011-templates-allow-fuse.patch to
      0006-templates-allow-fuse.patch
* remove unused debian/lxc-start.sh
* include autoreconf.mk to force Makefile.in to be rebuilt
* Remaining changes over debian:
  - add lxcguest package
  - debian/control
    * keep docbook-utils in Build-Depends
  - lxc.default: add commented example MIRROR

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * lxc: linux Container library
 
3
 *
 
4
 * (C) Copyright IBM Corp. 2007, 2008
 
5
 *
 
6
 * Authors:
 
7
 * Daniel Lezcano <dlezcano at fr.ibm.com>
 
8
 * Michel Normand <normand at fr.ibm.com>
 
9
 *
 
10
 * This library is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Lesser General Public
 
12
 * License as published by the Free Software Foundation; either
 
13
 * version 2.1 of the License, or (at your option) any later version.
 
14
 *
 
15
 * This library is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 * Lesser General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU Lesser General Public
 
21
 * License along with this library; if not, write to the Free Software
 
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
23
 */
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <limits.h>
 
27
#include <string.h>
 
28
#include <ctype.h>              /* for isprint() */
 
29
#include <errno.h>
 
30
#include <sys/stat.h>
 
31
#include <sys/types.h>
 
32
#include <unistd.h>
 
33
 
 
34
#include "arguments.h"
 
35
 
 
36
/*---------------------------------------------------------------------------*/
 
37
static int build_shortopts(const struct option *a_options,
 
38
                           char *a_shortopts, size_t a_size)
 
39
{
 
40
        const struct option *opt;
 
41
        int i = 0;
 
42
 
 
43
        if (!a_options || !a_shortopts || !a_size)
 
44
                return -1;
 
45
 
 
46
        for (opt = a_options; opt->name; opt++) {
 
47
 
 
48
                if (!isascii(opt->val))
 
49
                        continue;
 
50
 
 
51
                if (i < a_size)
 
52
                        a_shortopts[i++] = opt->val;
 
53
                else
 
54
                        goto is2big;
 
55
 
 
56
                if (opt->has_arg == no_argument)
 
57
                        continue;
 
58
 
 
59
                if (i < a_size)
 
60
                        a_shortopts[i++] = ':';
 
61
                else
 
62
                        goto is2big;
 
63
 
 
64
                if (opt->has_arg == required_argument)
 
65
                        continue;
 
66
 
 
67
                if (i < a_size)
 
68
                        a_shortopts[i++] = ':';
 
69
                else
 
70
                        goto is2big;
 
71
        }
 
72
 
 
73
        if (i < a_size)
 
74
                a_shortopts[i] = '\0';
 
75
        else
 
76
                goto is2big;
 
77
 
 
78
        return 0;
 
79
 
 
80
      is2big:
 
81
        errno = E2BIG;
 
82
        return -1;
 
83
}
 
84
 
 
85
/*---------------------------------------------------------------------------*/
 
86
static void print_usage(const struct option longopts[],
 
87
                        const struct lxc_arguments *a_args)
 
88
 
 
89
{
 
90
        int i;
 
91
        const struct option *opt;
 
92
 
 
93
        fprintf(stderr, "Usage: %s ", a_args->progname);
 
94
 
 
95
        for (opt = longopts, i = 1; opt->name; opt++, i++) {
 
96
                int j;
 
97
                char *uppername = strdup(opt->name);
 
98
 
 
99
                for (j = 0; uppername[j]; j++)
 
100
                        uppername[j] = toupper(uppername[j]);
 
101
 
 
102
                fprintf(stderr, "[");
 
103
 
 
104
                if (isprint(opt->val))
 
105
                        fprintf(stderr, "-%c|", opt->val);
 
106
 
 
107
                fprintf(stderr, "--%s", opt->name);
 
108
 
 
109
                if (opt->has_arg == required_argument)
 
110
                        fprintf(stderr, "=%s", uppername);
 
111
 
 
112
                if (opt->has_arg == optional_argument)
 
113
                        fprintf(stderr, "[=%s]", uppername);
 
114
 
 
115
                fprintf(stderr, "] ");
 
116
 
 
117
                if (!(i % 4))
 
118
                        fprintf(stderr, "\n\t");
 
119
 
 
120
                free(uppername);
 
121
        }
 
122
 
 
123
        fprintf(stderr, "\n");
 
124
        exit(0);
 
125
}
 
126
 
 
127
static void print_help(const struct lxc_arguments *args, int code)
 
128
{
 
129
        fprintf(stderr, "\
 
130
Usage: %s %s\
 
131
\n\
 
132
Common options :\n\
 
133
  -o, --logfile=FILE               Output log to FILE instead of stderr\n\
 
134
  -l, --logpriority=LEVEL          Set log priority to LEVEL\n\
 
135
  -q, --quiet                      Don't produce any output\n\
 
136
  -?, --help                       Give this help list\n\
 
137
      --usage                      Give a short usage message\n\
 
138
\n\
 
139
Mandatory or optional arguments to long options are also mandatory or optional\n\
 
140
for any corresponding short options.\n\
 
141
\n\
 
142
See the %s man page for further information.\n\n",
 
143
        args->progname, args->help, args->progname);
 
144
 
 
145
        exit(code);
 
146
}
 
147
 
 
148
extern int lxc_arguments_parse(struct lxc_arguments *args,
 
149
                               int argc, char * const argv[])
 
150
{
 
151
        char shortopts[256];
 
152
        int  ret = 0;
 
153
 
 
154
        ret = build_shortopts(args->options, shortopts, sizeof(shortopts));
 
155
        if (ret < 0) {
 
156
                lxc_error(args, "build_shortopts() failed : %s",
 
157
                          strerror(errno));
 
158
                return ret;
 
159
        }
 
160
 
 
161
        while (1)  {
 
162
                int c, index = 0;
 
163
 
 
164
                c = getopt_long(argc, argv, shortopts, args->options, &index);
 
165
                if (c == -1)
 
166
                        break;
 
167
                switch (c) {
 
168
                case 'n':       args->name = optarg; break;
 
169
                case 'o':       args->log_file = optarg; break;
 
170
                case 'l':       args->log_priority = optarg; break;
 
171
                case 'c':       args->console = optarg; break;
 
172
                case 'q':       args->quiet = 1; break;
 
173
                case OPT_USAGE: print_usage(args->options, args);
 
174
                case '?':       print_help(args, 1);
 
175
                case 'h':       print_help(args, 0);
 
176
                default:
 
177
                        if (args->parser) {
 
178
                                ret = args->parser(args, c, optarg);
 
179
                                if (ret)
 
180
                                        goto error;
 
181
                        }
 
182
                }
 
183
        }
 
184
 
 
185
        /*
 
186
         * Reclaim the remaining command arguments
 
187
         */
 
188
        args->argv = &argv[optind];
 
189
        args->argc = argc - optind;
 
190
 
 
191
        /* Check the command options */
 
192
 
 
193
        if (!args->name) {
 
194
                lxc_error(args, "missing container name, use --name option");
 
195
                return -1;
 
196
        }
 
197
 
 
198
        if (args->checker)
 
199
                ret = args->checker(args);
 
200
error:
 
201
        if (ret)
 
202
                lxc_error(args, "could not parse command line");
 
203
        return ret;
 
204
}
 
205
 
 
206
extern char **lxc_arguments_dup(const char *file, struct lxc_arguments *args)
 
207
{
 
208
        char **argv;
 
209
        int opt, nbargs = args->argc + 2;
 
210
 
 
211
        if (args->quiet)
 
212
                nbargs += 1;
 
213
 
 
214
        argv = malloc((nbargs + 1) * sizeof(*argv));
 
215
        if (!argv)
 
216
                return NULL;
 
217
 
 
218
        nbargs = 0;
 
219
 
 
220
        argv[nbargs++] = strdup(file);
 
221
 
 
222
        if (args->quiet)
 
223
                argv[nbargs++] = "--quiet";
 
224
 
 
225
        argv[nbargs++] = "--";
 
226
 
 
227
        for (opt = 0; opt < args->argc; opt++)
 
228
                argv[nbargs++] = strdup(args->argv[opt]);
 
229
 
 
230
        argv[nbargs] = NULL;
 
231
 
 
232
        return argv;
 
233
}
 
234
 
 
235
int lxc_arguments_str_to_int(struct lxc_arguments *args, const char *str)
 
236
{
 
237
        long val;
 
238
        char *endptr;
 
239
 
 
240
        errno = 0;
 
241
        val = strtol(str, &endptr, 10);
 
242
        if (errno) {
 
243
                lxc_error(args, "invalid statefd '%s' : %m", str);
 
244
                return -1;
 
245
        }
 
246
 
 
247
        if (*endptr) {
 
248
                lxc_error(args, "invalid digit for statefd '%s'", str);
 
249
                return -1;
 
250
        }
 
251
 
 
252
        return (int)val;
 
253
}