~ubuntu-branches/ubuntu/maverick/cluster-agents/maverick-proposed

« back to all changes in this revision

Viewing changes to tools/sfex_init.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2010-02-17 21:46:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100217214600-g44grvtkw7jbpciz
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * Shared Disk File EXclusiveness Control Program(SF-EX)
 
4
 *
 
5
 * sfex_init.c --- Initialize SF-EX meta-data.
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU General Public License
 
9
 * as published by the Free Software Foundation; either version 2
 
10
 * of the License, or (at your option) any later version.
 
11
 * 
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 
20
 * 02110-1301, USA.
 
21
 *
 
22
 * Copyright (c) 2007 NIPPON TELEGRAPH AND TELEPHONE CORPORATION
 
23
 *
 
24
 * $Id$
 
25
 *
 
26
 *-------------------------------------------------------------------------
 
27
 *
 
28
 * sfex_init [-b <blocksize>] [-n <numlocks>] <device>
 
29
 *
 
30
 * -b <blocksize> --- The size of the block is specified by the number of 
 
31
 * bytes. In general, to prevent a partial writing to the disk, the size 
 
32
 * of block is set to 512 bytes etc. 
 
33
 * Note a set value because this value is used also for the alignment 
 
34
 * adjustment in the input-output buffer in the program when direct I/O 
 
35
 * is used(When you specify --enable-directio option for configure script). 
 
36
 * (In Linux kernel 2.6, "direct I/O " does not work if this value is not 
 
37
 * a multiple of 512.) Default is 512 bytes.
 
38
 *
 
39
 * -n <numlocks> --- The number of storing lock data is specified by integer 
 
40
 * of one or more. When you want to control two or more resources by one 
 
41
 * meta-data, you set the value of two or more to numlocks. A necessary disk 
 
42
 * area for meta data are (blocksize*(1+numlocks))bytes. Default is 1.
 
43
 *
 
44
 * <device> --- This is file path which stored meta-data. It is usually 
 
45
 * expressed in "/dev/...", because it is partition on the shared disk.
 
46
 *
 
47
 * exit code --- 0 - Normal end. 3 - Error occurs while processing it. 
 
48
 * The content of the error is displayed into stderr. 4 - The mistake is 
 
49
 * found in the command line parameter.
 
50
 *
 
51
 *-------------------------------------------------------------------------*/
 
52
 
 
53
#include <stdio.h>
 
54
#include <stdlib.h>
 
55
#include <sys/types.h>
 
56
#include <errno.h>
 
57
#include <string.h>
 
58
#include <unistd.h>
 
59
#include <getopt.h>
 
60
#include <syslog.h>
 
61
 
 
62
#include "sfex.h"
 
63
#include "sfex_lib.h"
 
64
 
 
65
const char *progname;
 
66
char *nodename;
 
67
 
 
68
/*
 
69
 * usage --- display command line syntax
 
70
 *
 
71
 * display command line syntax. By the purpose, it can specify destination 
 
72
 * stream. stdout or stderr is set usually.
 
73
 *
 
74
 * dist --- destination stream of the command line syntax, such as stderr.
 
75
 *
 
76
 * return value --- void
 
77
 */
 
78
static void usage(FILE *dist) {
 
79
  fprintf(dist, "usage: %s [-n <numlocks>] <device>\n", progname);
 
80
}
 
81
 
 
82
/*
 
83
 * main --- main function
 
84
 *
 
85
 * entry point of sfex_init command.
 
86
 *
 
87
 * exit code --- 0 - Normal end. 3 - Error occurs while processing it. 
 
88
 * The content of the error is displayed into stderr. 4 - The mistake is 
 
89
 * found in the command line parameter.
 
90
 */
 
91
int
 
92
main(int argc, char *argv[]) {
 
93
  sfex_controldata cdata;
 
94
  sfex_lockdata ldata;
 
95
 
 
96
  /* command line parameter */
 
97
  int numlocks = 1;             /* default 1 locks  */
 
98
  const char *device;
 
99
 
 
100
  /*
 
101
   *  startup process
 
102
   */
 
103
 
 
104
/*
 
105
  openlog("SFex Init", LOG_PID|LOG_CONS, LOG_USER);
 
106
*/
 
107
 
 
108
  /* get a program name */
 
109
  progname = get_progname(argv[0]);
 
110
 
 
111
  /* read command line option */
 
112
  opterr = 0;
 
113
  while (1) {
 
114
    int c = getopt(argc, argv, "hn:");
 
115
    if (c == -1)
 
116
      break;
 
117
    switch (c) {
 
118
    case 'h':                   /* help */
 
119
      usage(stdout);
 
120
      exit(0);
 
121
    case 'n':                   /* -n <numlocks> */
 
122
      {
 
123
        unsigned long l = strtoul(optarg, NULL, 10);
 
124
        if (l < SFEX_MIN_NUMLOCKS || l > SFEX_MAX_NUMLOCKS) {
 
125
          fprintf(stderr,
 
126
                  "%s: ERROR: numlocks %s is out of range or invalid. it must be integer value between %lu and %lu.\n",
 
127
                  progname, optarg,
 
128
                  (unsigned long)SFEX_MIN_NUMLOCKS,
 
129
                  (unsigned long)SFEX_MAX_NUMLOCKS);
 
130
          exit(4);
 
131
        }
 
132
        numlocks = l;
 
133
      }
 
134
      break;
 
135
    case '?':                   /* error */
 
136
      usage(stderr);
 
137
      exit(4);
 
138
    }
 
139
  }
 
140
 
 
141
  /* check parameter except the option */
 
142
  if (optind >= argc) {
 
143
    fprintf(stderr, "%s: ERROR: no device specified.\n", progname);
 
144
    usage(stderr);
 
145
    exit(4);
 
146
  } else if (optind + 1 < argc) {
 
147
    fprintf(stderr, "%s: ERROR: too many arguments.\n", progname);
 
148
    usage(stderr);
 
149
    exit(4);
 
150
  }
 
151
  device = argv[optind];
 
152
 
 
153
  prepare_lock(device);
 
154
 
 
155
  /* main processes start */
 
156
 
 
157
  /* get a node name */
 
158
  nodename = get_nodename();
 
159
 
 
160
  /* create and control data and lock data */
 
161
  init_controldata(&cdata, sector_size, numlocks);
 
162
  init_lockdata(&ldata);
 
163
 
 
164
  /* write out control data and lock data */
 
165
  write_controldata(&cdata);
 
166
  {
 
167
    int index;
 
168
    for (index = 1; index <= numlocks; index++)
 
169
      write_lockdata(&cdata, &ldata, index);
 
170
  }
 
171
 
 
172
  exit(0);
 
173
}