1 | // =================================================================== |
---|
2 | // |
---|
3 | // Copyright (c) 2005, Intel Corp. |
---|
4 | // All rights reserved. |
---|
5 | // |
---|
6 | // Redistribution and use in source and binary forms, with or without |
---|
7 | // modification, are permitted provided that the following conditions |
---|
8 | // are met: |
---|
9 | // |
---|
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. |
---|
19 | // |
---|
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 | // =================================================================== |
---|
33 | // |
---|
34 | // vtpm_ipc.c Implements ipc routines using file io. This file can |
---|
35 | // be replaced with other ipc types. |
---|
36 | // |
---|
37 | // =================================================================== |
---|
38 | |
---|
39 | #include <sys/stat.h> |
---|
40 | #include "vtpm_ipc.h" |
---|
41 | #include "vtpmpriv.h" |
---|
42 | #include "log.h" |
---|
43 | |
---|
44 | int vtpm_ipc_init(vtpm_ipc_handle_t *ipc_h, char* name, int flags, BOOL create) { |
---|
45 | ipc_h->name = name; |
---|
46 | ipc_h->flags = flags; |
---|
47 | ipc_h->fh = VTPM_IPC_CLOSED; |
---|
48 | |
---|
49 | if (create) |
---|
50 | return(vtpm_ipc_create(ipc_h)); |
---|
51 | else |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
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) { |
---|
58 | int fh; |
---|
59 | struct stat file_info; |
---|
60 | |
---|
61 | if ((!ipc_h) || (!ipc_h->name)) |
---|
62 | return -1; |
---|
63 | |
---|
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); |
---|
67 | return -1; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | ipc_h->fh = VTPM_IPC_CLOSED; |
---|
72 | |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
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; |
---|
80 | int result; |
---|
81 | |
---|
82 | if (ipc_h) { |
---|
83 | my_ipc_h = ipc_h; |
---|
84 | } else { |
---|
85 | my_ipc_h = alt_ipc_h; |
---|
86 | } |
---|
87 | |
---|
88 | if (my_ipc_h->fh == VTPM_IPC_CLOSED) { |
---|
89 | my_ipc_h->fh = open(my_ipc_h->name, my_ipc_h->flags); |
---|
90 | } |
---|
91 | |
---|
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); |
---|
94 | return -1; |
---|
95 | } |
---|
96 | |
---|
97 | result = read(my_ipc_h->fh, bytes, size); |
---|
98 | if (result < 0) { |
---|
99 | my_ipc_h->fh = VTPM_IPC_CLOSED; |
---|
100 | } |
---|
101 | |
---|
102 | return (result); |
---|
103 | } |
---|
104 | |
---|
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; |
---|
108 | int result; |
---|
109 | |
---|
110 | if (ipc_h) { |
---|
111 | my_ipc_h = ipc_h; |
---|
112 | } else { |
---|
113 | my_ipc_h = alt_ipc_h; |
---|
114 | } |
---|
115 | |
---|
116 | if (my_ipc_h->fh == VTPM_IPC_CLOSED) { |
---|
117 | my_ipc_h->fh = open(my_ipc_h->name, my_ipc_h->flags); |
---|
118 | } |
---|
119 | |
---|
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); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | result = write(my_ipc_h->fh, bytes, size); |
---|
126 | if (result < 0) { |
---|
127 | my_ipc_h->fh = VTPM_IPC_CLOSED; |
---|
128 | } |
---|
129 | |
---|
130 | return (result); |
---|
131 | } |
---|
132 | |
---|
133 | // Mark file as closed and try and close it. Errors not reported. |
---|
134 | void vtpm_ipc_close(vtpm_ipc_handle_t *ipc_h) { |
---|
135 | |
---|
136 | if (ipc_h) { |
---|
137 | close(ipc_h->fh); |
---|
138 | ipc_h->fh = VTPM_IPC_CLOSED; |
---|
139 | } |
---|
140 | |
---|
141 | } |
---|