~kirkland/eucalyptus/label-metadata

« back to all changes in this revision

Viewing changes to node/handlers_xen.c

  • Committer: Neil
  • Date: 2010-06-18 23:43:02 UTC
  • mfrom: (1195.1.47 main)
  • Revision ID: neil@pall-20100618234302-ylt6pznuzk7j09pw
latest merge from main.

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
#include <errno.h>
70
70
#include <pthread.h>
71
71
#include <signal.h> /* SIGINT */
 
72
#include <sys/stat.h>
 
73
#include <fcntl.h>
72
74
 
73
75
#include "ipc.h"
74
76
#include "misc.h"
98
100
        snprintf (nc->virsh_cmd_path, MAX_PATH, EUCALYPTUS_VIRSH, nc->home);
99
101
        snprintf (nc->xm_cmd_path, MAX_PATH, EUCALYPTUS_XM);
100
102
        snprintf (nc->detach_cmd_path, MAX_PATH, EUCALYPTUS_DETACH, nc->home, nc->home);
101
 
        snprintf (nc->connect_storage_cmd_path, MAX_PATH, EUCALYPTUS_CONNECT_ISCSI, nc->home, nc->home);
102
 
        snprintf (nc->disconnect_storage_cmd_path, MAX_PATH, EUCALYPTUS_DISCONNECT_ISCSI, nc->home, nc->home);
103
 
        snprintf (nc->get_storage_cmd_path, MAX_PATH, EUCALYPTUS_GET_ISCSI, nc->home, nc->home);
 
103
        snprintf (nc->connect_storage_cmd_path, MAX_PATH, EUCALYPTUS_CONNECT_ISCSI, nc->home);
 
104
        snprintf (nc->disconnect_storage_cmd_path, MAX_PATH, EUCALYPTUS_DISCONNECT_ISCSI, nc->home);
 
105
        snprintf (nc->get_storage_cmd_path, MAX_PATH, EUCALYPTUS_GET_ISCSI, nc->home);
104
106
        strcpy(nc->uri, HYPERVISOR_URI);
105
107
        nc->convert_to_disk = 0;
106
108
 
280
282
                        ncMetadata *meta,
281
283
                        char *instanceId,
282
284
                        char **consoleOutput) {
283
 
  char *output;
284
 
  int pid, status, rc, bufsize, fd;
285
 
  char filename[MAX_PATH];  
 
285
 
 
286
  char *console_output=NULL, *console_append=NULL, *console_main=NULL;
 
287
  char console_file[MAX_PATH];
 
288
  int rc, fd, ret;
 
289
  struct stat statbuf;
 
290
 
 
291
  int bufsize, pid, status;
 
292
 
 
293
  *consoleOutput = NULL;
 
294
 
 
295
  snprintf(console_file, 1024, "%s/%s/%s/console.append.log", scGetInstancePath(), meta->userId, instanceId);
 
296
  rc = stat(console_file, &statbuf);
 
297
  if (rc >= 0) {
 
298
    fd = open(console_file, O_RDONLY);
 
299
    if (fd >= 0) {
 
300
      console_append = malloc(4096);
 
301
      if (console_append) {
 
302
        bzero(console_append, 4096);
 
303
        rc = read(fd, console_append, (4096)-1);
 
304
        close(fd);          
 
305
      }
 
306
    }
 
307
  }
 
308
 
286
309
 
287
310
  if (getuid() != 0) {
288
 
    output = strdup("NOT SUPPORTED");
289
 
    if (!output) {
 
311
    console_main = strdup("NOT SUPPORTED");
 
312
    if (!console_main) {
290
313
      fprintf(stderr, "strdup failed (out of memory?)\n");
 
314
      if (console_append) free(console_append);
291
315
      return 1;
292
316
    }
293
 
    *consoleOutput = base64_enc((unsigned char *)output, strlen(output));    
294
 
    if (output) free(output);
295
 
    return(0);
296
 
  }
 
317
  } else {
297
318
 
298
319
  bufsize = sizeof(char) * 1024 * 64;
299
 
  output = malloc(bufsize);
300
 
  bzero(output, bufsize);
 
320
  console_main = malloc(bufsize);
 
321
  if (!console_main) {
 
322
    logprintfl(EUCAERROR, "doGetConsoleOutput(): out of memory!\n");
 
323
    if (console_append) free(console_append);
 
324
    return(1);
 
325
  }
 
326
  bzero(console_main, bufsize);
301
327
 
302
 
  snprintf(filename, MAX_PATH, "/tmp/consoleOutput.%s", instanceId);
 
328
  snprintf(console_file, MAX_PATH, "/tmp/consoleOutput.%s", instanceId);
303
329
  
304
330
  pid = fork();
305
331
  if (pid == 0) {
306
332
    int fd;
307
 
    fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
 
333
    fd = open(console_file, O_WRONLY | O_TRUNC | O_CREAT, 0644);
308
334
    if (fd < 0) {
309
335
      // error
310
336
    } else {
325
351
    struct stat statbuf;
326
352
    
327
353
    count=0;
328
 
    while(count < 10000 && stat(filename, &statbuf) < 0) {count++;}
329
 
    fd = open(filename, O_RDONLY);
 
354
    while(count < 10000 && stat(console_file, &statbuf) < 0) {count++;}
 
355
    fd = open(console_file, O_RDONLY);
330
356
    if (fd < 0) {
331
 
      logprintfl (EUCAERROR, "ERROR: could not open consoleOutput file %s for reading\n", filename);
 
357
      logprintfl (EUCAERROR, "ERROR: could not open consoleOutput file %s for reading\n", console_file);
332
358
    } else {
333
359
      FD_ZERO(&rfds);
334
360
      FD_SET(fd, &rfds);
335
361
      tv.tv_sec = 0;
336
362
      tv.tv_usec = 500000;
337
363
      rc = select(1, &rfds, NULL, NULL, &tv);
338
 
      bzero(output, bufsize);
 
364
      bzero(console_main, bufsize);
339
365
      
340
366
      count = 0;
341
367
      rc = 1;
342
368
      while(rc && count < 1000) {
343
 
        rc = read(fd, output, bufsize-1);
 
369
        rc = read(fd, console_main, bufsize-1);
344
370
        count++;
345
371
      }
346
372
      close(fd);
349
375
    wait(&status);
350
376
  }
351
377
  
352
 
  unlink(filename);
353
 
  
354
 
  if (output[0] == '\0') {
355
 
    snprintf(output, bufsize, "EMPTY");
356
 
  }
357
 
  
358
 
  *consoleOutput = base64_enc((unsigned char *)output, strlen(output));
359
 
  free(output);
360
 
  
361
 
  return(0);
 
378
  unlink(console_file);
 
379
  }
 
380
  
 
381
  ret = 1;
 
382
  console_output = malloc( (64*1024) + 4096 );
 
383
  if (console_output) {
 
384
    bzero(console_output, (64*1024) + 4096 );
 
385
    if (console_append) {
 
386
      strncat(console_output, console_append, 4096);
 
387
    }
 
388
    if (console_main) {
 
389
      strncat(console_output, console_main, 1024*64);
 
390
    }
 
391
    *consoleOutput = base64_enc((unsigned char *)console_output, strlen(console_output));
 
392
    ret = 0;
 
393
  }
 
394
 
 
395
  if (console_append) free(console_append);
 
396
  if (console_main) free(console_main);
 
397
  if (console_output) free(console_output);
 
398
 
 
399
  return(ret);
362
400
}
363
401
 
364
402
static int
404
442
                /*get credentials, decrypt them*/
405
443
                //parse_target(remoteDev);
406
444
                /*login to target*/
407
 
                if((local_iscsi_dev = connect_iscsi_target(nc->connect_storage_cmd_path, remoteDev)) == NULL)
408
 
                    return ERROR;
409
 
                snprintf (xml, 1024, "<disk type='block'><driver name='phy'/><source dev='%s'/><target dev='%s'/></disk>", local_iscsi_dev, localDevReal);
 
445
                local_iscsi_dev = connect_iscsi_target(nc->connect_storage_cmd_path, remoteDev);
 
446
                if (!local_iscsi_dev || !strstr(local_iscsi_dev, "/dev")) {
 
447
                  logprintfl(EUCAERROR, "AttachVolume(): failed to connect to iscsi target\n");
 
448
                  rc = 1;
 
449
                } else {
 
450
                  snprintf (xml, 1024, "<disk type='block'><driver name='phy'/><source dev='%s'/><target dev='%s'/></disk>", local_iscsi_dev, localDevReal);
 
451
                }
410
452
            } else {
411
453
                snprintf (xml, 1024, "<disk type='block'><driver name='phy'/><source dev='%s'/><target dev='%s'/></disk>", remoteDev, localDevReal);
412
454
                rc = stat(remoteDev, &statbuf);
435
477
            virDomainFree(dom);
436
478
            sem_v(hyp_sem);
437
479
            if(is_iscsi_target) {
438
 
                free(local_iscsi_dev);
 
480
              if (local_iscsi_dev) free(local_iscsi_dev);
439
481
            }
440
482
        } else {
441
483
            if (instance->state != BOOTING && instance->state != STAGING) {