152
const struct fni *fnip;
157
for (fnip = fnis; fnip->suffix; fnip++) {
159
*fnip->store = dpkg_db_get_path(fnip->suffix);
162
updatefnbuf = m_malloc(strlen(updatesdir) + IMPORTANTMAXLEN + 5);
163
strcpy(updatefnbuf, updatesdir);
164
updatefnrest = updatefnbuf + strlen(updatefnbuf);
166
db_initialized = true;
172
const struct fni *fnip;
177
for (fnip = fnis; fnip->suffix; fnip++) {
183
db_initialized = false;
147
186
static int dblockfd = -1;
150
modstatdb_is_locked(const char *admindir)
189
modstatdb_is_locked(void)
152
struct varbuf lockfile = VARBUF_INIT;
156
varbufprintf(&lockfile, "%s/%s", admindir, LOCKFILE);
158
194
if (dblockfd == -1) {
159
lockfd = open(lockfile.buf, O_RDONLY);
195
lockfd = open(lockfile, O_RDONLY);
160
196
if (lockfd == -1)
161
ohshite(_("unable to open lock file %s for testing"), lockfile.buf);
197
ohshite(_("unable to open lock file %s for testing"), lockfile);
163
199
lockfd = dblockfd;
166
locked = file_is_locked(lockfd, lockfile.buf);
202
locked = file_is_locked(lockfd, lockfile);
168
204
/* We only close the file if there was no lock open, otherwise we would
169
205
* release the existing lock on close. */
170
206
if (dblockfd == -1)
173
varbuf_destroy(&lockfile);
179
modstatdb_lock(const char *admindir)
213
modstatdb_can_lock(void)
182
char *dblockfile = NULL;
184
n = strlen(admindir);
185
dblockfile = m_malloc(n + sizeof(LOCKFILE) + 2);
186
strcpy(dblockfile, admindir);
187
strcpy(dblockfile + n, "/" LOCKFILE);
218
dblockfd = open(lockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
189
219
if (dblockfd == -1) {
190
dblockfd = open(dblockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
191
if (dblockfd == -1) {
193
ohshit(_("you do not have permission to lock the dpkg status database"));
220
if (errno == EACCES || errno == EPERM)
194
223
ohshite(_("unable to open/create status database lockfile"));
198
file_lock(&dblockfd, dblockfile,
199
_("unable to lock dpkg status database"),
200
_("status database area is locked by another process"));
232
if (!modstatdb_can_lock())
233
ohshit(_("you do not have permission to lock the dpkg status database"));
235
file_lock(&dblockfd, FILE_LOCK_NOWAIT, lockfile, _("dpkg status database"));
206
239
modstatdb_unlock(void)
242
pop_cleanup(ehflag_normaltidy);
211
247
enum modstatdb_rw
212
modstatdb_init(const char *admindir, enum modstatdb_rw readwritereq)
248
modstatdb_open(enum modstatdb_rw readwritereq)
214
const struct fni *fnip;
216
for (fnip=fnis; fnip->suffix; fnip++) {
218
*fnip->store = m_malloc(strlen(admindir) + strlen(fnip->suffix) + 2);
219
sprintf(*fnip->store, "%s/%s", admindir, fnip->suffix);
222
cflags= readwritereq & msdbrw_flagsmask;
223
readwritereq &= ~msdbrw_flagsmask;
252
cflags = readwritereq & msdbrw_available_mask;
253
readwritereq &= ~msdbrw_available_mask;
225
255
switch (readwritereq) {
226
256
case msdbrw_needsuperuser:
227
257
case msdbrw_needsuperuserlockonly:
228
258
if (getuid() || geteuid())
229
259
ohshit(_("requested operation requires superuser privilege"));
231
261
case msdbrw_write: case msdbrw_writeifposs:
232
if (access(admindir, W_OK)) {
262
if (access(dpkg_db_get_dir(), W_OK)) {
233
263
if (errno != EACCES)
234
264
ohshite(_("unable to access dpkg status area"));
235
265
else if (readwritereq == msdbrw_write)
236
266
ohshit(_("operation requires read/write access to dpkg status area"));
237
267
cstatus= msdbrw_readonly;
239
modstatdb_lock(admindir);
240
270
cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
241
271
msdbrw_needsuperuserlockonly :
248
278
internerr("unknown modstatdb_rw '%d'", readwritereq);
251
updatefnbuf = m_malloc(strlen(updatesdir) + IMPORTANTMAXLEN + 5);
252
strcpy(updatefnbuf, updatesdir);
253
updatefnrest= updatefnbuf+strlen(updatefnbuf);
255
281
if (cstatus != msdbrw_needsuperuserlockonly) {
257
if(!(cflags & msdbrw_noavail))
283
if (cflags >= msdbrw_available_readonly)
258
284
parsedb(availablefile,
259
285
pdb_recordavailable | pdb_rejectstatus | pdb_lax_parser,
263
289
if (cstatus >= msdbrw_write) {
265
varbufinit(&uvb, 10240);
291
varbuf_init(&uvb, 10240);
268
294
trig_fixup_awaiters(cstatus);
269
trig_incorporate(cstatus, admindir);
295
trig_incorporate(cstatus);
292
319
void modstatdb_shutdown(void) {
293
const struct fni *fnip;
320
if (cflags >= msdbrw_available_write)
321
writedb(availablefile, 1, 0);
294
323
switch (cstatus) {
295
324
case msdbrw_write:
296
325
modstatdb_checkpoint();
297
writedb(availablefile,1,0);
298
/* tidy up a bit, but don't worry too much about failure */
326
/* Tidy up a bit, but don't worry too much about failure. */
299
327
fclose(importanttmp);
300
328
unlink(importanttmpfile);
301
329
varbuf_destroy(&uvb);
303
331
case msdbrw_needsuperuserlockonly:
304
332
modstatdb_unlock();
309
for (fnip=fnis; fnip->suffix; fnip++) {