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 206
 }
203 207
 
204 208
 
209
+/**
210
+ * enable dumpable flag for core dumping after setuid() & friends
211
+ * @return 0 when no critical error occured, -1 on such error
212
+ */
213
+int enable_dumpable(void)
214
+{
215
+#ifdef __OS_linux
216
+	struct rlimit lim;
217
+	/* re-enable core dumping on linux after setuid() & friends */
218
+	if(disable_core_dump==0) {
219
+		LM_DBG("trying enable core dumping...\n");
220
+		if(prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)<=0) {
221
+			LM_DBG("core dumping is disabled now...\n");
222
+			if(prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)<0) {
223
+				LM_WARN("cannot re-enable core dumping!\n");
224
+			} else {
225
+				LM_DBG("core dumping has just been enabled...\n");
226
+				if (getrlimit(RLIMIT_CORE, &lim)<0){
227
+					LOG(L_CRIT, "cannot get the maximum core size: %s\n",
228
+							strerror(errno));
229
+					return -1;
230
+				} else {
231
+					DBG("current core file limit: %lu (max: %lu)\n",
232
+						(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
233
+				}
234
+			}
235
+		} else {
236
+			LM_DBG("core dumping is enabled now (%d)...\n",
237
+					prctl(PR_GET_DUMPABLE, 0, 0, 0, 0));
238
+		}
239
+	}
240
+#endif
241
+	return 0;
242
+}
205 243
 
206 244
 /** daemon init.
207 245
  *@param name - daemon name used for logging (used when opening syslog).
... ...
@@ -289,6 +327,10 @@ int daemonize(char*  name,  int status_wait)
289 327
 			exit(0);
290 328
 		}
291 329
 	}
330
+
331
+	if(enable_dumpable()<0)
332
+		goto error;
333
+
292 334
 	/* added by noh: create a pid file for the main process */
293 335
 	if (pid_file!=0){
294 336
 		
... ...
@@ -415,6 +457,10 @@ int do_suid()
415 457
 			goto error;
416 458
 		}
417 459
 	}
460
+
461
+	if(enable_dumpable()<0)
462
+		goto error;
463
+
418 464
 	return 0;
419 465
 error:
420 466
 	return -1;
... ...
@@ -477,7 +523,7 @@ error:
477 523
 
478 524
 
479 525
 /*! \brief enable core dumps */
480
-int set_core_dump(int enable, int size)
526
+int set_core_dump(int enable, long unsigned int size)
481 527
 {
482 528
 	struct rlimit lim;
483 529
 	struct rlimit newlim;
... ...
@@ -510,8 +556,11 @@ int set_core_dump(int enable, int size)
510 556
 						(unsigned long)lim.rlim_max);
511 557
 			}
512 558
 			goto error; /* it's an error we haven't got the size we wanted*/
559
+		}else{
560
+			newlim.rlim_cur=lim.rlim_cur;
561
+			newlim.rlim_max=lim.rlim_max;
562
+			goto done; /*nothing to do */
513 563
 		}
514
-		goto done; /*nothing to do */
515 564
 	}else{
516 565
 		/* disable */
517 566
 		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