diff --git a/man/man8/swtpm.8 b/man/man8/swtpm.8 index 19f30f5c..830c80a0 100644 --- a/man/man8/swtpm.8 +++ b/man/man8/swtpm.8 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "swtpm 8" -.TH swtpm 8 "2018-04-03" "swtpm" "" +.TH swtpm 8 "2018-04-05" "swtpm" "" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -333,10 +333,11 @@ has been read. This variant of the migration key parameter allows to provide a passphrase in a file. A maximum of 32 bytes are read from the file and a key is derived from it using a \&\s-1SHA512\s0 hash. Currently only 128 bit keys are supported. -.IP "\fB\-\-pid file=\fR" 4 -.IX Item "--pid file=" +.IP "\fB\-\-pid file=|fd=\fR" 4 +.IX Item "--pid file=|fd=" This options allows to set the name of file where the process \s-1ID \s0(pid) of the \s-1TPM\s0 -will be written into. +will be written into. It is also possible to pass a file descriptor to a file that +has been opened for writing. .IP "\fB\-r|\-\-runas \fR" 4 .IX Item "-r|--runas " Switch to the given user. This option can only be used when swtpm is started as root. diff --git a/man/man8/swtpm.pod b/man/man8/swtpm.pod index 269d5775..f29ec700 100644 --- a/man/man8/swtpm.pod +++ b/man/man8/swtpm.pod @@ -244,10 +244,11 @@ This variant of the migration key parameter allows to provide a passphrase in a A maximum of 32 bytes are read from the file and a key is derived from it using a SHA512 hash. Currently only 128 bit keys are supported. -=item B<--pid file=EpidfileE> +=item B<--pid file=EpidfileE|fd=EfiledescriptorE> This options allows to set the name of file where the process ID (pid) of the TPM -will be written into. +will be written into. It is also possible to pass a file descriptor to a file that +has been opened for writing. =item B<-r|--runas EownerE> diff --git a/man/man8/swtpm_cuse.8 b/man/man8/swtpm_cuse.8 index b45f553a..fffd925d 100644 --- a/man/man8/swtpm_cuse.8 +++ b/man/man8/swtpm_cuse.8 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "swtpm_cuse 8" -.TH swtpm_cuse 8 "2017-11-11" "swtpm" "" +.TH swtpm_cuse 8 "2018-04-05" "swtpm" "" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -254,10 +254,11 @@ has been read. This variant of the migration key parameter allows to provide a passphrase in a file. A maximum of 32 bytes are read from the file and a key is derived from it using a \&\s-1SHA512\s0 hash. Currently only 128 bit keys are supported. -.IP "\fB\-\-pid file=\fR" 4 -.IX Item "--pid file=" +.IP "\fB\-\-pid file=\fR|fd=>" 4 +.IX Item "--pid file=|fd=>" This options allows to set the name of file where the process \s-1ID \s0(pid) of the \s-1CUSE TPM\s0 -will be written into. The file will be written by the root user. +will be written into. The file will be written by the root user. It is also possible to +pass a file descriptor to a file that has been opened for writing. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBswtpm_bios\fR, \fBswtpm_ioctl\fR diff --git a/man/man8/swtpm_cuse.pod b/man/man8/swtpm_cuse.pod index a6726b6f..3cc33b49 100644 --- a/man/man8/swtpm_cuse.pod +++ b/man/man8/swtpm_cuse.pod @@ -132,10 +132,11 @@ This variant of the migration key parameter allows to provide a passphrase in a A maximum of 32 bytes are read from the file and a key is derived from it using a SHA512 hash. Currently only 128 bit keys are supported. -=item B<--pid file=EpidfileE> +=item B<--pid file=EpidfileE>|fd=EfiledescriptorE> This options allows to set the name of file where the process ID (pid) of the CUSE TPM -will be written into. The file will be written by the root user. +will be written into. The file will be written by the root user. It is also possible to +pass a file descriptor to a file that has been opened for writing. =back diff --git a/src/swtpm/common.c b/src/swtpm/common.c index 32d3798f..90e03f88 100644 --- a/src/swtpm/common.c +++ b/src/swtpm/common.c @@ -112,6 +112,10 @@ static const OptionDesc pid_opt_desc[] = { .name = "file", .type = OPT_TYPE_STRING, }, + { + .name = "fd", + .type = OPT_TYPE_INT, + }, END_OPTION_DESC }; @@ -418,18 +422,18 @@ handle_migration_key_options(char *options) * * @options: the 'pid' options to parse * @pidfile: Point to pointer for pidfile + * @pidfilefd: Pointer to file descriptor for pidfile * * Returns 0 on success, -1 on failure. */ static int -parse_pid_options(char *options, char **pidfile) +parse_pid_options(char *options, char **pidfile, int *pidfilefd) { OptionValues *ovs = NULL; char *error = NULL; const char *filename = NULL; ovs = options_parse(options, pid_opt_desc, &error); - if (!ovs) { logprintf(STDERR_FILENO, "Error parsing pid options: %s\n", error); @@ -437,16 +441,19 @@ parse_pid_options(char *options, char **pidfile) } filename = option_get_string(ovs, "file", NULL); - if (!filename) { + *pidfilefd = option_get_int(ovs, "fd", -1); + if (!filename && *pidfilefd < 0) { logprintf(STDERR_FILENO, - "The file parameter is required for the pid option.\n"); + "The file or fd parameter is required for the pid option.\n"); goto error; } - *pidfile = strdup(filename); - if (!*pidfile) { - logprintf(STDERR_FILENO, "Out of memory."); - goto error; + if (filename) { + *pidfile = strdup(filename); + if (!*pidfile) { + logprintf(STDERR_FILENO, "Out of memory."); + goto error; + } } option_values_free(ovs); @@ -471,15 +478,18 @@ int handle_pid_options(char *options) { char *pidfile = NULL; + int pidfilefd = -1; int ret = 0; if (!options) return 0; - if (parse_pid_options(options, &pidfile) < 0) + if (parse_pid_options(options, &pidfile, &pidfilefd) < 0) return -1; - if (pidfile_set(pidfile) < 0) + if (pidfile && pidfile_set(pidfile) < 0) + ret = -1; + else if (pidfile_set_fd(pidfilefd) < 0) ret = -1; free(pidfile); diff --git a/src/swtpm/cuse_tpm.c b/src/swtpm/cuse_tpm.c index 649359fa..b283e10a 100644 --- a/src/swtpm/cuse_tpm.c +++ b/src/swtpm/cuse_tpm.c @@ -200,7 +200,8 @@ static const char *usage = " log level 5 and higher will enable libtpms logging;\n" " all logged output will be prefixed with prefix;\n" " the log file can be reset (truncate)\n" -"--pid file= : write the process ID into the given file\n" +"--pid file=|fd=\n" +" : write the process ID into the given file\n" "--tpmstate dir=\n" " : set the directory where the TPM's state will be written\n" " into; the TPM_PATH environment variable can be used\n" diff --git a/src/swtpm/pidfile.c b/src/swtpm/pidfile.c index 5a5c1e8f..6ae5a51f 100644 --- a/src/swtpm/pidfile.c +++ b/src/swtpm/pidfile.c @@ -47,6 +47,7 @@ #include "logging.h" static char *g_pidfile; +static int pidfilefd = -1; int pidfile_set(const char *pidfile) { @@ -59,6 +60,13 @@ int pidfile_set(const char *pidfile) return 0; } +int pidfile_set_fd(int newpidfilefd) +{ + pidfilefd = newpidfilefd; + + return 0; +} + /* * pidfile_write: Write the given pid to the pidfile * @@ -70,10 +78,14 @@ int pidfile_write(pid_t pid) { FILE *f; - if (!g_pidfile) + if (g_pidfile) { + f = fopen(g_pidfile, "w+"); + } else if (pidfilefd >= 0) { + f = fdopen(pidfilefd, "w"); + } else { return 0; + } - f = fopen(g_pidfile, "w+"); if (!f) { logprintf(STDERR_FILENO, "Could not open pidfile %s : %s\n", g_pidfile, strerror(errno)); diff --git a/src/swtpm/pidfile.h b/src/swtpm/pidfile.h index b212a87f..2bc39632 100644 --- a/src/swtpm/pidfile.h +++ b/src/swtpm/pidfile.h @@ -39,6 +39,7 @@ #define _SWTPM_PIDFILE_H_ int pidfile_set(const char *pidfile); +int pidfile_set_fd(int newpidfilefd); int pidfile_write(pid_t pid); void pidfile_remove(void); diff --git a/src/swtpm/swtpm.c b/src/swtpm/swtpm.c index 8a7c645e..33cac7d6 100644 --- a/src/swtpm/swtpm.c +++ b/src/swtpm/swtpm.c @@ -141,7 +141,7 @@ static void usage(FILE *file, const char *prgname, const char *iface) "--locality [reject-locality-4][,allow-set-locality]\n" " : reject-locality-4: reject any command in locality 4\n" " allow-set-locality: accept SetLocality command\n" - "--pid file=\n" + "--pid file=|fd=\n" " : write the process ID into the given file\n" "--tpmstate dir=\n" " : set the directory where the TPM's state will be written\n" diff --git a/src/swtpm/swtpm_chardev.c b/src/swtpm/swtpm_chardev.c index d8d6f477..f763cdb0 100644 --- a/src/swtpm/swtpm_chardev.c +++ b/src/swtpm/swtpm_chardev.c @@ -168,7 +168,7 @@ static void usage(FILE *file, const char *prgname, const char *iface) "--key pwdfile=[,mode=aes-cbc][,remove=[true|false]]\n" " : provide a passphrase in a file; the AES key will be\n" " derived from this passphrase\n" - "--pid file=\n" + "--pid file=|fd=\n" " : write the process ID into the given file\n" "--tpmstate dir=\n" " : set the directory where the TPM's state will be written\n"