瀏覽代碼

Add setcloexec function to set close-on-exec flags, and use that everywhere

Wichert Akkerman 25 年之前
父節點
當前提交
eda3be6285
共有 4 個文件被更改,包括 19 次插入4 次删除
  1. 2 0
      include/dpkg.h.in
  2. 3 1
      lib/dbmodify.c
  3. 11 0
      lib/mlib.c
  4. 3 3
      main/filesdb.c

+ 2 - 0
include/dpkg.h.in

@@ -3,6 +3,7 @@
  * dpkg.h - general header for Debian package handling
  *
  * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
+ * Copyright (C) 2000,2001 Wichert Akkerman <wichert@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
@@ -190,6 +191,7 @@ void werr(const char *what) NONRETURNING;
 
 /*** from mlib.c ***/
 
+void setcloexec(int fd, const char* fn);
 void *m_malloc(size_t);
 void *m_realloc(void*, size_t);
 int m_fork(void);

+ 3 - 1
lib/dbmodify.c

@@ -3,6 +3,7 @@
  * dbmodify.c - routines for managing dpkg database updates
  *
  * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
+ * Copyright (C) 2001 Wichert Akkerman <wichert@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
@@ -100,12 +101,13 @@ static void cleanupdates(void) {
 }
 
 static void createimptmp(void) {
-  int i;
+  int i, f;
   
   onerr_abort++;
   
   importanttmp= fopen(importanttmpfile,"w");
   if (!importanttmp) ohshite(_("unable to create %.250s"),importanttmpfile);
+  setcloexec(fileno(importanttmp),importanttmpfile);
   for (i=0; i<512; i++) fputs("#padding\n",importanttmp);
   if (ferror(importanttmp))
     ohshite(_("unable to fill %.250s with padding"),importanttmpfile);

+ 11 - 0
lib/mlib.c

@@ -25,6 +25,8 @@
 #include <string.h>
 #include <signal.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <sys/wait.h>
 #include <sys/types.h>
 
@@ -136,6 +138,15 @@ int waitsubproc(pid_t pid, const char *description, int flags) {
   return checksubprocerr(status,description,flags);
 }
 
+void setcloexec(int fd, const char* fn) {
+  int f;
+
+  if ((f=fcntl(fd, F_GETFD))==-1)
+    ohshite(_("unable to read filedescriptor flags for %.250s"),fn);
+  if (fcntl(fd, F_SETFD, (f|FD_CLOEXEC))==-1)
+    ohshite(_("unable to set close-on-exec flag for %.250s"),fn);
+}
+
 struct buffer_write_md5ctx {
   struct MD5Context ctx;
   unsigned char **hash;

+ 3 - 3
main/filesdb.c

@@ -3,7 +3,7 @@
  * filesdb.c - management of database of files installed on system
  *
  * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
- * Copyright (C) 2000 Wichert Akkerman <wakkerma@debian.org>
+ * Copyright (C) 2000,2001 Wichert Akkerman <wakkerma@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
@@ -335,6 +335,7 @@ void ensure_statoverrides(void) {
   }
   if (statoverridefile) fclose(statoverridefile);
   statoverridefile= file;
+  setcloexec(fileno(statoverridefile), vb.buf);
 
   /* If the statoverride list is empty we don't need to bother reading it. */
   if (!stab2.st_size) {
@@ -454,10 +455,9 @@ void ensure_diversions(void) {
       fclose(file); onerr_abort--; return;
     }
   }
-  l= fcntl(fileno(file), F_GETFD);
-  if (l >= 0) fcntl(fileno(file), F_SETFD, l | FD_CLOEXEC);
   if (diversionsfile) fclose(diversionsfile);
   diversionsfile= file;
+  setcloexec(fileno(diversionsfile), vb.buf);
 
   for (ov= diversions; ov; ov= ov->next) {
     ov->useinstead->divert->camefrom->divert= 0;