File: parameter.c

package info (click to toggle)
cowdancer 0.47
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 696 kB
  • ctags: 179
  • sloc: ansic: 2,634; sh: 407; makefile: 116; cpp: 6; python: 4
file content (471 lines) | stat: -rw-r--r-- 12,848 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*BINFMTC:
 *  parameter handling for cpbuilder.
 *  Copyright (C) 2007-2008 Junichi Uekawa
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "parameter.h"


/* 
   
The pbuilder command-line to pass

0: pbuilder
1: build/create/login etc.
offset: the next command

The last-command will be
PBUILDER_ADD_PARAM(NULL);

 */
char* pbuildercommandline[MAXPBUILDERCOMMANDLINE];
int offset=2;

/**
 * load configuration.
 *
 * Returns bash return codes or -1 on popen error.
 * 
 * Most interesting codes:
 *  -1 = popen failed
 *   0 = ok
 *   1 = file not found
 *   2 = syntax error
 */
int load_config_file(const char* config, pbuilderconfig* pc)
{
  char *s;
  FILE* f;

  char* buf=NULL;
  size_t bufsiz=0;
  char* delim;
  int result;

  asprintf(&s, "env bash -c 'set -e ; . %s; set ' 2>&1", config);
  f=popen(s, "r");
  if( NULL == f )
    return -1;
  while ( (0 == feof(f)) && (getline(&buf,&bufsiz,f)>0) )
    {
      if (strrchr(buf,'\n'))
	{
	  *(strrchr(buf,'\n'))=0;
	}
      
      if ((delim=strchr(buf,'=')))
	{
	  /* assuming config entry */
	  *(delim++)=0;
	  if (!strcmp(buf, "MIRRORSITE"))
	    {
	      pc->mirror=strdup(delim);
	      //printf("DEBUG: %s, %s\n", buf, delim);
	    }
	  else if (!strcmp(buf, "BUILDRESULT"))
	    {
	      pc->buildresult=strdup(delim);
	    }
	  else if (!strcmp(buf, "DISTRIBUTION"))
	    {
	      pc->distribution=strdup(delim);
	      //printf("DEBUG: %s, %s\n", buf, delim);
	    }
	  else if (!strcmp(buf, "KERNEL_IMAGE"))
	    {
	      pc->kernel_image=strdup(delim);
	      //printf("DEBUG: %s, %s\n", buf, delim);
	    }
	  else if (!strcmp(buf, "INITRD"))
	    {
	      pc->initrd=strdup(delim);
	      //printf("DEBUG: %s, %s\n", buf, delim);
	    }
	  else if (!strcmp(buf, "MEMORY_MEGS"))
	    {
	      pc->memory_megs=atoi(delim);
	      //printf("DEBUG: %s, %s\n", buf, delim);
	    }
	  else if (!strcmp(buf, "ARCH"))
	    {
	      pc->arch=strdup(delim);
	      //printf("DEBUG: %s, %s\n", buf, delim);
	    }
	  else if (!strcmp(buf, "BASEPATH"))
	    {
	      pc->basepath=strdup(delim);
	      //printf("DEBUG: %s, %s\n", buf, delim);
	    }
	  else if (!strcmp(buf, "BUILDPLACE"))
	    {
	      pc->buildplace=strdup(delim);
	    }
	  else if (!strcmp(buf, "COMPONENTS"))
	    {
	      pc->components=strdup(delim);
	    }
	  else if (!strcmp(buf, "DEBBUILDOPTS"))
	    {
	      pc->debbuildopts=strdup(delim);
	    }
	  else if (!strcmp(buf, "NO_COWDANCER_UPDATE"))
	    {
	      pc->no_cowdancer_update=1;
	    }
	  else if (!strcmp(buf, "DEBIAN_ETCH_WORKAROUND"))
	    {
	      pc->debian_etch_workaround=1;
	    }
	}
    }

  result = WEXITSTATUS( pclose(f) );
  if(buf) {
    // Don't warn of missing config files
    if( result > 1 )
      printf( "(exit %i) -> %s\n", result, buf );
    free(buf);
  }
  if(s) free(s);
  return result;
}


int cpbuilder_dumpconfig(pbuilderconfig* pc)
{
  /* dump configuration */

  printf("dump config\n");
#define DUMPINT(S) printf("  "#S": %i\n", pc->S);
#define DUMPSTR(S) printf("  "#S": %s\n", pc->S);

  DUMPINT(mountproc);
  DUMPINT(mountdev);
  DUMPINT(mountdevpts);
  DUMPINT(save_after_login);

  DUMPSTR(buildplace);
  DUMPSTR(buildresult);
  DUMPSTR(basepath);
  DUMPSTR(mirror);
  DUMPSTR(distribution);
  DUMPSTR(components);
  DUMPSTR(debbuildopts);

  DUMPINT(no_cowdancer_update);
  
  DUMPSTR(kernel_image);
  DUMPSTR(initrd);
  DUMPINT(memory_megs);
  DUMPSTR(arch);
  return 0;
}

int parse_parameter(int ac, char** av, 
		    const char* keyword)
{
  int c;			/* option */
  int index_point;
  int config_ok = -1, load_ok;
  char * cmdstr=NULL;
  static pbuilderconfig pc;
  
  static struct option long_options[]=
  {
    {"basepath", required_argument, 0, 'b'},
    {"buildplace", required_argument, 0, 'B'},
    {"mountproc", no_argument, &(pc.mountproc), 1},
    {"mountdev", no_argument, &(pc.mountdev), 1},
    {"mountdevpts", no_argument, &(pc.mountdevpts), 1},
    {"nomountproc", no_argument, &(pc.mountproc), 0},
    {"nomountdev", no_argument, &(pc.mountdev), 0},
    {"nomountdevpts", no_argument, &(pc.mountdevpts), 0},
    {"save-after-login", no_argument, &(pc.save_after_login), 1},
    {"save-after-exec", no_argument, &(pc.save_after_login), 1},
    {"build", no_argument, (int*)&(pc.operation), pbuilder_build},
    {"create", no_argument, (int*)&(pc.operation), pbuilder_create},
    {"update", no_argument, (int*)&(pc.operation), pbuilder_update},
    {"login", no_argument, (int*)&(pc.operation), pbuilder_login},
    {"execute", no_argument, (int*)&(pc.operation), pbuilder_execute},
    {"help", no_argument, (int*)&(pc.operation), pbuilder_help},
    {"dumpconfig", no_argument, (int*)&(pc.operation), pbuilder_dumpconfig},
    {"version", no_argument, 0, 'v'},
    {"configfile", required_argument, 0, 'c'},
    {"mirror", required_argument, 0, 0},
    {"buildresult", required_argument, 0, 0},
    {"distribution", required_argument, 0, 0},
    {"components", required_argument, 0, 0},
    {"debbuildopts", required_argument, 0, 0},
    {"no-cowdancer-update", no_argument, 0, 0},
    {"debian-etch-workaround", no_argument, 0, 0},

    /* verbatim options, synced as of pbuilder 0.153 */
    {"othermirror", required_argument, 0, 'M'},
    {"http-proxy", required_argument, 0, 'M'},
    {"aptcache", required_argument, 0, 'M'},
    {"extrapackages", required_argument, 0, 'M'},
    {"hookdir", required_argument, 0, 'M'},
    {"debemail", required_argument, 0, 'M'},
    {"logfile", required_argument, 0, 'M'},
    {"aptconfdir", required_argument, 0, 'M'},
    {"timeout", required_argument, 0, 'M'},
    {"bindmounts", required_argument, 0, 'M'},
    {"debootstrapopts", required_argument, 0, 'M'},
    {"debootstrap", required_argument, 0, 'M'},
    
    {"removepackages", no_argument, 0, 'm'},
    {"override-config", no_argument, 0, 'm'},
    {"pkgname-logfile", no_argument, 0, 'm'},
    {"binary-arch", no_argument, 0, 'm'},
    {"preserve-buildplace", no_argument, 0, 'm'},
    {"debug", no_argument, 0, 'm'},
    {"autocleanaptcache", no_argument, 0, 'm'},

    {0,0,0,0}
  };

  /* define pc to be clear. */
  memset (&pc, 0, sizeof(pbuilderconfig));
  /* default command-line component */
  pbuildercommandline[0]="pbuilder";
  
  /**
   * Try to load all standard config files.
   * Skip non existing, but exit on broken ones.
   * config_ok is 0, if any load was successfull
   **/
  load_ok = load_config_file("/usr/share/pbuilder/pbuilderrc", &pc);
  if( load_ok > 1 ) exit( 2 );
  if( config_ok != 0 ) config_ok = load_ok;

  load_ok = load_config_file("/etc/pbuilderrc", &pc);
  if( load_ok > 1 ) exit( 3 );
  if( config_ok != 0 ) config_ok = load_ok;

  load_ok = load_config_file("~/.pbuilderrc", &pc);
  if( load_ok > 1 ) exit( 4 );
  if( config_ok != 0 ) config_ok = load_ok;

  /* load config files here. */
  while((c = getopt_long (ac, av, "b:d:Mmhv", long_options, &index_point)) != -1)
    {
      switch (c)
	{
	case 'b':		/* basepath */
	  if (pc.operation == pbuilder_create)
	    {
	      if (mkdir(optarg, 0777)<0)
		{
		  perror("mkdir");
		  return 1;
		}
	    }
	  else if (!pc.operation)
	    {
	      fprintf(stderr, "need to specify operation before --basepath option\n");
	      return 1;
	    }
	  if (!(pc.basepath = canonicalize_file_name(optarg)))
	    {
	      fprintf(stderr, "cannot canonicalize filename %s, does not exist\n", optarg);
	      return 1;
	    }
	  break;
	case 'B':		/* buildplace */
	  pc.buildplace = strdup(optarg);
	  break;
	case 'c':		/* --config */
	  load_ok = load_config_file(optarg, &pc);
	  if( load_ok > 1 ) exit( 5 );
	  if( config_ok != 0 ) config_ok = load_ok;

	  if (0>asprintf(&cmdstr, "--%s", long_options[index_point].name))
	    {
	      /* error */
	      fprintf(stderr, "out of memory constructing command-line options\n");
	      exit (1);
	    }
	  PBUILDER_ADD_PARAM(cmdstr);
	  PBUILDER_ADD_PARAM(strdup(optarg));
	  break;
	case 'M':		/* pass through to pbuilder: duplicate with param */
	  if (0>asprintf(&cmdstr, "--%s", long_options[index_point].name))
	    {
	      /* error */
	      fprintf(stderr, "out of memory constructing command-line options\n");
	      exit (1);
	    }
	  PBUILDER_ADD_PARAM(cmdstr);
	  PBUILDER_ADD_PARAM(strdup(optarg));
	  break;
	case 'm':		/* pass through to pbuilder: duplicate without param */
	  if (0>asprintf(&cmdstr, "--%s", long_options[index_point].name))
	    {
	      /* error */
	      fprintf(stderr, "out of memory constructing command-line options\n");
	      exit (1);
	    }
	  PBUILDER_ADD_PARAM(cmdstr);
	  break;
	case 0:
	  /* other cases with long option with flags, this is expected
	     behavior, so ignore it, for most of the time.
	  */
	  
	  /* handle specific options which also give 0. */

	  /* first, generate 'cmdstr' which is useful anyway */
	  if (0>asprintf(&cmdstr, "--%s", long_options[index_point].name))
	    {
	      /* error */
	      fprintf(stderr, "out of memory constructing command-line options\n");
	      exit (1);
	    }

	  if (!strcmp(long_options[index_point].name,"mirror"))
	    {
	      pc.mirror=strdup(optarg);
	    }
	  else if (!strcmp(long_options[index_point].name,"buildresult"))
	    {
	      pc.buildresult=strdup(optarg);
	    }
	  else if (!strcmp(long_options[index_point].name,"distribution"))
	    {
	      pc.distribution=strdup(optarg);
	    }
	  else if (!strcmp(long_options[index_point].name,"no-cowdancer-update"))
	    {
	      pc.no_cowdancer_update=1;
	    }
	  else if (!strcmp(long_options[index_point].name,"debian-etch-workaround")) 
	    {
	      pc.debian_etch_workaround=1;
	    }
	  else if (!strcmp(long_options[index_point].name,"components"))
	    {
	      /* this is for qemubuilder */
	      pc.components=strdup(optarg);
	      
	      /* pass it for cowbuilder */
	      PBUILDER_ADD_PARAM(cmdstr);
	      PBUILDER_ADD_PARAM(strdup(optarg));
	    }
	  else if (!strcmp(long_options[index_point].name,"debbuildopts"))
	    {
	      /* this is for qemubuilder */
	      pc.debbuildopts=strdup(optarg);
	      
	      /* pass it for cowbuilder */
	      PBUILDER_ADD_PARAM(cmdstr);
	      PBUILDER_ADD_PARAM(strdup(optarg));
	    }
	  break;
	case 'h':		/* -h */
	case 'v':		/* -v --version */
	  pc.operation=pbuilder_help;
	  break;
	default:
	  fprintf(stderr, "Unhandled option\n");
	  /* Error case. */
	  return 1;
	}
    }

  if( 0 != config_ok ) {
    printf( "Couldn't load any valid config file.\n" );
    exit( 6 );
  }

  /* set default values */
  if (!pc.basepath) 
    asprintf(&(pc.basepath), "/var/cache/pbuilder/base.%s", keyword);
  if (!pc.buildplace)
    {
      fprintf(stderr, "E: BUILDPLACE is not set\n");
      return 1;
    }
  else
    {
      char* buildplace_ = pc.buildplace;
      mkdir(buildplace_,0777); /* create if it does not exist */
      asprintf(&(pc.buildplace), "%s/%s.%i",
	       buildplace_, keyword, (int)getpid());
      free(buildplace_);
    }

  if (!pc.distribution)
    pc.distribution=strdup("sid");
  
  /* add mandatory command-line options */
  PBUILDER_ADD_PARAM("--distribution");
  PBUILDER_ADD_PARAM(pc.distribution);

  if (!pc.memory_megs)
    pc.memory_megs=128;

  switch(pc.operation)
    {
    case pbuilder_build:
      if (!av[optind])
	{
	  /* parameter missing */
	  fprintf(stderr, "E: parameter missing for build operation\n");
	  return 1;
	}
      
      return cpbuilder_build(&pc, av[optind]);
      
    case pbuilder_create:
      return cpbuilder_create(&pc);
      
    case pbuilder_update:
      return cpbuilder_update(&pc);
      
    case pbuilder_login:
      return cpbuilder_login(&pc);

    case pbuilder_execute:
      if (!av[optind])
	{
	  /* parameter missing */
	  fprintf(stderr, "E: parameter missing for execute operation\n");
	  return 1;
	}
      return cpbuilder_execute(&pc, &av[optind]);

    case pbuilder_help:
      return cpbuilder_help();

    case pbuilder_dumpconfig:
      return cpbuilder_dumpconfig(&pc);

    default:			
      fprintf (stderr, "E: No operation specified\n");
      return 1;
    }
  
  return 0;
}