File: ulocks.c

package info (click to toggle)
clearsilver 0.10.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: ansic: 24,586; python: 4,233; sh: 2,502; cs: 1,429; ruby: 819; java: 735; makefile: 589; perl: 120; lisp: 34; sql: 21
file content (204 lines) | stat: -rw-r--r-- 3,646 bytes parent folder | download | duplicates (10)
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
/*
 * Copyright 2001-2004 Brandon Long
 * All Rights Reserved.
 *
 * ClearSilver Templating System
 *
 * This code is made available under the terms of the ClearSilver License.
 * http://www.clearsilver.net/license.hdf
 *
 */

#include "cs_config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "neo_misc.h"
#include "neo_err.h"
#include "neo_files.h"
#include "ulocks.h"

NEOERR *fCreate(int *plock, const char *file) 
{
  NEOERR *err;
  int lock;
  char *p;

  *plock = -1;

  /* note the default mode of 666 is possibly a security hole in that
   * someone else can grab your lock and DoS you.  For internal use, who
   * cares?
   */
  if((lock = open(file, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT|O_EXCL, 0666)) < 0) 
  {
    if (errno == ENOENT)
    {
      p = strrchr (file, '/');
      if (p != NULL)
      {
	*p = '\0';
	err = ne_mkdirs(file, 0777);
	*p = '/';
	if (err != STATUS_OK) return nerr_pass(err);
	lock = open(file, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0666);
      }
    }
    if (errno == EEXIST)
      return nerr_pass(fFind(plock, file));

    if (lock < 0)
      return nerr_raise_errno (NERR_IO, "Unable to open lock file %s", file);
  }

  *plock = lock;

  return STATUS_OK;
}

void fDestroy(int lock) 
{

  if(lock < 0)
    return;

  close(lock);

  return;
}

NEOERR *fFind(int *plock, const char *file) 
{
  int lock;

  *plock = -1;

  if((lock = open(file, O_WRONLY|O_NDELAY|O_APPEND, 0666)) < 0) {
    if (errno == ENOENT)
      return nerr_raise (NERR_NOT_FOUND, "Unable to find lock file %s", file);
    return nerr_raise_errno (NERR_IO, "Unable to open lock file %s", file);
  }

  *plock = lock;

  return STATUS_OK;
}

NEOERR *fLock(int lock) 
{

  if(lockf(lock, F_LOCK, 0) < 0)
    return nerr_raise_errno (NERR_LOCK, "File lock failed");

  return STATUS_OK;
}

void fUnlock(int lock) 
{

  if(lock < 0)
    return;

  lockf(lock, F_ULOCK, 0);

  return;
}

#ifdef HAVE_PTHREADS

NEOERR *mCreate(pthread_mutex_t *mutex) 
{
  int err;

  if((err = pthread_mutex_init(mutex, NULL))) {
    return nerr_raise (NERR_LOCK, "Unable to initialize mutex: %s", 
	strerror(err));
  }

  return STATUS_OK;
}

void mDestroy(pthread_mutex_t *mutex) 
{

  pthread_mutex_destroy(mutex);  

  return;
}

NEOERR *mLock(pthread_mutex_t *mutex) 
{
  int err;

  if((err = pthread_mutex_lock(mutex)))
    return nerr_raise(NERR_LOCK, "Mutex lock failed: %s", strerror(err));

  return STATUS_OK;
}

NEOERR *mUnlock(pthread_mutex_t *mutex) 
{
  int err;

  if((err = pthread_mutex_unlock(mutex)))
    return nerr_raise(NERR_LOCK, "Mutex unlock failed: %s", strerror(err));

  return STATUS_OK;
}

NEOERR *cCreate(pthread_cond_t *cond) 
{
  int err;

  if((err = pthread_cond_init(cond, NULL))) {
    return nerr_raise(NERR_LOCK, "Unable to initialize condition variable: %s", 
	strerror(err));
  }

  return STATUS_OK;
}

void cDestroy(pthread_cond_t *cond) 
{
  pthread_cond_destroy(cond);  

  return;
}

NEOERR *cWait(pthread_cond_t *cond, pthread_mutex_t *mutex) 
{
  int err;

  if((err = pthread_cond_wait(cond, mutex)))
    return nerr_raise(NERR_LOCK, "Condition wait failed: %s", strerror(err));

  return STATUS_OK;
}

NEOERR *cBroadcast(pthread_cond_t *cond) 
{
  int err;

  if((err = pthread_cond_broadcast(cond)))
    return nerr_raise(NERR_LOCK, "Condition broadcast failed: %s", 
	strerror(err));

  return STATUS_OK;
}

NEOERR *cSignal(pthread_cond_t *cond) 
{
  int err;

  if((err = pthread_cond_signal(cond)))
    return nerr_raise (NERR_LOCK, "Condition signal failed: %s", strerror(err));

  return STATUS_OK;
}

#endif