source: trunk/packages/xen-3.1/xen-3.1/tools/vtpm_manager/manager/vtpmd.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

  • Property svn:mime-type set to text/cpp
File size: 12.6 KB
Line 
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// vtpmd.c
35//
36//  Application
37//
38// ===================================================================
39
40#include <stdio.h>
41#include <unistd.h>
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <fcntl.h>
45#include <signal.h>
46#include <string.h>
47#include <pthread.h>
48#include "vtpm_manager.h"
49#include "vtpmpriv.h"
50#include "tcg.h"
51#include "log.h"
52#include "vtpm_ipc.h"
53
54#define TPM_EMULATOR_PATH "/usr/bin/vtpmd"
55
56#define VTPM_BE_FNAME          "/dev/vtpm"
57#define VTPM_DUMMY_TX_BE_FNAME "/var/vtpm/fifos/dummy_out.fifo"
58#define VTPM_DUMMY_RX_BE_FNAME "/var/vtpm/fifos/dummy_in.fifo"
59#define VTPM_TX_TPM_FNAME      "/var/vtpm/fifos/tpm_cmd_to_%d.fifo"
60#define VTPM_RX_TPM_FNAME      "/var/vtpm/fifos/tpm_rsp_from_all.fifo"
61#define VTPM_TX_VTPM_FNAME     "/var/vtpm/fifos/vtpm_rsp_to_%d.fifo"
62#define VTPM_RX_VTPM_FNAME     "/var/vtpm/fifos/vtpm_cmd_from_all.fifo"
63#define VTPM_TX_HP_FNAME       "/var/vtpm/fifos/to_console.fifo"
64#define VTPM_RX_HP_FNAME       "/var/vtpm/fifos/from_console.fifo"
65
66#define VTPM_TYPE_PVM_STRING "pvm"
67#define VTPM_TYPE_HVM_STRING "hvm"
68
69struct vtpm_thread_params_s {
70  vtpm_ipc_handle_t *tx_ipc_h;
71  vtpm_ipc_handle_t *rx_ipc_h;
72  BOOL fw_tpm;
73  vtpm_ipc_handle_t *fw_tx_ipc_h;
74  vtpm_ipc_handle_t *fw_rx_ipc_h;
75  BOOL is_priv;
76  char *thread_name;
77};
78
79// This is needed to all extra_close_dmi to close this to prevent a
80// broken pipe when no DMIs are left.
81static vtpm_ipc_handle_t *g_rx_tpm_ipc_h;
82
83void *vtpm_manager_thread(void *arg_void) {
84  TPM_RESULT *status = (TPM_RESULT *) malloc(sizeof(TPM_RESULT) );
85  struct vtpm_thread_params_s *arg = (struct vtpm_thread_params_s *) arg_void;
86
87  *status = VTPM_Manager_Handler(arg->tx_ipc_h, arg->rx_ipc_h,
88                                 arg->fw_tpm, arg->fw_tx_ipc_h, arg->fw_rx_ipc_h,
89                                 arg->is_priv, arg->thread_name);
90
91  return (status);
92}
93
94
95void signal_handler(int reason) {
96  if (pthread_equal(pthread_self(), vtpm_globals->master_pid)) {
97    vtpmloginfo(VTPM_LOG_VTPM, "VTPM Manager shutting down for signal %d.\n", reason);
98  } else {
99    // For old Linux Thread machines, signals are delivered to each thread. Deal with them.
100    vtpmloginfo(VTPM_LOG_VTPM, "Child shutting down\n");
101    pthread_exit(NULL);
102  }
103
104  VTPM_Stop_Manager();
105  exit(-1);
106}
107
108struct sigaction ctl_c_handler;
109
110TPM_RESULT VTPM_New_DMI_Extra(VTPM_DMI_RESOURCE *dmi_res, BYTE vm_type, BYTE startup_mode) {
111
112  TPM_RESULT status = TPM_SUCCESS;
113  int fh;
114  char dmi_id_str[11]; // UINT32s are up to 10 digits + NULL
115  char *tx_vtpm_name, *tx_tpm_name, *vm_type_string;
116  struct stat file_info;
117
118  if (dmi_res->dmi_id == VTPM_CTL_DM) {
119    dmi_res->tx_tpm_ipc_h = NULL;
120    dmi_res->rx_tpm_ipc_h = NULL;
121    dmi_res->tx_vtpm_ipc_h = NULL;
122    dmi_res->rx_vtpm_ipc_h = NULL;
123  } else {
124    // Create a pair of fifo pipes
125    dmi_res->rx_tpm_ipc_h = NULL;
126    dmi_res->rx_vtpm_ipc_h = NULL;
127
128    if ( ((dmi_res->tx_tpm_ipc_h = (vtpm_ipc_handle_t *) malloc (sizeof(vtpm_ipc_handle_t))) == NULL ) ||
129         ((dmi_res->tx_vtpm_ipc_h =(vtpm_ipc_handle_t *) malloc (sizeof(vtpm_ipc_handle_t))) == NULL ) ||
130         ((tx_tpm_name = (char *) malloc(11 + strlen(VTPM_TX_TPM_FNAME))) == NULL ) ||
131         ((tx_vtpm_name =(char *) malloc(11 + strlen(VTPM_TX_VTPM_FNAME))) == NULL) ) {
132      status =TPM_RESOURCES;
133      goto abort_egress;
134    }
135
136    sprintf(tx_tpm_name, VTPM_TX_TPM_FNAME, (uint32_t) dmi_res->dmi_id);
137    sprintf(tx_vtpm_name, VTPM_TX_VTPM_FNAME, (uint32_t) dmi_res->dmi_id);
138
139    if ( (vtpm_ipc_init(dmi_res->tx_tpm_ipc_h, tx_tpm_name, O_WRONLY | O_NONBLOCK, TRUE) != 0) ||
140         (vtpm_ipc_init(dmi_res->tx_vtpm_ipc_h, tx_vtpm_name, O_WRONLY, TRUE) != 0) ) { //FIXME: O_NONBLOCK?
141      status = TPM_IOERROR;
142      goto abort_egress;
143    }
144
145    // Measure DMI
146    // FIXME: This will measure DMI. Until then use a fixed DMI_Measurement value
147    // Also, this mechanism is specific to 1 VM architecture.
148    /*
149    fh = open(TPM_EMULATOR_PATH, O_RDONLY);
150    stat_ret = fstat(fh, &file_stat);
151    if (stat_ret == 0)
152      dmi_size = file_stat.st_size;
153    else {
154      vtpmlogerror(VTPM_LOG_VTPM, "Could not open vtpmd!!\n");
155      status = TPM_IOERROR;
156      goto abort_egress;
157    }
158    dmi_buffer
159    */
160    memset(&dmi_res->DMI_measurement, 0xcc, sizeof(TPM_DIGEST));
161
162    if (vm_type == VTPM_TYPE_PVM)
163      vm_type_string = (BYTE *)&VTPM_TYPE_PVM_STRING;
164    else
165      vm_type_string = (BYTE *)&VTPM_TYPE_HVM_STRING;
166
167    // Launch DMI
168    sprintf(dmi_id_str, "%d", (int) dmi_res->dmi_id);
169#ifdef MANUAL_DM_LAUNCH
170    vtpmlogerror(VTPM_LOG_VTPM, "Manually start VTPM with dmi=%s now.\n", dmi_id_str);
171    dmi_res->dmi_pid = 0;
172#else
173    pid_t pid = fork();
174
175    if (pid == -1) {
176      vtpmlogerror(VTPM_LOG_VTPM, "Could not fork to launch vtpm\n");
177      status = TPM_RESOURCES;
178      goto abort_egress;
179    } else if (pid == 0) {
180      switch (startup_mode) {
181      case TPM_ST_CLEAR:
182        execl (TPM_EMULATOR_PATH, "vtpmd", "clear", vm_type_string, dmi_id_str, NULL);
183        break;
184      case TPM_ST_STATE:
185        execl (TPM_EMULATOR_PATH, "vtpmd", "save", vm_type_string, dmi_id_str, NULL);
186        break;
187      case TPM_ST_DEACTIVATED:
188        execl (TPM_EMULATOR_PATH, "vtpmd", "deactivated", vm_type_string, dmi_id_str, NULL);
189        break;
190      default:
191        status = TPM_BAD_PARAMETER;
192        goto abort_egress;
193      }
194
195      // Returning from these at all is an error.
196      vtpmlogerror(VTPM_LOG_VTPM, "Could not exec to launch vtpm\n");
197    } else {
198      dmi_res->dmi_pid = pid;
199      vtpmloginfo(VTPM_LOG_VTPM, "Launching DMI on PID = %d\n", pid);
200    }
201#endif // MANUAL_DM_LAUNCH
202
203  } // If DMI = VTPM_CTL_DM
204    status = TPM_SUCCESS;
205
206abort_egress:
207  return (status);
208}
209
210TPM_RESULT VTPM_Close_DMI_Extra(VTPM_DMI_RESOURCE *dmi_res) {
211  TPM_RESULT status = TPM_SUCCESS;
212
213  if (vtpm_globals->connected_dmis == 0) {
214    // No more DMI's connected. Close fifo to prevent a broken pipe.
215    // This is hackish. Need to think of another way.
216    vtpm_ipc_close(g_rx_tpm_ipc_h);
217  }
218
219 
220  if (dmi_res->dmi_id != VTPM_CTL_DM) {
221    vtpm_ipc_close(dmi_res->tx_tpm_ipc_h);
222    vtpm_ipc_close(dmi_res->tx_vtpm_ipc_h);
223
224    free(dmi_res->tx_tpm_ipc_h->name);
225    free(dmi_res->tx_vtpm_ipc_h->name);
226
227#ifndef MANUAL_DM_LAUNCH
228    if (dmi_res->dmi_id != VTPM_CTL_DM) {
229      if (dmi_res->dmi_pid != 0) {
230        vtpmloginfo(VTPM_LOG_VTPM, "Killing dmi on pid %d.\n", dmi_res->dmi_pid);
231        if (kill(dmi_res->dmi_pid, SIGKILL) !=0) {
232          vtpmloginfo(VTPM_LOG_VTPM, "DMI on pid %d is already dead.\n", dmi_res->dmi_pid);
233        } else if (waitpid(dmi_res->dmi_pid, NULL, 0) != dmi_res->dmi_pid) {
234          vtpmlogerror(VTPM_LOG_VTPM, "DMI on pid %d failed to stop.\n", dmi_res->dmi_pid);
235          status = TPM_FAIL;
236        }
237      } else {
238        vtpmlogerror(VTPM_LOG_VTPM, "Could not kill dmi because it's pid was 0.\n");
239        status = TPM_FAIL;
240      }
241    }
242#endif
243
244  } //endif ! dom0
245  return status;
246}
247
248
249int main(int argc, char **argv) {
250  vtpm_ipc_handle_t *tx_be_ipc_h, *rx_be_ipc_h, rx_tpm_ipc_h, rx_vtpm_ipc_h, tx_hp_ipc_h, rx_hp_ipc_h;
251  struct vtpm_thread_params_s be_thread_params, dmi_thread_params, hp_thread_params;
252  pthread_t be_thread, dmi_thread, hp_thread;
253
254#ifdef DUMMY_BACKEND
255  vtpm_ipc_handle_t tx_dummy_ipc_h, rx_dummy_ipc_h;
256#else
257  vtpm_ipc_handle_t real_be_ipc_h;
258#endif
259
260  vtpmloginfo(VTPM_LOG_VTPM, "Starting VTPM.\n");
261 
262  // -------------------- Initialize Manager -----------------
263  if (VTPM_Init_Manager() != TPM_SUCCESS) {
264    vtpmlogerror(VTPM_LOG_VTPM, "Closing vtpmd due to error during startup.\n");
265    return -1;
266  }
267 
268  // -------------------- Setup Ctrl+C Handlers --------------
269  ctl_c_handler.sa_handler = signal_handler;
270  sigemptyset(&ctl_c_handler.sa_mask);
271  ctl_c_handler.sa_flags = 0;   
272 
273  if (sigaction(SIGINT, &ctl_c_handler, NULL) == -1)
274    vtpmlogerror(VTPM_LOG_VTPM, "Could not install SIGINT handler. Ctl+break will not stop manager gently.\n");
275 
276  // For easier debuggin with gdb
277  if (sigaction(SIGHUP, &ctl_c_handler, NULL) == -1)
278    vtpmlogerror(VTPM_LOG_VTPM, "Could not install SIGHUP handler. Ctl+break will not stop manager gently.\n");   
279 
280  sigset_t sig_mask;
281  sigemptyset(&sig_mask);
282  sigaddset(&sig_mask, SIGPIPE);
283  sigprocmask(SIG_BLOCK, &sig_mask, NULL);
284 
285  // ------------------- Set up file ipc structures ----------
286#ifdef DUMMY_BACKEND
287  if ( (vtpm_ipc_init(&tx_dummy_ipc_h, VTPM_DUMMY_TX_BE_FNAME, O_RDWR, TRUE) != 0) ||
288       (vtpm_ipc_init(&rx_dummy_ipc_h, VTPM_DUMMY_RX_BE_FNAME, O_RDWR, TRUE) != 0) ) {
289
290    vtpmlogerror(VTPM_LOG_VTPM, "Unable to create Dummy BE FIFOs.\n");
291    exit(-1);
292  }
293
294  tx_be_ipc_h = &tx_dummy_ipc_h;
295  rx_be_ipc_h = &rx_dummy_ipc_h;
296#else
297  vtpm_ipc_init(&real_be_ipc_h, VTPM_BE_FNAME, O_RDWR, FALSE);
298
299  tx_be_ipc_h = &real_be_ipc_h;
300  rx_be_ipc_h = &real_be_ipc_h;
301#endif
302
303  if ( (vtpm_ipc_init(&rx_tpm_ipc_h, VTPM_RX_TPM_FNAME, O_RDONLY, TRUE) != 0) ||
304       (vtpm_ipc_init(&rx_vtpm_ipc_h, VTPM_RX_VTPM_FNAME, O_RDWR, TRUE) != 0) || //FIXME: O_RDONLY?
305       (vtpm_ipc_init(&tx_hp_ipc_h,  VTPM_TX_HP_FNAME, O_RDWR, TRUE) != 0)    ||
306       (vtpm_ipc_init(&rx_hp_ipc_h,  VTPM_RX_HP_FNAME, O_RDWR, TRUE) != 0) ) {
307    vtpmlogerror(VTPM_LOG_VTPM, "Unable to create initial FIFOs.\n");
308    exit(-1);
309  }
310
311  g_rx_tpm_ipc_h = &rx_tpm_ipc_h;
312
313  // -------------------- Set up thread params -------------
314
315  be_thread_params.tx_ipc_h = tx_be_ipc_h;
316  be_thread_params.rx_ipc_h = rx_be_ipc_h;
317  be_thread_params.fw_tpm = TRUE;
318  be_thread_params.fw_tx_ipc_h = NULL;
319  be_thread_params.fw_rx_ipc_h = &rx_tpm_ipc_h;
320  be_thread_params.is_priv = FALSE;
321  be_thread_params.thread_name = "Backend Listener";
322
323  dmi_thread_params.tx_ipc_h = NULL;
324  dmi_thread_params.rx_ipc_h = &rx_vtpm_ipc_h;
325  dmi_thread_params.fw_tpm = FALSE;
326  dmi_thread_params.fw_tx_ipc_h = NULL;
327  dmi_thread_params.fw_rx_ipc_h = NULL;
328  dmi_thread_params.is_priv = FALSE;
329  dmi_thread_params.thread_name = "VTPM Listener";
330
331  hp_thread_params.tx_ipc_h = &tx_hp_ipc_h;
332  hp_thread_params.rx_ipc_h = &rx_hp_ipc_h;
333  hp_thread_params.fw_tpm = FALSE;
334  hp_thread_params.fw_tx_ipc_h = NULL;
335  hp_thread_params.fw_rx_ipc_h = NULL;
336  hp_thread_params.is_priv = TRUE;
337  hp_thread_params.thread_name = "Hotplug Listener";
338
339  // --------------------- Launch Threads -----------------
340
341  vtpm_lock_init();
342
343  vtpm_globals->master_pid = pthread_self();
344 
345  if (pthread_create(&be_thread, NULL, vtpm_manager_thread, &be_thread_params) != 0) {
346    vtpmlogerror(VTPM_LOG_VTPM, "Failed to launch BE Thread.\n");
347    exit(-1);
348  }
349 
350  if (pthread_create(&dmi_thread, NULL, vtpm_manager_thread, &dmi_thread_params) != 0) {
351    vtpmlogerror(VTPM_LOG_VTPM, "Failed to launch DMI Thread.\n");
352    exit(-1);
353  }
354
355 
356  if (pthread_create(&hp_thread, NULL, vtpm_manager_thread, &hp_thread_params) != 0) {
357    vtpmlogerror(VTPM_LOG_VTPM, "Failed to launch HP Thread.\n");
358    exit(-1);
359  }
360 
361  //Join the other threads until exit time.
362  pthread_join(be_thread, NULL);
363  pthread_join(dmi_thread, NULL);
364  pthread_join(hp_thread, NULL);
365 
366  vtpmlogerror(VTPM_LOG_VTPM, "VTPM Manager shut down unexpectedly.\n");
367 
368  VTPM_Stop_Manager();
369  vtpm_lock_destroy();
370  return 0;
371}
Note: See TracBrowser for help on using the repository browser.