File: lock.c

package info (click to toggle)
fsp 2.71-8hamm10
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 988 kB
  • ctags: 1,287
  • sloc: ansic: 7,715; makefile: 363; sh: 118
file content (249 lines) | stat: -rw-r--r-- 5,868 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
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
    /*********************************************************************\
    *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
    *                                                                     *
    *  You may copy or modify this file in any manner you wish, provided  *
    *  that this notice is always included, and that you hold the author  *
    *  harmless for any loss or damage resulting from the installation or *
    *  use of this software.                                              *
    \*********************************************************************/

#include "tweak.h"
#include "client_def.h"
#include "c_extern.h"

#ifndef NOLOCKING
static char key_string[sizeof(KEY_PREFIX)+32];
     
static char code_str[] =
       "0123456789:_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     
static void make_key_string PROTO2(unsigned long, server_addr,
				   unsigned long, server_port)
{
  unsigned long v1, v2;
  char *p;
  
  strcpy(key_string,KEY_PREFIX);
  for(p = key_string; *p; p++);
  v1 = server_addr;
  v2 = server_port;
  
  *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  v1 = v1 | (v2 << (32-3*6));
  *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  *p   = 0;
}

/********************************************************************/
/******* For those systems that has flock function call *************/
/********************************************************************/
#ifdef USE_FLOCK

#include <sys/file.h>

int key_persists = 1;
static unsigned int lock_fd;
static unsigned short okey;

unsigned short client_get_key PROTO0((void))
{
  if(flock(lock_fd,LOCK_EX) == -1) {
    perror("flock");
    exit(1);
  }
  if(read(lock_fd,&okey,sizeof(okey)) == -1) {
    perror("read"); exit(1);
  }
  if(lseek(lock_fd,0L,0) == -1) {
    perror("seek");
    exit(1);
  }
  return(okey);
}

void client_put_key PROTO1(unsigned short, key)
{
  if(write(lock_fd,&key,sizeof(key)) == -1) {
    perror("write");
    exit(1);
  }
  if(lseek(lock_fd,0L,0) == -1) {
    perror("seek");
    exit(1);
  }
  if(flock(lock_fd,LOCK_UN) == -1) {
    perror("unflock");
    exit(1);
  }
}

void client_init_key PROTO3(unsigned long, server_addr,
			    unsigned long, server_port,
			    unsigned short, key)
{
  unsigned long omask;
  okey = key;
  
  make_key_string(server_addr,server_port);
  
  omask = umask(0);
  lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  umask(omask);
}

#endif
/********************************************************************/
/******* For those systems that has lockf function call *************/
/********************************************************************/
#ifdef USE_LOCKF

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

int key_persists = 1;
static unsigned int lock_fd;
static unsigned short okey;

unsigned short client_get_key PROTO0((void))
{
  if(lockf(lock_fd,F_LOCK,sizeof(okey)) == -1) {
    perror("lockf");
    exit(1);
  }
  if(read(lock_fd,&okey,sizeof(okey)) == -1) {
    perror("read");
    exit(1);
  }
  if(lseek(lock_fd,0L,0) == -1) {
    perror("seek");
    exit(1);
  }
  return(okey);
}

void client_put_key PROTO1(unsigned short, key)
{
  if(write(lock_fd,&key,sizeof(key)) == -1) {
    perror("write");
    exit(1);
  }
  if(lseek(lock_fd,0L,0) == -1) {
    perror("seek");
    exit(1);
  }
  if(lockf(lock_fd,F_ULOCK,sizeof(key)) == -1) {
    perror("unlockf");
    exit(1);
  }
}

void client_init_key PROTO3(unsigned long, server_addr,
			    unsigned long, server_port,
			    unsigned short, key)
{
  unsigned long omask;
  okey = key;
  
  make_key_string(server_addr,server_port);
  
  omask = umask(0);
  lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  umask(omask);
}

#endif
/********************************************************************/
/******* For those systems that has SysV shared memory + lockf ******/
/********************************************************************/
#ifdef USE_SHAREMEM_AND_LOCKF

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/ipc.h>
extern char *shmat();

int key_persists = 0;
static unsigned short *share_key;
static unsigned int lock_fd;

unsigned short client_get_key PROTO0((void))
{
  if(lockf(lock_fd,F_LOCK,2) == -1) {
    perror("lockf");
    exit(1);
  }
  return(*share_key);
}

void client_put_key PROTO1(unsigned short, key)
{
  *share_key = key;
  if(lockf(lock_fd,F_ULOCK,2) == -1) {
    perror("unlockf");
    exit(1);
  }
}

void client_init_key PROTO3(unsigned long, server_addr,
			    unsigned long, server_port,
			    unsigned short, key)
{
  unsigned long omask;
  key_t lock_key;
  int   lock_shm;
  
  make_key_string(server_addr,server_port);
  
  omask = umask(0);
  lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  umask(omask);
  
  if((lock_key = ftok(key_string,3432)) == -1) {
    perror("ftok");
    exit(1);
  }
  if((lock_shm = shmget(lock_key,sizeof(short),IPC_CREAT|0666)) == -1) {
    perror("shmget");
    exit(1);
  }
  if(!(share_key = (unsigned short *) shmat(lock_shm,(char*)0,0))) {
    perror("shmat");
    exit(1);
  }
}

#endif
/********************************************************************/
/******* For those who does not want to use locking *****************/
/********************************************************************/
#else /* NOLOCKING */

int key_persists = 0;
static unsigned short okey;

unsigned short client_get_key PROTO0((void))
{
  return(okey);
}

void client_put_key PROTO1(unsigned short, key)
{
  okey = key;
}

void client_init_key PROTO3(unsigned long, server_addr,
			    unsigned long, server_port,
			    unsigned short, key)
{
  okey = key;
}

#endif