This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC][PATCH 2/5][flight-recorder] Output message to syslog


This patch outputs messages of the stpd to syslog when the stpd
runs in background. In the background mode, the stpd is seperated
from console. Thus it has to send its messages (errors, wornings)
into syslog.

-- 
Masami HIRAMATSU
2nd Research Dept.
Hitachi, Ltd., Systems Development Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com

 runtime/stpd/librelay.c |  100 ++++++++++++++++++++++++------------------------
 runtime/stpd/librelay.h |    7 ++-
 runtime/stpd/stpd.c     |   56 ++++++++++++++++++++++++++
 3 files changed, 111 insertions(+), 52 deletions(-)
Index: src/runtime/stpd/librelay.c
===================================================================
--- src.orig/runtime/stpd/librelay.c	2006-08-25 15:02:14.000000000 +0900
+++ src/runtime/stpd/librelay.c	2006-08-25 19:11:54.000000000 +0900
@@ -142,12 +142,12 @@
 	if (transport_mode != STP_TRANSPORT_RELAYFS)
 		return;

-	printf("summary:\n");
+	print_out("summary:\n");
 	for (i = 0; i < ncpus; i++) {
-		printf("cpu %u:\n", i);
-		printf("    %u sub-buffers processed\n",
+		print_out("cpu %u:\n", i);
+		print_out("    %u sub-buffers processed\n",
 		       status[i].info.consumed);
-		printf("    %u max backlog\n", status[i].max_backlog);
+		print_out("    %u max backlog\n", status[i].max_backlog);
 	}
 }

@@ -197,7 +197,7 @@
 	sprintf(tmp, "%s%d", relay_filebase, cpu);
 	relay_file[cpu] = open(tmp, O_RDONLY | O_NONBLOCK);
 	if (relay_file[cpu] < 0) {
-		fprintf(stderr, "ERROR: couldn't open relayfs file %s: errcode = %s\n", tmp, strerror(errno));
+		print_err ("ERROR: couldn't open relayfs file %s: errcode = %s\n", tmp, strerror(errno));
 		return -1;
 	}

@@ -205,13 +205,13 @@
 	dbug("Opening %s.\n", tmp);
 	proc_file[cpu] = open(tmp, O_RDWR | O_NONBLOCK);
 	if (proc_file[cpu] < 0) {
-		fprintf(stderr, "ERROR: couldn't open proc file %s: errcode = %s\n", tmp, strerror(errno));
+		print_err ("ERROR: couldn't open proc file %s: errcode = %s\n", tmp, strerror(errno));
 		return -1;
 	}

 	sprintf(tmp, "%s%d", percpu_tmpfilebase, cpu);	
 	if((percpu_tmpfile[cpu] = fopen(tmp, "w+")) == NULL) {
-		fprintf(stderr, "ERROR: Couldn't open output file %s: errcode = %s\n", tmp, strerror(errno));
+		print_err ("ERROR: Couldn't open output file %s: errcode = %s\n", tmp, strerror(errno));
 		close(relay_file[cpu]);
 		return -1;
 	}
@@ -222,7 +222,7 @@
 				 0);
 	if(relay_buffer[cpu] == MAP_FAILED)
 	{
-		fprintf(stderr, "ERROR: couldn't mmap relay file, total_bufsize (%d) = subbuf_size (%d) * n_subbufs(%d), error = %s \n", (int)total_bufsize, (int)params.subbuf_size,
(int)params.n_subbufs, strerror(errno));
+		print_err ("ERROR: couldn't mmap relay file, total_bufsize (%d) = subbuf_size (%d) * n_subbufs(%d), error = %s \n", (int)total_bufsize, (int)params.subbuf_size,
(int)params.n_subbufs, strerror(errno));
 		close(relay_file[cpu]);
 		fclose(percpu_tmpfile[cpu]);
 		return -1;
@@ -244,7 +244,7 @@
 	for (i = 0; i < ncpus; i++) {
 		sprintf(tmp, "%s%d", percpu_tmpfilebase, i);
 		if (unlink(tmp) < 0) {
-			fprintf(stderr, "ERROR: couldn't unlink percpu file %s: errcode = %s\n", tmp, strerror(errno));
+			print_err ("ERROR: couldn't unlink percpu file %s: errcode = %s\n", tmp, strerror(errno));
 			return -1;
 		}
 	}
@@ -266,7 +266,7 @@
 			killed++;
 	}
 	if (killed != n)
-		fprintf(stderr, "WARNING: couldn't kill all per-cpu threads:  %d killed, %d total\n", killed, n);
+		print_err ("WARNING: couldn't kill all per-cpu threads:  %d killed, %d total\n", killed, n);

 	return killed;
 }
@@ -294,7 +294,7 @@
 		len = (params.subbuf_size - sizeof(padding)) - padding;
 		if (len) {
 			if (fwrite_unlocked (subbuf_ptr, len, 1, percpu_tmpfile[cpu]) != 1) {
-				fprintf(stderr, "ERROR: couldn't write to output file for cpu %d, exiting: errcode = %d: %s\n", cpu, errno, strerror(errno));
+				print_err ("ERROR: couldn't write to output file for cpu %d, exiting: errcode = %d: %s\n", cpu, errno, strerror(errno));
 				exit(1);
 			}
 		}
@@ -322,12 +322,12 @@
 		rc = poll(&pollfd, 1, -1);
 		if (rc < 0) {
 			if (errno != EINTR) {
-				fprintf(stderr, "ERROR: poll error: %s\n",
-					strerror(errno));
+				print_err ("ERROR: poll error: %s\n",
+					  strerror(errno));
 				exit(1);
 			}
-			fprintf(stderr, "WARNING: poll warning: %s\n",
-				strerror(errno));
+			print_err ("WARNING: poll warning: %s\n",
+				  strerror(errno));
 			rc = 0;
 		}

@@ -345,7 +345,7 @@
 			consumed_info.cpu = cpu;
 			consumed_info.consumed = subbufs_consumed;
 			if (write (proc_file[cpu], &consumed_info, sizeof(struct consumed_info)) < 0)
-				fprintf(stderr,"WARNING: writing consumed info failed.\n");
+				print_err ("WARNING: writing consumed info failed.\n");
 		}
 		pthread_mutex_lock(&processing_mutex);
 		processing--;
@@ -370,7 +370,7 @@
 			consumed_info.cpu = cpu;
 			consumed_info.consumed = subbufs_consumed;
 			if (write (proc_file[cpu], &consumed_info, sizeof(struct consumed_info)) < 0)
-				fprintf(stderr,"WARNING: writing consumed info failed.\n");
+				print_err ("WARNING: writing consumed info failed.\n");
 		}
 	}
 }
@@ -387,20 +387,20 @@

 	for (i = 0; i < ncpus; i++) {
 		if (open_relayfs_files(i, params.relay_filebase) < 0) {
-			fprintf(stderr, "ERROR: couldn't open relayfs files, cpu = %d\n", i);
+			print_err ("ERROR: couldn't open relayfs files, cpu = %d\n", i);
 			goto err;
 		}
 		/* create a thread for each per-cpu buffer */
 		if (pthread_create(&reader[i], NULL, reader_thread, (void *)(long)i) < 0) {
 			close_relayfs_files(i);
-			fprintf(stderr, "ERROR: Couldn't create reader thread, cpu = %d\n", i);
+			print_err ("ERROR: Couldn't create reader thread, cpu = %d\n", i);
 			goto err;
 		}
 	}

         if (print_totals && verbose)
-          printf("Using channel with %u sub-buffers of size %u.\n",
-                 params.n_subbufs, params.subbuf_size);
+		print_out("Using channel with %u sub-buffers of size %u.\n",
+			  params.n_subbufs, params.subbuf_size);

 	return 0;
 err:
@@ -422,22 +422,22 @@

 	dbug ("execing target_cmd %s\n", target_cmd);
 	if ((pid = fork()) < 0) {
-		perror ("fork");
+		perror_err ("fork");
 		exit(-1);
 	} else if (pid == 0) {
 		int signum;

 		if (setregid(cmd_gid, cmd_gid) < 0) {
-			perror("setregid");
+			perror_err ("setregid");
 		}
 		if (setreuid(cmd_uid, cmd_uid) < 0) {
-			perror("setreuid");
+			perror_err ("setreuid");
 		}
 		/* wait here until signaled */
 		sigwait(&usrset, &signum);

 		if (execl("/bin/sh", "sh", "-c", target_cmd, NULL) < 0)
-			perror(target_cmd);
+			perror_err (target_cmd);
 		_exit(-1);
 	}
 	target_pid = pid;
@@ -449,16 +449,16 @@

 	dbug ("system %s\n", cmd);
 	if ((pid = fork()) < 0) {
-		perror ("fork");
+		perror_err ("fork");
 	} else if (pid == 0) {
 		if (setregid(cmd_gid, cmd_gid) < 0) {
-			perror("setregid");
+			perror_err ("setregid");
 		}
 		if (setreuid(cmd_uid, cmd_uid) < 0) {
-			perror("setreuid");
+			perror_err ("setreuid");
 		}
 		if (execl("/bin/sh", "sh", "-c", cmd, NULL) < 0)
-			perror(cmd);
+			perror_err (cmd);
 		_exit(-1);
 	}
 }
@@ -491,18 +491,18 @@
         /* modoptions[3...N] set by command line parser. */

 	if ((pid = fork()) < 0) {
-		perror ("fork");
+		perror_err ("fork");
 		exit(-1);
 	} else if (pid == 0) {
 		if (execvp("/sbin/insmod",  modoptions) < 0)
 			_exit(-1);
 	}
 	if (waitpid(pid, &rstatus, 0) < 0) {
-		perror("waitpid");
+		perror_err ("waitpid");
 		exit(-1);
 	}
 	if (WIFEXITED(rstatus) && WEXITSTATUS(rstatus)) {
-		fprintf(stderr, "ERROR, couldn't insmod probe module %s\n", modpath);
+		print_err ("ERROR, couldn't insmod probe module %s\n", modpath);
 		return -1;
 	}

@@ -518,7 +518,7 @@
 	dbug("Opening %s\n", buf);
 	control_channel = open(buf, O_RDWR);
 	if (control_channel < 0) {
-		fprintf(stderr, "ERROR: couldn't open control channel %s: errcode = %s\n", buf, strerror(errno));
+		print_err ("ERROR: couldn't open control channel %s: errcode = %s\n", buf, strerror(errno));
 		return -1;
 	}

@@ -532,7 +532,7 @@
 	ti.n_subbufs = 0;
 	ti.target = target_pid;
 	if (send_request(STP_TRANSPORT_INFO, &ti, sizeof(ti)) < 0) {
-		fprintf(stderr, "stpd failed because TRANSPORT_INFO returned an error.\n");
+		print_err ("stpd failed because TRANSPORT_INFO returned an error.\n");
 		if (target_cmd)
 			kill (target_pid, SIGKILL);
 		close(control_channel);
@@ -562,7 +562,7 @@
 		sprintf (tmp, "%s%d", percpu_tmpfilebase, i);
 		fp[i] = fopen (tmp, "r");
 		if (!fp[i]) {
-			fprintf (stderr, "error opening file %s.\n", tmp);
+			print_err ("ERROR: opening file %s.\n", tmp);
 			return -1;
 		}
 		if (fread (buf, TIMESTAMP_SIZE, 1, fp[i]))
@@ -576,8 +576,8 @@

 	ofp = fopen (outfile_name, "w");
 	if (!ofp) {
-		fprintf (stderr, "ERROR: couldn't open output file %s: errcode = %s\n",
-			 outfile_name, strerror(errno));
+		print_err ("ERROR: couldn't open output file %s: errcode = %s\n",
+			  outfile_name, strerror(errno));
 		return -1;
 	}

@@ -618,7 +618,7 @@
 		fclose (fp[i]);
 	fclose (ofp);
 	if (dropped)
-		fprintf (stderr, "Sequence had %d drops.\n", dropped);
+		print_err ("WARNING: Sequence had %d drops.\n", dropped);
 	return 0;
 }

@@ -636,7 +636,7 @@
 	/* what about child processes? we will wait for them here. */
 	err = waitpid(-1, NULL, WNOHANG);
 	if (err >= 0)
-		fprintf(stderr,"\nWaititing for processes to exit\n");
+		print_err ("\nWaititing for processes to exit\n");
 	while(wait(NULL) > 0);

 	if (transport_mode == STP_TRANSPORT_RELAYFS) {
@@ -671,8 +671,8 @@
 	if (!closed) {
 		snprintf(tmpbuf, sizeof(tmpbuf), "/sbin/rmmod -w %s", modname);
 		if (system(tmpbuf)) {
-			fprintf(stderr, "ERROR: couldn't rmmod probe module %s.  No output will be written.\n",
-				modname);
+			print_err ("ERROR: couldn't rmmod probe module %s.  No output will be written.\n",
+				  modname);
 			exit(1);
 		}
 	}
@@ -732,8 +732,8 @@
 	while (1) { /* handle messages from control channel */
 		nb = read(control_channel, recvbuf, sizeof(recvbuf));
 		if (nb <= 0) {
-			perror("recv");
-			fprintf(stderr, "WARNING: unexpected EOF. nb=%d\n", nb);
+			perror_err ("recv");
+			print_err ("WARNING: unexpected EOF. nb=%d\n", nb);
 			continue;
 		}

@@ -741,7 +741,7 @@
 		data = (void *)(recvbuf + sizeof(int));

 		if (!transport_mode && type != STP_TRANSPORT_INFO && type != STP_EXIT) {
-			fprintf(stderr, "WARNING: invalid stp command: no transport\n");
+			print_err ("WARNING: invalid stp command: no transport\n");
 			continue;
 		}

@@ -754,18 +754,18 @@
 			params.n_subbufs = info->n_subbufs;
 #ifdef DEBUG
 			if (transport_mode == STP_TRANSPORT_RELAYFS)
-				fprintf (stderr, "TRANSPORT_INFO recvd: RELAYFS %d bufs of %d bytes.\n",
+				print_err ("TRANSPORT_INFO recvd: RELAYFS %d bufs of %d bytes.\n",
 					 params.n_subbufs,
 					 params.subbuf_size);
 			else
-				fprintf (stderr, "TRANSPORT_INFO recvd: PROC with %d Mbyte buffers.\n",
+				print_err ("TRANSPORT_INFO recvd: PROC with %d Mbyte buffers.\n",
 					 info->buf_size);
 #endif
 			if (!streaming()) {
 				rc = init_relayfs();
 				if (rc < 0) {
 					close(control_channel);
-					fprintf(stderr, "ERROR: couldn't init relayfs, exiting\n");
+					print_err ("ERROR: couldn't init relayfs, exiting\n");
 					/* FIXME. Need to cleanup properly */
 					exit(1);
 				}
@@ -774,7 +774,7 @@
 					outfile_name = DEFAULT_RELAYFS_OUTFILE_NAME;
 				ofp = fopen (outfile_name, "w");
 				if (!ofp) {
-					fprintf (stderr, "ERROR: couldn't open output file %s: errcode = %s\n",
+					print_err ("ERROR: couldn't open output file %s: errcode = %s\n",
 						 outfile_name, strerror(errno));
 					/* FIXME. Need to cleanup properly */
 					exit(1);
@@ -788,7 +788,7 @@
 			fwrite_unlocked(data, nb - sizeof(int), 1, ofp);
 			break;
 		case STP_OOB_DATA:
-			fputs ((char *)data, stderr);
+			print_err ("%s\n", (char *)data);
 			break;
 		case STP_EXIT:
 		{
@@ -816,7 +816,7 @@
 			break;
 		}
 		default:
-			fprintf(stderr, "WARNING: ignored message of type %d\n", (type));
+			print_err ("WARNING: ignored message of type %d\n", (type));
 		}
 	}
 	fclose(ofp);
Index: src/runtime/stpd/librelay.h
===================================================================
--- src.orig/runtime/stpd/librelay.h	2005-08-25 01:27:36.000000000 +0900
+++ src/runtime/stpd/librelay.h	2006-08-25 15:02:17.000000000 +0900
@@ -1,7 +1,12 @@
 #include "../transport/transport_msgs.h"

+/* message output routine */
+extern void (*print_err)(char *fmt, ...);
+extern void (*print_out)(char *fmt, ...);
+extern void (*perror_err)(const char *s);
+
 #ifdef DEBUG
-#define dbug(args...) {fprintf(stderr,"%s:%d ",__FUNCTION__, __LINE__); fprintf(stderr,args); }
+#define dbug(args...) {print_err("%s:%d ",__FUNCTION__, __LINE__); print_err(args); }
 #else
 #define dbug(args...) ;
 #endif /* DEBUG */
Index: src/runtime/stpd/stpd.c
===================================================================
--- src.orig/runtime/stpd/stpd.c	2006-08-25 15:02:14.000000000 +0900
+++ src/runtime/stpd/stpd.c	2006-08-25 19:11:54.000000000 +0900
@@ -29,6 +29,11 @@
 #include <pwd.h>
 #include "librelay.h"

+#include <syslog.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+
 extern char *optarg;
 extern int optopt;
 extern int optind;
@@ -52,6 +57,51 @@
 uid_t cmd_uid;
 gid_t cmd_gid;

+/* message output routine (implements) */
+static void _print_syserr(char * fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	vsyslog(LOG_ERR, fmt, ap);
+	va_end(ap);
+}
+
+static void _print_sysnotice(char * fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	vsyslog(LOG_NOTICE, fmt, ap);
+	va_end(ap);
+}
+
+static void _perror_syserr(const char *s)
+{
+	char buf[128];
+	strerror_r(errno,buf,128);
+	syslog(LOG_ERR, "%s: %s\n", s, buf);
+}
+
+static void _print_stderr(char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+}
+
+static void _print_stdout(char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	vfprintf(stdout, fmt, ap);
+	va_end(ap);
+}
+
+void (*print_err)(char *fmt, ...) = _print_stderr;
+void (*print_out)(char *fmt, ...) = _print_stdout;
+void (*perror_err)(const char *s) = perror;
+
+
  /* relayfs base file name */
 static char stpd_filebase[1024];
 #define RELAYFS_MAGIC			0xF0B4A981
@@ -246,10 +296,14 @@
 			perror("Couldn't be a daemon.");
 			exit(1);
 		}
+		openlog("stpd",LOG_PID,LOG_DAEMON);
+		print_err = _print_syserr;
+		print_out = _print_sysnotice;
+		perror_err = _perror_syserr;
 	}

 	if (stp_main_loop()) {
-		fprintf(stderr,"Couldn't enter main loop. Exiting.\n");
+		print_err("Couldn't enter main loop. Exiting.\n");
 		exit(1);
 	}





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]