File: lockfile-progs.c

package info (click to toggle)
lockfile-progs 0.1.9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 64 kB
  • ctags: 27
  • sloc: ansic: 166; makefile: 108; sh: 13
file content (208 lines) | stat: -rw-r--r-- 5,322 bytes parent folder | download | duplicates (2)
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
/* lockfile-progs.c

   Copyright 1998 Rob Browning <rlb@defaultvalue.org>

   This code is covered under the terms of the Gnu Public License.
   See the accompanying COPYING file for details.

  To do:

   It might be useful at some point to support a --user option to
   mail-lock that can only be used by the superuser (of course, they
   could just use lockfile-create with an appropriate path...

*/

#define _GNU_SOURCE

#include <lockfile.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <getopt.h>
#include <pwd.h>
#include <sys/types.h>

static const char *action = NULL;
static char *target_file = NULL;
static int retry_count = 9; /* This will be a maximum of 3 minutes */
static int touchlock_oneshot = 0;

/* not used yet, so not documented... */
static int lockfile_verbose = 0;

static int result = 0;

static void
usage(const char *command_name, FILE *file) {
  char *usage_str = NULL;

  if(strcmp(command_name, "mail-lock") == 0) {
    usage_str = "usage: mail-lock [ --retry retry-count ]\n";
  } else if(strcmp(command_name, "mail-unlock") == 0) {
    usage_str = "usage: mail-unlock\n";
  } else if(strcmp(command_name, "mail-touchlock") == 0) {
    usage_str = "usage: mail-touchlock [ --oneshot ]\n";
  } else if(strcmp(command_name, "lockfile-create") == 0) {
    usage_str = "usage: lockfile-create [ --retry retry-count ] file\n";
  } else if(strcmp(command_name, "lockfile-remove") == 0) {
    usage_str = "usage: lockfile-remove file\n";
  } else if(strcmp(command_name, "lockfile-touch") == 0) {
    usage_str = "usage: lockfile-touch [ --oneshot ] file\n";
  } else {
    fprintf(stderr, "lockfile: big problem unknown command name: %s\n",
            command_name);
    exit(1);
  }
  fprintf(file, usage_str);
}

static void
parse_arguments(const int argc, char *argv[]) {

  int opt_result;
  struct option opt_specs[] = {
    { "retry", required_argument, NULL, 'r' },
    { "oneshot", no_argument, NULL, 'o' },
    //{ "verbose", no_argument, NULL, 'v' },
    { NULL, 0, NULL, 0 }
  };
  
  char *cmd_name = rindex(argv[0], '/');
  int mail_cmd_p = 0;
  
  if(cmd_name != NULL) {
    /* Skip the '/' */
    cmd_name++;
  } else {
    cmd_name = argv[0];
  }

  while((opt_result = getopt_long(argc, argv, "", opt_specs, NULL)) != -1) {
    switch(opt_result) {
      case 'o':
        touchlock_oneshot = 1;
        break;
      case 'v':
        lockfile_verbose = 1;
        break;
      case 'r':
        {
          char *rest_of_string;
          long tmp_value = strtol(optarg, &rest_of_string, 10);
          
          if((tmp_value == 0) && (rest_of_string == optarg)) {
            /* Bad value */
            fprintf(stderr, "%s: bad retry-count value\n", cmd_name);
            usage(cmd_name, stderr);
            exit(1);
          } else {
            retry_count = tmp_value;
          }
        }
        break;
      default:
        fprintf(stderr, "%s: getopt returned impossible value 0%o.\n",
                cmd_name, opt_result);
        exit(1);
        break;
    }
  }
  
  if(strcmp(cmd_name, "mail-lock") == 0) {
    action = "lock";
    mail_cmd_p = 1;
  } 
  else if(strcmp(cmd_name, "mail-unlock") == 0) {
    action = "unlock";
    mail_cmd_p = 1;
  }
  else if(strcmp(cmd_name, "mail-touchlock") == 0) {
    action = "touch";
    mail_cmd_p = 1;
  }
  else if(strcmp(cmd_name, "lockfile-create") == 0) {
    action = "lock";
  } 
  else if(strcmp(cmd_name, "lockfile-remove") == 0) {
    action = "unlock";
  }
  else if(strcmp(cmd_name, "lockfile-touch") == 0) {
    action = "touch";
  } else {
    usage(cmd_name, stderr);
    exit(1);
  }

  if(mail_cmd_p) {
    if(optind == argc) {
      uid_t user_id = geteuid();
      struct passwd *user_info = getpwuid(user_id);

      if(user_info == NULL) {
        fprintf(stderr, "%s: fatal error, can't find info for user id %ud\n",
                cmd_name, user_id);
        exit(1);
      }
      
      if(asprintf(&target_file, "/var/spool/mail/%s",
                  user_info->pw_name) == -1) {
        fprintf(stderr, "asprintf failed: line %d\n", __LINE__);
        exit(1);
      }
    } else {
      usage(cmd_name, stderr);
      exit(1);
    }
  } else {
    if((argc - optind) != 1) {
      usage(cmd_name, stderr);
      exit(1);
    }
    target_file = argv[optind];
  }
}

static void
handle_touchdeath(int sig) {
  exit(result);
}

int
main(int argc, char *argv[]) {
  char * lockfilename = NULL;

  parse_arguments(argc, argv);

  if(asprintf(&lockfilename, "%s.lock", target_file) == -1) {
    fprintf(stderr, "asprintf failed: line %d\n", __LINE__);
    exit(1);
  }
  
  if(strcmp(action, "unlock") == 0) {
    result = lockfile_remove(lockfilename);
  } else if(strcmp(action, "lock") == 0) {
    if(lockfile_create(lockfilename, retry_count, 0) == L_SUCCESS) {
      result = 0;
    } else {
      fprintf(stderr, "lockfile creation failed\n");
      result = 1;
    }
  } else if(strcmp(action, "touch") == 0) {
    signal(SIGTERM, handle_touchdeath);

    if(touchlock_oneshot) {
      result = lockfile_touch(lockfilename);
    } else {
      while(1 && (result == 0)) {
        result = lockfile_touch(lockfilename);
        sleep(60);
      }
    }
  }

  if(lockfilename) free(lockfilename);
  return(result);
}