[sr-dev] git:3.1: core: enable dumpable flag after setuid() on Linux

Daniel-Constantin Mierla miconda at gmail.com
Sun Mar 6 12:14:59 CET 2011


Module: sip-router
Branch: 3.1
Commit: 487fef38bf962ff778f9380639cf33cf6c55ef80
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=487fef38bf962ff778f9380639cf33cf6c55ef80

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Sun Mar  6 00:51:41 2011 +0100

core: enable dumpable flag after setuid() on Linux

- init rlimit struct for proper dbg message when core limits are not
  changed
(cherry picked from commit f4136d7742b0b0bfc09a9157d573c8a7f6118b76)

---

 daemonize.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 daemonize.h |    2 +-
 2 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/daemonize.c b/daemonize.c
index 7411652..f131212 100644
--- a/daemonize.c
+++ b/daemonize.c
@@ -58,6 +58,10 @@
 #include <pwd.h>
 #include <grp.h>
 
+#ifdef __OS_linux
+#include <sys/prctl.h>
+#endif
+
 #ifdef HAVE_SCHED_SETSCHEDULER
 #include <sched.h>
 #endif
@@ -202,6 +206,40 @@ void daemon_status_no_wait()
 }
 
 
+/**
+ * enable dumpable flag for core dumping after setuid() & friends
+ * @return 0 when no critical error occured, -1 on such error
+ */
+int enable_dumpable(void)
+{
+#ifdef __OS_linux
+	struct rlimit lim;
+	/* re-enable core dumping on linux after setuid() & friends */
+	if(disable_core_dump==0) {
+		LM_DBG("trying enable core dumping...\n");
+		if(prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)<=0) {
+			LM_DBG("core dumping is disabled now...\n");
+			if(prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)<0) {
+				LM_WARN("cannot re-enable core dumping!\n");
+			} else {
+				LM_DBG("core dumping has just been enabled...\n");
+				if (getrlimit(RLIMIT_CORE, &lim)<0){
+					LOG(L_CRIT, "cannot get the maximum core size: %s\n",
+							strerror(errno));
+					return -1;
+				} else {
+					DBG("current core file limit: %lu (max: %lu)\n",
+						(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
+				}
+			}
+		} else {
+			LM_DBG("core dumping is enabled now (%d)...\n",
+					prctl(PR_GET_DUMPABLE, 0, 0, 0, 0));
+		}
+	}
+#endif
+	return 0;
+}
 
 /** daemon init.
  *@param name - daemon name used for logging (used when opening syslog).
@@ -289,6 +327,10 @@ int daemonize(char*  name,  int status_wait)
 			exit(0);
 		}
 	}
+
+	if(enable_dumpable()<0)
+		goto error;
+
 	/* added by noh: create a pid file for the main process */
 	if (pid_file!=0){
 		
@@ -415,6 +457,10 @@ int do_suid()
 			goto error;
 		}
 	}
+
+	if(enable_dumpable()<0)
+		goto error;
+
 	return 0;
 error:
 	return -1;
@@ -477,7 +523,7 @@ error:
 
 
 /*! \brief enable core dumps */
-int set_core_dump(int enable, int size)
+int set_core_dump(int enable, long unsigned int size)
 {
 	struct rlimit lim;
 	struct rlimit newlim;
@@ -510,8 +556,11 @@ int set_core_dump(int enable, int size)
 						(unsigned long)lim.rlim_max);
 			}
 			goto error; /* it's an error we haven't got the size we wanted*/
+		}else{
+			newlim.rlim_cur=lim.rlim_cur;
+			newlim.rlim_max=lim.rlim_max;
+			goto done; /*nothing to do */
 		}
-		goto done; /*nothing to do */
 	}else{
 		/* disable */
 		newlim.rlim_cur=0;
diff --git a/daemonize.h b/daemonize.h
index 622a710..9003a01 100644
--- a/daemonize.h
+++ b/daemonize.h
@@ -30,7 +30,7 @@
 int daemonize(char* name, int daemon_status_fd_input);
 int do_suid();
 int increase_open_fds(int target);
-int set_core_dump(int enable, int size);
+int set_core_dump(int enable, long unsigned int size);
 int mem_lock_pages();
 int set_rt_prio(int prio, int policy);
 




More information about the sr-dev mailing list