summaryrefslogtreecommitdiffstats
path: root/contrib/amd/fsinfo/fsi_util.c
blob: a41b3be38613b57cc0421f1d0c13d381f21f1aa8 (plain)
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 * Copyright (c) 1997-2003 Erez Zadok
 * Copyright (c) 1989 Jan-Simon Pendry
 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
 * Copyright (c) 1989 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgment:
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      %W% (Berkeley) %G%
 *
 * $Id: fsi_util.c,v 1.3.2.5 2003/07/18 04:50:19 ezk Exp $
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <am_defs.h>
#include <fsi_data.h>
#include <fsinfo.h>

/* static variables */
static int show_range = 10;
static int col = 0;
static int total_shown = 0;
static int total_mmm = 8;


static int
col_output(int len)
{
  int wrapped = 0;

  col += len;
  if (col > 77) {
    fputc('\n', stdout);
    col = len;
    wrapped = 1;
  }
  return wrapped;
}


static void
show_total(void)
{
  if (total_mmm != -show_range + 1) {
    char n[8];
    int len;

    if (total_mmm < 0)
      fputc('*', stdout);
    sprintf(n, "%d", total_shown);
    len = strlen(n);
    if (col_output(len))
      fputc(' ', stdout);
    fputs(n, stdout);
    fflush(stdout);
    total_mmm = -show_range;
  }
}


void
col_cleanup(int eoj)
{
  if (verbose < 0)
    return;
  if (eoj) {
    show_total();
    fputs(")]", stdout);
  }
  if (col) {
    fputc('\n', stdout);
    col = 0;
  }
}


/*
 * Lots of ways of reporting errors...
 */
void
error(char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  col_cleanup(0);
  fprintf(stderr, "%s: Error, ", progname);
  vfprintf(stderr, fmt, ap);
  fputc('\n', stderr);
  errors++;
  va_end(ap);
}


void
lerror(ioloc *l, char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  col_cleanup(0);
  fprintf(stderr, "%s:%d: ", l->i_file, l->i_line);
  vfprintf(stderr, fmt, ap);
  fputc('\n', stderr);
  errors++;
  va_end(ap);
}


void
lwarning(ioloc *l, char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  col_cleanup(0);
  fprintf(stderr, "%s:%d: ", l->i_file, l->i_line);
  vfprintf(stderr, fmt, ap);
  fputc('\n', stderr);
  va_end(ap);
}


void
fatal(char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  col_cleanup(1);
  fprintf(stderr, "%s: Fatal, ", progname);
  vfprintf(stderr, fmt, ap);
  fputc('\n', stderr);
  va_end(ap);
  exit(1);
}


/*
 * Debug log
 */
void
fsi_log(char *fmt, ...)
{
  va_list ap;

  if (verbose > 0) {
    va_start(ap, fmt);
    fputc('#', stdout);
    fprintf(stdout, "%s: ", progname);
    vfprintf(stdout, fmt, ap);
    putc('\n', stdout);
    va_end(ap);
  }
}


void
info_hdr(FILE *ef, char *info)
{
  fprintf(ef, "# *** NOTE: This file contains %s info\n", info);
}


void
gen_hdr(FILE *ef, char *hn)
{
  fprintf(ef, "# *** NOTE: Only for use on %s\n", hn);
}


static void
make_banner(FILE *fp)
{
  time_t t = time((time_t *) 0);
  char *cp = ctime(&t);

  fprintf(fp,
	  "\
# *** This file was automatically generated -- DO NOT EDIT HERE ***\n\
# \"%s\" run by %s@%s on %s\
#\n\
",
	  progname, username, hostname, cp);
}


void
show_new(char *msg)
{
  if (verbose < 0)
    return;

  total_shown++;
  if (total_mmm > show_range) {
    show_total();
  } else if (total_mmm == 0) {
    fputc('*', stdout);
    fflush(stdout);
    col += 1;
  }
  total_mmm++;
}


void
show_area_being_processed(char *area, int n)
{
  static char *last_area = 0;

  if (verbose < 0)
    return;
  if (last_area) {
    if (total_shown)
      show_total();
    fputs(")", stdout);
    col += 1;
  }

  if (!last_area || !STREQ(area, last_area)) {
    if (last_area) {
      col_cleanup(0);
      total_shown = 0;
      total_mmm = show_range + 1;
    }
    (void) col_output(strlen(area) + 2);
    fprintf(stdout, "[%s", area);
    last_area = area;
  }

  fputs(" (", stdout);
  col += 2;
  show_range = n;
  total_mmm = n + 1;

  fflush(stdout);
}


/*
 * Open a file with the given prefix and name
 */
FILE *
pref_open(char *pref, char *hn, void (*hdr) (FILE *, char *), char *arg)
{
  char p[MAXPATHLEN];
  FILE *ef;

  sprintf(p, "%s%s", pref, hn);
  fsi_log("Writing %s info for %s to %s", pref, hn, p);
  ef = fopen(p, "w");
  if (ef) {
    (*hdr) (ef, arg);
    make_banner(ef);
  } else {
    error("can't open %s for writing", p);
  }

  return ef;
}


int
pref_close(FILE *fp)
{
  return fclose(fp) == 0;
}


/*
 * Determine where Amd would automount the host/volname pair
 */
void
compute_automount_point(char *buf, host *hp, char *vn)
{
  sprintf(buf, "%s/%s%s", autodir, hp->h_lochost, vn);
}


char *
xcalloc(int i, int s)
{
  char *p = (char *) calloc(i, (unsigned) s);

  if (!p)
    fatal("Out of memory");
  return p;
}


/*
 * Data constructors..
 */
automount *
new_automount(char *name)
{
  automount *ap = CALLOC(struct automount);

  ap->a_ioloc = current_location();
  ap->a_name = name;
  ap->a_volname = 0;
  ap->a_mount = 0;
  ap->a_opts = 0;
  show_new("automount");
  return ap;
}


auto_tree *
new_auto_tree(char *def, qelem *ap)
{
  auto_tree *tp = CALLOC(struct auto_tree);

  tp->t_ioloc = current_location();
  tp->t_defaults = def;
  tp->t_mount = ap;
  show_new("auto_tree");
  return tp;
}


host *
new_host(void)
{
  host *hp = CALLOC(struct host);

  hp->h_ioloc = current_location();
  hp->h_mask = 0;
  show_new("host");
  return hp;
}


void
set_host(host *hp, int k, char *v)
{
  int m = 1 << k;

  if (hp->h_mask & m) {
    yyerror("host field \"%s\" already set", host_strings[k]);
    return;
  }
  hp->h_mask |= m;

  switch (k) {

  case HF_HOST:{
      char *p = strdup(v);
      dict_ent *de = dict_locate(dict_of_hosts, v);

      if (de)
	yyerror("duplicate host %s!", v);
      else
	dict_add(dict_of_hosts, v, (char *) hp);
      hp->h_hostname = v;
      domain_strip(p, hostname);
      if (strchr(p, '.') != 0)
	XFREE(p);
      else
	hp->h_lochost = p;
    }
    break;

  case HF_CONFIG:{
      qelem *q;
      qelem *vq = (qelem *) v;

      hp->h_mask &= ~m;
      if (hp->h_config)
	q = hp->h_config;
      else
	q = hp->h_config = new_que();
      ins_que(vq, q->q_back);
    }
    break;

  case HF_ETHER:{
      qelem *q;
      qelem *vq = (qelem *) v;

      hp->h_mask &= ~m;
      if (hp->h_ether)
	q = hp->h_ether;
      else
	q = hp->h_ether = new_que();
      ins_que(vq, q->q_back);
    }
    break;

  case HF_ARCH:
    hp->h_arch = v;
    break;

  case HF_OS:
    hp->h_os = v;
    break;

  case HF_CLUSTER:
    hp->h_cluster = v;
    break;

  default:
    abort();
    break;
  }
}


ether_if *
new_ether_if(void)
{
  ether_if *ep = CALLOC(struct ether_if);

  ep->e_mask = 0;
  ep->e_ioloc = current_location();
  show_new("ether_if");
  return ep;
}


void
set_ether_if(ether_if *ep, int k, char *v)
{
  int m = 1 << k;

  if (ep->e_mask & m) {
    yyerror("netif field \"%s\" already set", ether_if_strings[k]);
    return;
  }
  ep->e_mask |= m;

  switch (k) {

  case EF_INADDR:{
      ep->e_inaddr.s_addr = inet_addr(v);
      if (ep->e_inaddr.s_addr == (u_long) - 1)
	yyerror("malformed IP dotted quad: %s", v);
      XFREE(v);
    }
    break;

  case EF_NETMASK:{
      u_long nm = 0;

      if ((sscanf(v, "0x%lx", &nm) == 1 || sscanf(v, "%lx", &nm) == 1) && nm != 0)
	ep->e_netmask = htonl(nm);
      else
	yyerror("malformed netmask: %s", v);
      XFREE(v);
    }
    break;

  case EF_HWADDR:
    ep->e_hwaddr = v;
    break;

  default:
    abort();
    break;
  }
}


void
set_disk_fs(disk_fs *dp, int k, char *v)
{
  int m = 1 << k;

  if (dp->d_mask & m) {
    yyerror("fs field \"%s\" already set", disk_fs_strings[k]);
    return;
  }
  dp->d_mask |= m;

  switch (k) {

  case DF_FSTYPE:
    dp->d_fstype = v;
    break;

  case DF_OPTS:
    dp->d_opts = v;
    break;

  case DF_DUMPSET:
    dp->d_dumpset = v;
    break;

  case DF_LOG:
    dp->d_log = v;
    break;

  case DF_PASSNO:
    dp->d_passno = atoi(v);
    XFREE(v);
    break;

  case DF_FREQ:
    dp->d_freq = atoi(v);
    XFREE(v);
    break;

  case DF_MOUNT:
    dp->d_mount = &((fsi_mount *) v)->m_q;
    break;

  default:
    abort();
    break;
  }
}


disk_fs *
new_disk_fs(void)
{
  disk_fs *dp = CALLOC(struct disk_fs);

  dp->d_ioloc = current_location();
  show_new("disk_fs");
  return dp;
}


void
set_mount(fsi_mount *mp, int k, char *v)
{
  int m = 1 << k;

  if (mp->m_mask & m) {
    yyerror("mount tree field \"%s\" already set", mount_strings[k]);
    return;
  }
  mp->m_mask |= m;

  switch (k) {

  case DM_VOLNAME:
    dict_add(dict_of_volnames, v, (char *) mp);
    mp->m_volname = v;
    break;

  case DM_EXPORTFS:
    mp->m_exportfs = v;
    break;

  case DM_SEL:
    mp->m_sel = v;
    break;

  default:
    abort();
    break;
  }
}


fsi_mount *
new_mount(void)
{
  fsi_mount *fp = CALLOC(struct fsi_mount);

  fp->m_ioloc = current_location();
  show_new("mount");
  return fp;
}


void
set_fsmount(fsmount *fp, int k, char *v)
{
  int m = 1 << k;

  if (fp->f_mask & m) {
    yyerror("mount field \"%s\" already set", fsmount_strings[k]);
    return;
  }
  fp->f_mask |= m;

  switch (k) {

  case FM_LOCALNAME:
    fp->f_localname = v;
    break;

  case FM_VOLNAME:
    fp->f_volname = v;
    break;

  case FM_FSTYPE:
    fp->f_fstype = v;
    break;

  case FM_OPTS:
    fp->f_opts = v;
    break;

  case FM_FROM:
    fp->f_from = v;
    break;

  case FM_DIRECT:
    break;

  default:
    abort();
    break;
  }
}


fsmount *
new_fsmount(void)
{
  fsmount *fp = CALLOC(struct fsmount);

  fp->f_ioloc = current_location();
  show_new("fsmount");
  return fp;
}


void
init_que(qelem *q)
{
  q->q_forw = q->q_back = q;
}


qelem *
new_que(void)
{
  qelem *q = CALLOC(qelem);

  init_que(q);
  return q;
}


void
ins_que(qelem *elem, qelem *pred)
{
  qelem *p;

  p = pred->q_forw;
  elem->q_back = pred;
  elem->q_forw = p;
  pred->q_forw = elem;
  p->q_back = elem;
}


void
rem_que(qelem *elem)
{
  qelem *p, *p2;

  p = elem->q_forw;
  p2 = elem->q_back;

  p2->q_forw = p;
  p->q_back = p2;
}
OpenPOWER on IntegriCloud