All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [2/4] put xen console message into syslog but xm dmesg is not affected
@ 2006-07-07  8:21 MINAI Katsuhito
  2006-07-10 15:02 ` Keir Fraser
  0 siblings, 1 reply; 6+ messages in thread
From: MINAI Katsuhito @ 2006-07-07  8:21 UTC (permalink / raw)
  To: Akio Takebe; +Cc: Hans-Christian Armingeon, xen-devel, Mark Williamson

[-- Attachment #1: Type: text/plain, Size: 105 bytes --]

[2/4] add xenlogd daemon for xen console logging

Signed-off-by: Katsuhito Minai <minai@jp.fujitsu.com>


[-- Attachment #2: 2.xenlogd.patch --]
[-- Type: application/octet-stream, Size: 6946 bytes --]

diff -r 8e55c5c11475 tools/misc/Makefile
--- a/tools/misc/Makefile	Wed Jul 05 18:48:41 2006 +0100
+++ b/tools/misc/Makefile	Fri Jul 07 16:40:48 2006 +0900
@@ -13,10 +13,10 @@ CFLAGS   += $(INCLUDES)
 
 HDRS     = $(wildcard *.h)
 
-TARGETS  = xenperf xc_shadow
+TARGETS  = xenperf xc_shadow xenlogd
 
 INSTALL_BIN  = $(TARGETS) xencons
-INSTALL_SBIN = netfix xm xen-bugtool xend xenperf
+INSTALL_SBIN = netfix xm xen-bugtool xend xenperf xenlogd
 
 .PHONY: all
 all: build
diff -r 8e55c5c11475 tools/misc/xenlogd.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/misc/xenlogd.c	Fri Jul 07 16:40:48 2006 +0900
@@ -0,0 +1,299 @@
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <syslog.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+
+#define XCDSIZE (32*1024)
+#define XENHEAD "(XEN) "
+#define PIDFILE "/var/run/xenlogd.pid"
+
+extern int xc_interface_open(void);
+extern int xc_readconsolering(int, char **, int *, int);
+static char *cmdname;
+
+void
+quit(int xc, char *fmt, ...)
+{
+  va_list ap;
+  
+  va_start(ap, fmt);
+  vsyslog(LOG_KERN|LOG_ALERT, fmt, ap);
+  va_end(ap);
+  exit(xc);
+}
+
+int
+putsyslog(char *bp, int size)
+{
+  static char *prsvp = NULL;
+  static int prsvc = 0;
+  char *endp;
+  int offset;
+
+  if ((prsvp == NULL) && ((prsvp = malloc(XCDSIZE)) == NULL)) {
+    quit(1, "allocate buffer failed");
+  }
+
+  bp[size] = '\0';
+
+  for (endp = bp + size; bp < endp; bp++) {
+    if (*bp == '\n') {
+      prsvp[prsvc]  = '\0';
+      if ((strncmp(prsvp, XENHEAD, strlen(XENHEAD)) == 0)) {
+        offset = strlen(XENHEAD);
+      } else {
+        offset = 0;
+      }
+      syslog(LOG_KERN|LOG_ALERT, "%s", prsvp + offset);
+      prsvc = 0;
+    } else {
+      prsvp[prsvc] = *bp;
+      prsvc++;
+    }
+  }
+  return(prsvc);
+}
+
+static void child_exit(int sig)
+{
+  while (waitpid(-1, NULL, WNOHANG) > 0);
+}
+
+static void term_daemon(int sig)
+{
+  unlink(PIDFILE);
+  quit(0, "logging ended normally.");
+}
+
+void
+daemonize(void)
+{
+  pid_t pid;
+  int fd;
+  int len;
+  int i;
+  int pfd[2];
+  struct sigaction siga;
+  char buf[1024];
+  
+  if (getppid() == 1) {
+    return;
+  }
+
+  signal(SIGPIPE, SIG_IGN);
+
+  if (pipe(pfd) < 0) {
+    quit(1, "open pipe failed");
+  }
+
+  if ((pid = fork()) > 0) {
+    exit(0);
+  } 
+  if (pid < 0) {
+    quit(1, "fork failed: errno = %d", errno);
+  }
+  
+  setsid();
+  
+  if ((pid = fork()) > 0) {
+    exit(0);
+  } 
+  if (pid < 0) {
+    quit(1, "fork failed: errno = %d", errno);
+  }
+  
+  if ((fd = open("/dev/null",O_RDWR)) == -1) {
+    quit(1, "open /dev/null failed: errno = %d", errno);
+  }
+  
+  for (i = 0; i <= 2; i++) {
+    close(i);
+    dup2(fd, i);
+  }
+  
+  close(fd);
+  umask(027);
+
+  if (chdir("/") < 0) {
+    quit(1, "change to root dir failed: errno = %d", errno);
+  }
+
+  pid = getpid();
+  snprintf(buf, sizeof(buf), "%s.%d", PIDFILE, pid);
+  
+  fd = open(buf, O_RDWR | O_CREAT | O_EXCL);
+  if (fd < 0) {
+    quit(1, "open %s failed: errno = %d", buf, errno);
+  }
+
+  if (link(buf, PIDFILE) < 0) {
+    unlink(buf);
+    quit(1, "other logging daemon may keep alive");
+  }
+  unlink(buf);
+  
+  len = sprintf(buf, "%d\n", pid);
+  if (write(fd, buf, len) < 0) {
+    quit(1, "write %s failed: errno = %d", PIDFILE, errno);
+  }
+
+  siga.sa_handler = term_daemon;
+  sigemptyset(&siga.sa_mask);
+  sigaddset(&siga.sa_mask, SIGTERM);
+  siga.sa_flags = 0;
+
+  signal(SIGCHLD, child_exit);
+  signal(SIGTSTP, SIG_IGN);
+  signal(SIGTTOU, SIG_IGN);
+  signal(SIGTTIN, SIG_IGN);
+  sigaction(SIGTERM, &siga, NULL);
+
+  close(pfd[1]);
+  read(pfd[0], buf, sizeof(buf));
+  close(pfd[0]);
+}
+
+void
+kill_daemon(void)
+{
+  int fd, pid, len;
+  char buf[1024];
+  char *comm;
+
+  fd = open(PIDFILE, O_RDWR);
+  if (fd < 0) {
+    if (errno == ENOENT) {
+      fprintf(stderr,"%s: no other daemon exist\n", cmdname);
+    } else {
+      perror("open error: ");
+    }
+    goto delpidfile;
+  }
+  len = read(fd, buf, sizeof(buf));
+  if (len < 0) {
+    perror("read error: ");
+    goto delpidfile;
+  }
+  close(fd);
+  buf[len] = '\0';
+  pid = atoi(buf);
+  
+  if (pid) {
+    snprintf(buf, sizeof(buf), "/proc/%d/stat", pid);
+    if ((fd = open(buf, O_RDONLY)) >= 0) {
+      len = read(fd, buf, sizeof(buf));
+      strtok(buf, " ");
+      if (((comm = strtok(NULL, " ")) != NULL) 
+          && (strcmp(comm, "(xenlogd)") == 0)) {
+        if (kill(pid, SIGTERM) < 0) {
+          perror("kill error: ");
+          goto delpidfile;
+        }
+        while (kill(pid, SIGTERM) == 0);
+        if (errno != ESRCH) {
+          perror("kill error: ");
+        }
+      }
+    }
+  }
+
+ delpidfile:
+  if ((fd = open(PIDFILE, O_RDWR)) >= 0) {
+    close(fd);
+    unlink(PIDFILE);
+    fprintf(stderr, "%s: lockfile existed but no daemon alive. it was deleted.\n", cmdname);
+  }
+}
+
+void usage(void)
+{
+  fprintf(stderr, "usage: %s [-h] [-n] [-k] [-r]\n", cmdname);
+  fprintf(stderr, "       -h: print help\n");
+  fprintf(stderr, "       -n: no daemonize\n");
+  fprintf(stderr, "       -k: kill alive logging daemon\n");
+  fprintf(stderr, "       -r: restart logging daemon after kill alive one\n");
+  exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+  int fd;
+  int rc;
+  int bufsize;
+  int opt;
+  char *bufaddr;
+  int no_daemonize = 0;
+  int kill_d = 0;
+  int restart_d = 0;
+
+  cmdname = argv[0];
+
+  while ((opt = getopt(argc, argv, "hnkr")) != -1) {
+    switch (opt) {
+    case 'h':
+    case '?':
+    default:
+      usage();
+      break;
+    case 'n':
+      no_daemonize = 1;
+      break;
+    case 'r':
+      restart_d = 1;
+      /* fall through */
+    case 'k':
+      kill_d = 1;
+      break;
+    }
+  }
+
+  openlog(XENHEAD, LOG_NDELAY|LOG_CONS, LOG_KERN);
+
+  fd = xc_interface_open();
+  if (fd < 0) {
+    quit(1, "failed to open xen interface");
+  }
+
+  if ((bufaddr = malloc(XCDSIZE)) ==NULL) {
+    quit(1, "allocate buffer failed");
+  }
+
+  if (kill_d) {
+    kill_daemon();
+    if (!restart_d) {
+      exit(0);
+    }
+  }
+
+  if (!no_daemonize) {
+    daemonize();
+  }
+  
+  syslog(LOG_KERN|LOG_ALERT, "logging has been started.");
+
+  for (;;) {
+    bufsize = XCDSIZE;
+
+    rc = xc_readconsolering(fd, &bufaddr, &bufsize, -1);
+    if (rc < 0) {
+      quit(1, "error: read console ring: %d: errno %d", rc, errno);
+    }
+
+    if (bufsize) {
+      if (putsyslog(bufaddr, bufsize)) {
+	continue;
+      }
+    }
+    sleep(1);
+  }
+}

[-- Attachment #3: Type: text/plain, Size: 138 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [2/4] put xen console message into syslog but xm dmesg is not affected
  2006-07-07  8:21 [PATCH] [2/4] put xen console message into syslog but xm dmesg is not affected MINAI Katsuhito
@ 2006-07-10 15:02 ` Keir Fraser
  2006-07-11  6:41   ` [PATCH] [2/4] put xen console message into syslog butxm " MINAI Katsuhito
  2006-07-11 12:30   ` [PATCH] [2/4] put xen console message into syslog but?xm " Horms
  0 siblings, 2 replies; 6+ messages in thread
From: Keir Fraser @ 2006-07-10 15:02 UTC (permalink / raw)
  To: MINAI Katsuhito
  Cc: Mark Williamson, Hans-Christian Armingeon, xen-devel, Akio Takebe


On 7 Jul 2006, at 09:21, MINAI Katsuhito wrote:

> [2/4] add xenlogd daemon for xen console logging
>
> Signed-off-by: Katsuhito Minai <minai@jp.fujitsu.com>

Let's not add extra code styles to the tree: either format your code in 
Xen style (4-space soft tabs; braces on their own lines) or in Linux 
style (hard tabs; K&R style).

  -- Keir

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [2/4] put xen console message into syslog butxm dmesg is not affected
  2006-07-10 15:02 ` Keir Fraser
@ 2006-07-11  6:41   ` MINAI Katsuhito
  2006-07-11 12:30   ` [PATCH] [2/4] put xen console message into syslog but?xm " Horms
  1 sibling, 0 replies; 6+ messages in thread
From: MINAI Katsuhito @ 2006-07-11  6:41 UTC (permalink / raw)
  To: Keir Fraser
  Cc: Akio Takebe, Hans-Christian Armingeon, xen-devel, Mark Williamson

Hi keir

I am sorry that the code style of my patch is scattering.

After the discussion with you concerning xc_readconsolering()
reached a conclusion, I'd like to re-contribute the patch
to match the code style of Xen.


Best regards,
Katsuhito Minai


On 11 Jul 2006, at 12:02 am, Keir Fraser said:
>
> On 7 Jul 2006, at 09:21, MINAI Katsuhito wrote:
>
>> [2/4] add xenlogd daemon for xen console logging
>>
>> Signed-off-by: Katsuhito Minai <minai@jp.fujitsu.com>
>
> Let's not add extra code styles to the tree: either format your code in
> Xen style (4-space soft tabs; braces on their own lines) or in Linux
> style (hard tabs; K&R style).
>
>   -- Keir
>
>
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xensource.com
> http://lists.xensource.com/xen-devel
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [2/4] put xen console message into syslog but?xm dmesg is not affected
  2006-07-10 15:02 ` Keir Fraser
  2006-07-11  6:41   ` [PATCH] [2/4] put xen console message into syslog butxm " MINAI Katsuhito
@ 2006-07-11 12:30   ` Horms
  2006-07-11 15:13     ` Keir Fraser
  1 sibling, 1 reply; 6+ messages in thread
From: Horms @ 2006-07-11 12:30 UTC (permalink / raw)
  To: Keir Fraser; +Cc: xen-devel, Katsuhito Minai

On Mon, 10 Jul 2006 16:02:16 +0100, Keir Fraser wrote:
> 
> On 7 Jul 2006, at 09:21, MINAI Katsuhito wrote:
> 
>> [2/4] add xenlogd daemon for xen console logging
>>
>> Signed-off-by: Katsuhito Minai <minai@jp.fujitsu.com>
> 
> Let's not add extra code styles to the tree: either format your code in 
> Xen style (4-space soft tabs; braces on their own lines) or in Linux 
> style (hard tabs; K&R style).

Two is already one too many IMHO :-)

-- 
Horms                                           
  H: http://www.vergenet.net/~horms/
  W: http://www.valinux.co.jp/en/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [2/4] put xen console message into syslog but?xm dmesg is not affected
  2006-07-11 12:30   ` [PATCH] [2/4] put xen console message into syslog but?xm " Horms
@ 2006-07-11 15:13     ` Keir Fraser
  2006-07-12  0:42       ` Horms
  0 siblings, 1 reply; 6+ messages in thread
From: Keir Fraser @ 2006-07-11 15:13 UTC (permalink / raw)
  To: Horms; +Cc: xen-devel, Katsuhito Minai


On 11 Jul 2006, at 13:30, Horms wrote:

>> Let's not add extra code styles to the tree: either format your code 
>> in
>> Xen style (4-space soft tabs; braces on their own lines) or in Linux
>> style (hard tabs; K&R style).
>
> Two is already one too many IMHO :-)

As long as each discrete 'tool' or library is self-consistent in which 
it picks I don't really see a problem. In fact I wouldn't be too 
bothered about having more than two styles to choose from -- but 
two-space soft tabs are not an option as far as I'm concerned, which is 
the main reason I commented. There are more important issues than petty 
battles over coding standards.

  -- Keir

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [2/4] put xen console message into syslog but?xm dmesg is not affected
  2006-07-11 15:13     ` Keir Fraser
@ 2006-07-12  0:42       ` Horms
  0 siblings, 0 replies; 6+ messages in thread
From: Horms @ 2006-07-12  0:42 UTC (permalink / raw)
  To: Keir Fraser; +Cc: xen-devel, Katsuhito Minai

On Tue, Jul 11, 2006 at 04:13:01PM +0100, Keir Fraser wrote:
> 
> On 11 Jul 2006, at 13:30, Horms wrote:
> 
> >>Let's not add extra code styles to the tree: either format your code 
> >>in
> >>Xen style (4-space soft tabs; braces on their own lines) or in Linux
> >>style (hard tabs; K&R style).
> >
> >Two is already one too many IMHO :-)
> 
> As long as each discrete 'tool' or library is self-consistent in which 
> it picks I don't really see a problem. In fact I wouldn't be too 
> bothered about having more than two styles to choose from -- but 
> two-space soft tabs are not an option as far as I'm concerned, which is 
> the main reason I commented. There are more important issues than petty 
> battles over coding standards.

Sorry, I was niggling a bit. I don't mind too much either, as long
as its clear which directories (or files) use which standard. Though
it is a bit of a mind-warp when you have files from xen/ and some kernel
files open at the same time. I had to get quite good at teaching my
editor what to do for me.

-- 
Horms                                           
  H: http://www.vergenet.net/~horms/
  W: http://www.valinux.co.jp/en/

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-07-12  0:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-07-07  8:21 [PATCH] [2/4] put xen console message into syslog but xm dmesg is not affected MINAI Katsuhito
2006-07-10 15:02 ` Keir Fraser
2006-07-11  6:41   ` [PATCH] [2/4] put xen console message into syslog butxm " MINAI Katsuhito
2006-07-11 12:30   ` [PATCH] [2/4] put xen console message into syslog but?xm " Horms
2006-07-11 15:13     ` Keir Fraser
2006-07-12  0:42       ` Horms

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.