swtpm: Allow file desciptor passing for pid file

Allow the passing of a file descriptor where the PID file will
be written into.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
This commit is contained in:
Stefan Berger 2018-04-05 10:43:23 -04:00
parent 0d00e18f87
commit db60877590
10 changed files with 55 additions and 27 deletions

View File

@ -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=<pidfile>\fR" 4
.IX Item "--pid file=<pidfile>"
.IP "\fB\-\-pid file=<pidfile>|fd=<filedescriptor>\fR" 4
.IX Item "--pid file=<pidfile>|fd=<filedescriptor>"
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 <owner>\fR" 4
.IX Item "-r|--runas <owner>"
Switch to the given user. This option can only be used when swtpm is started as root.

View File

@ -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=E<lt>pidfileE<gt>>
=item B<--pid file=E<lt>pidfileE<gt>|fd=E<lt>filedescriptorE<gt>>
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 E<lt>ownerE<gt>>

View File

@ -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=<pidfile>\fR" 4
.IX Item "--pid file=<pidfile>"
.IP "\fB\-\-pid file=<pidfile>\fR|fd=<filedescriptor>>" 4
.IX Item "--pid file=<pidfile>|fd=<filedescriptor>>"
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

View File

@ -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=E<lt>pidfileE<gt>>
=item B<--pid file=E<lt>pidfileE<gt>>|fd=E<lt>filedescriptorE<gt>>
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

View File

@ -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);

View File

@ -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=<path> : write the process ID into the given file\n"
"--pid file=<path>|fd=<filedescriptor>\n"
" : write the process ID into the given file\n"
"--tpmstate dir=<dir>\n"
" : set the directory where the TPM's state will be written\n"
" into; the TPM_PATH environment variable can be used\n"

View File

@ -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));

View File

@ -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);

View File

@ -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=<path>\n"
"--pid file=<path>|fd=<filedescriptor>\n"
" : write the process ID into the given file\n"
"--tpmstate dir=<dir>\n"
" : set the directory where the TPM's state will be written\n"

View File

@ -168,7 +168,7 @@ static void usage(FILE *file, const char *prgname, const char *iface)
"--key pwdfile=<path>[,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=<path>\n"
"--pid file=<path>|fd=<filedescriptor>\n"
" : write the process ID into the given file\n"
"--tpmstate dir=<dir>\n"
" : set the directory where the TPM's state will be written\n"