[PATCH 4/7] staging: usbip: add option for usbipd to save its process id.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Introduce option "-P" / "--pid" to request that usbipd save its PID to
a file while running.

Signed-Off-By: Anthony Foiani <anthony.foiani@xxxxxxxxx>
---
 drivers/staging/usbip/userspace/src/usbipd.c | 38 +++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/usbip/userspace/src/usbipd.c b/drivers/staging/usbip/userspace/src/usbipd.c
index 1ecca9d..9c83148 100644
--- a/drivers/staging/usbip/userspace/src/usbipd.c
+++ b/drivers/staging/usbip/userspace/src/usbipd.c
@@ -50,6 +50,8 @@
 
 #define MAIN_LOOP_TIMEOUT 10
 
+#define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid"
+
 static const char usbip_version_string[] = PACKAGE_STRING;
 
 static const char usbipd_help_string[] =
@@ -60,6 +62,10 @@ static const char usbipd_help_string[] =
 	"	-d, --debug\n"
 	"		Print debugging information.\n"
 	"\n"
+	"	-PFILE, --pid FILE\n"
+	"		Write process id to FILE.\n"
+	"		If no FILE specified, use " DEFAULT_PID_FILE "\n"
+	"\n"
 	"	-h, --help\n"
 	"		Print this help.\n"
 	"\n"
@@ -522,11 +528,35 @@ static int do_standalone_mode(int daemonize)
 	return 0;
 }
 
+static void write_pid_file(const char *pid_file)
+{
+	if (pid_file) {
+		dbg("creating pid file %s", pid_file);
+		FILE *fp = fopen(pid_file, "w");
+		if (!fp) {
+			err("pid_file: %s: %d (%s)",
+			    pid_file, errno, strerror(errno));
+			return;
+		}
+		fprintf(fp, "%d\n", getpid());
+		fclose(fp);
+	}
+}
+
+static void remove_pid_file(const char *pid_file)
+{
+	if (pid_file) {
+		dbg("removing pid file %s", pid_file);
+		unlink(pid_file);
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	static const struct option longopts[] = {
 		{ "daemon",  no_argument, NULL, 'D' },
 		{ "debug",   no_argument, NULL, 'd' },
+		{ "pid",     optional_argument, NULL, 'P' },
 		{ "help",    no_argument, NULL, 'h' },
 		{ "version", no_argument, NULL, 'v' },
 		{ NULL,	     0,           NULL,  0  }
@@ -540,6 +570,7 @@ int main(int argc, char *argv[])
 
 	int daemonize = 0;
 	int opt, rc = -1;
+	const char *pid_file = NULL;
 
 	usbip_use_stderr = 1;
 	usbip_use_syslog = 0;
@@ -549,7 +580,7 @@ int main(int argc, char *argv[])
 
 	cmd = cmd_standalone_mode;
 	for (;;) {
-		opt = getopt_long(argc, argv, "Ddhv", longopts, NULL);
+		opt = getopt_long(argc, argv, "DdP::hv", longopts, NULL);
 
 		if (opt == -1)
 			break;
@@ -564,6 +595,9 @@ int main(int argc, char *argv[])
 		case 'h':
 			cmd = cmd_help;
 			break;
+		case 'P':
+			pid_file = optarg ? optarg : DEFAULT_PID_FILE;
+			break;
 		case 'v':
 			cmd = cmd_version;
 			break;
@@ -576,7 +610,9 @@ int main(int argc, char *argv[])
 
 	switch (cmd) {
 	case cmd_standalone_mode:
+		write_pid_file(pid_file);
 		rc = do_standalone_mode(daemonize);
+		remove_pid_file(pid_file);
 		break;
 	case cmd_version:
 		printf(PROGNAME " (%s)\n", usbip_version_string);
-- 
1.8.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux