Browse code

core: enable dumpable flag after setuid() on Linux

- init rlimit struct for proper dbg message when core limits are not
changed

Daniel-Constantin Mierla authored on 05/03/2011 23:51:41
Showing 2 changed files
... ...
@@ -58,6 +58,10 @@
58 58
 #include <pwd.h>
59 59
 #include <grp.h>
60 60
 
61
+#ifdef __OS_linux
62
+#include <sys/prctl.h>
63
+#endif
64
+
61 65
 #ifdef HAVE_SCHED_SETSCHEDULER
62 66
 #include <sched.h>
63 67
 #endif
... ...
@@ -202,6 +206,40 @@ void daemon_status_no_wait()
202 202
 }
203 203
 
204 204
 
205
+/**
206
+ * enable dumpable flag for core dumping after setuid() & friends
207
+ * @return 0 when no critical error occured, -1 on such error
208
+ */
209
+int enable_dumpable(void)
210
+{
211
+#ifdef __OS_linux
212
+	struct rlimit lim;
213
+	/* re-enable core dumping on linux after setuid() & friends */
214
+	if(disable_core_dump==0) {
215
+		LM_DBG("trying enable core dumping...\n");
216
+		if(prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)<=0) {
217
+			LM_DBG("core dumping is disabled now...\n");
218
+			if(prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)<0) {
219
+				LM_WARN("cannot re-enable core dumping!\n");
220
+			} else {
221
+				LM_DBG("core dumping has just been enabled...\n");
222
+				if (getrlimit(RLIMIT_CORE, &lim)<0){
223
+					LOG(L_CRIT, "cannot get the maximum core size: %s\n",
224
+							strerror(errno));
225
+					return -1;
226
+				} else {
227
+					DBG("current core file limit: %lu (max: %lu)\n",
228
+						(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
229
+				}
230
+			}
231
+		} else {
232
+			LM_DBG("core dumping is enabled now (%d)...\n",
233
+					prctl(PR_GET_DUMPABLE, 0, 0, 0, 0));
234
+		}
235
+	}
236
+#endif
237
+	return 0;
238
+}
205 239
 
206 240
 /** daemon init.
207 241
  *@param name - daemon name used for logging (used when opening syslog).
... ...
@@ -289,6 +327,10 @@ int daemonize(char*  name,  int status_wait)
289 289
 			exit(0);
290 290
 		}
291 291
 	}
292
+
293
+	if(enable_dumpable()<0)
294
+		goto error;
295
+
292 296
 	/* added by noh: create a pid file for the main process */
293 297
 	if (pid_file!=0){
294 298
 		
... ...
@@ -415,6 +457,10 @@ int do_suid()
415 415
 			goto error;
416 416
 		}
417 417
 	}
418
+
419
+	if(enable_dumpable()<0)
420
+		goto error;
421
+
418 422
 	return 0;
419 423
 error:
420 424
 	return -1;
... ...
@@ -477,7 +523,7 @@ error:
477 477
 
478 478
 
479 479
 /*! \brief enable core dumps */
480
-int set_core_dump(int enable, int size)
480
+int set_core_dump(int enable, long unsigned int size)
481 481
 {
482 482
 	struct rlimit lim;
483 483
 	struct rlimit newlim;
... ...
@@ -510,8 +556,11 @@ int set_core_dump(int enable, int size)
510 510
 						(unsigned long)lim.rlim_max);
511 511
 			}
512 512
 			goto error; /* it's an error we haven't got the size we wanted*/
513
+		}else{
514
+			newlim.rlim_cur=lim.rlim_cur;
515
+			newlim.rlim_max=lim.rlim_max;
516
+			goto done; /*nothing to do */
513 517
 		}
514
-		goto done; /*nothing to do */
515 518
 	}else{
516 519
 		/* disable */
517 520
 		newlim.rlim_cur=0;
... ...
@@ -30,7 +30,7 @@
30 30
 int daemonize(char* name, int daemon_status_fd_input);
31 31
 int do_suid();
32 32
 int increase_open_fds(int target);
33
-int set_core_dump(int enable, int size);
33
+int set_core_dump(int enable, long unsigned int size);
34 34
 int mem_lock_pages();
35 35
 int set_rt_prio(int prio, int policy);
36 36