1
// ===================================================================
3
// Copyright (c) 2005, Intel Corp.
4
// All rights reserved.
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions
10
// * Redistributions of source code must retain the above copyright
11
// notice, this list of conditions and the following disclaimer.
12
// * Redistributions in binary form must reproduce the above
13
// copyright notice, this list of conditions and the following
14
// disclaimer in the documentation and/or other materials provided
15
// with the distribution.
16
// * Neither the name of Intel Corporation nor the names of its
17
// contributors may be used to endorse or promote products derived
18
// from this software without specific prior written permission.
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31
// OF THE POSSIBILITY OF SUCH DAMAGE.
32
// ===================================================================
34
// vtpm_ipc.c Implements ipc routines using file io. This file can
35
// be replaced with other ipc types.
37
// ===================================================================
44
int vtpm_ipc_init(vtpm_ipc_handle_t *ipc_h, char* name, int flags, BOOL create) {
47
ipc_h->fh = VTPM_IPC_CLOSED;
50
return(vtpm_ipc_create(ipc_h));
55
// Create the file that needs opening. Used only for FIFOs
56
// FYI: This may cause problems in other file IO schemes. We'll see.
57
int vtpm_ipc_create(vtpm_ipc_handle_t *ipc_h) {
59
struct stat file_info;
61
if ((!ipc_h) || (!ipc_h->name))
64
if ( stat(ipc_h->name, &file_info) == -1) {
65
if ( mkfifo(ipc_h->name, S_IWUSR | S_IRUSR ) ) {
66
vtpmlogerror(VTPM_LOG_VTPM, "Failed to create fifo %s.\n", ipc_h->name);
71
ipc_h->fh = VTPM_IPC_CLOSED;
77
// Read size bytes. If FH isn't open, open it.
78
int vtpm_ipc_read(vtpm_ipc_handle_t *ipc_h, vtpm_ipc_handle_t *alt_ipc_h, BYTE *bytes, UINT32 size){
79
vtpm_ipc_handle_t *my_ipc_h;
88
if (my_ipc_h->fh == VTPM_IPC_CLOSED) {
89
my_ipc_h->fh = open(my_ipc_h->name, my_ipc_h->flags);
92
if ( my_ipc_h->fh == VTPM_IPC_CLOSED ) {
93
vtpmlogerror(VTPM_LOG_VTPM, "VTPM ERROR: Can't open %s for reading.\n", my_ipc_h->name);
97
result = read(my_ipc_h->fh, bytes, size);
99
my_ipc_h->fh = VTPM_IPC_CLOSED;
105
// Write size bytes. If FH isn't open, open it.
106
int vtpm_ipc_write(vtpm_ipc_handle_t *ipc_h, vtpm_ipc_handle_t *alt_ipc_h, BYTE *bytes, UINT32 size) {
107
vtpm_ipc_handle_t *my_ipc_h;
113
my_ipc_h = alt_ipc_h;
116
if (my_ipc_h->fh == VTPM_IPC_CLOSED) {
117
my_ipc_h->fh = open(my_ipc_h->name, my_ipc_h->flags);
120
if ( my_ipc_h->fh == VTPM_IPC_CLOSED ) {
121
vtpmlogerror(VTPM_LOG_VTPM, "VTPM ERROR: Can't open %s for writing.\n", my_ipc_h->name);
125
result = write(my_ipc_h->fh, bytes, size);
127
my_ipc_h->fh = VTPM_IPC_CLOSED;
133
// Mark file as closed and try and close it. Errors not reported.
134
void vtpm_ipc_close(vtpm_ipc_handle_t *ipc_h) {
138
ipc_h->fh = VTPM_IPC_CLOSED;