VKERNEL Pidfile patch

Chris Turner c.turner at 199technologies.org
Sun Jun 17 17:49:22 PDT 2007


Simon 'corecode' Schubert wrote:
> 
> so, make this
> if (pidfile != NULL)
> 
> plus, do you think the vkernel should clean up the pidfile when shutting
> down?
> 

Okay.. as suggested.

Updated version:

  - globalized the pidfile char*
  - reworked writepid() to check for NULL
    (consistant with other arg-checking within the file)
  - added cleanpid() and added to halt hooks.

vkernel.7 diff is still against previous version, but as-cleaned,
should apply OK.

Thanks,

  - Chris

Index: share/man/man7/vkernel.7
===================================================================
RCS file: /var/local/apps/dcvs/src/share/man/man7/vkernel.7,v
retrieving revision 1.18
diff -u -p -r1.18 vkernel.7
--- share/man/man7/vkernel.7	14 Jun 2007 21:11:29 -0000	1.18
+++ share/man/man7/vkernel.7	17 Jun 2007 04:05:58 -0000
@@ -51,6 +51,7 @@ .Op Fl e Ar name Ns = Ns Li value : Ns A
 .Op Fl i Ar file
 .Op Fl I Ar interface Ns Op Ar :address1 Ns Oo Ar :address2 Oc Ns Oo Ar /netmask Oc
 .Op Fl m Ar size
+.Op Fl p Ar file
 .Op Fl r Ar file
 .Sh DESCRIPTION
 The
@@ -149,6 +150,9 @@ .Cm K , M ,
 and
 .Cm G
 are allowed.
+.It Fl p Ar file
+Specify a file in which to store the process ID. 
+A warning is issued if this file cannot be opened for writing.
 .It Fl r Ar file
 Specify a R/W disk image
 .Ar file
Index: sys/platform/vkernel/platform/init.c
===================================================================
RCS file: /var/local/apps/dcvs/src/sys/platform/vkernel/platform/init.c,v
retrieving revision 1.39
diff -u -p -r1.39 init.c
--- sys/platform/vkernel/platform/init.c	28 May 2007 05:26:29 -0000	1.39
+++ sys/platform/vkernel/platform/init.c	18 Jun 2007 00:33:30 -0000
@@ -80,6 +80,7 @@ 
 int DiskNum;
 struct vknetif_info NetifInfo[VKNETIF_MAX];
 int NetifNum;
+char *pid_file = NULL;
 vm_offset_t KvaStart;
 vm_offset_t KvaEnd;
 vm_offset_t KvaSize;
@@ -107,6 +108,8 @@ 
 static void init_vkernel(void);
 static void init_disk(char *diskExp[], int diskFileNum, enum vkdisk_type type); 
 static void init_netif(char *netifExp[], int netifFileNum);
+static void writepid( void );
+static void cleanpid( void );
 static void usage(const char *ctl);
 
 static int save_ac;
@@ -129,7 +132,7 @@ 	int cdFileNum = 0;
 	int c;
 	int i;
 	int n;
-
+	
 	save_ac = ac;
 	save_av = av;
 
@@ -138,7 +141,7 @@ 	 * Process options
 	 */
 	kernel_mem_readonly = 1;
 
-	while ((c = getopt(ac, av, "c:svm:r:e:i:I:U")) != -1) {
+	while ((c = getopt(ac, av, "c:svm:r:e:i:p:I:U")) != -1) {
 		switch(c) {
 		case 'e':
 			/*
@@ -200,12 +203,16 @@ 					break;
 				}
 			}
 			break;
+		case 'p':
+			pid_file = optarg;	
+			break;
 		case 'U':
 			kernel_mem_readonly = 0;
 			break;
 		}
 	}
 
+	writepid();
 	cpu_disable_intr();
 	init_sys_memory(memImageFile);
 	init_kern_memory();
@@ -1039,6 +1046,38 @@ }
 
 static
 void
+writepid( void )
+{
+	pid_t self = 0;
+	FILE *file = NULL;
+
+	if (pid_file == NULL)
+		return;
+
+	self = getpid();
+	file = fopen(pid_file, "w");
+
+	if (file != NULL) {
+		fprintf(file, "%ld\n", (long)self);
+		fclose(file);
+	}
+	else {
+		perror("Warning: couldn't open pidfile");
+	}
+}
+
+static
+void
+cleanpid( void ) 
+{
+	if (pid_file == NULL)
+		return;
+	if ( unlink(pid_file) != 0 )
+		perror("Warning: couldn't remove pidfile");
+}
+
+static
+void
 usage(const char *ctl)
 {
 	
@@ -1049,6 +1088,7 @@ 
 {
 	kprintf("cpu reset, rebooting vkernel\n");
 	closefrom(3);
+	cleanpid();
 	execv(save_av[0], save_av);
 }
 
@@ -1056,5 +1096,6 @@ 
 cpu_halt(void)
 {
 	kprintf("cpu halt, exiting vkernel\n");
+	cleanpid();
 	exit(0);
 }




More information about the Submit mailing list