Beispiel #1
0
int file_change(DIR_NODE *old_dir, DIR_NODE *new_dir)
{
    FILE_NODE *old_file = old_dir->next_file;
    FILE_NODE *new_file;

    int change_flag = UNCHANGED;
    /*get deleted or modified files*/
    while (old_file != NULL)
    {
        new_file = new_dir->next_file;
        while (new_file != NULL)
        {
            if (OK == is_same_file(old_file, new_file))
            {
                /*get modifid files*/
                if (OK == is_file_changed(old_file, new_file))
                {
                    new_event(MOD_FILE, (void *)new_file);
                    change_flag = CHANGED;
                }
                break;
            }
            new_file = new_file->next_file;
        }
        if (NULL == new_file)
        {
            change_flag = CHANGED;
            new_event(DEL_FILE, (void *)old_file);
        }
       old_file = old_file->next_file;
    }

    new_file = new_dir->next_file;
    /*get new files*/
    while (new_file)
    {
        old_file = old_dir->next_file;
        while (old_file != NULL)
        {
            /*don't need get modifid files*/
            if (OK == is_same_file(old_file, new_file))
                break;
            old_file = old_file->next_file;
        }
        if (NULL == old_file)
        {
            change_flag = CHANGED;
            TEST
            new_event(ADD_FILE, (void *)new_file);
        }
        new_file = new_file->next_file;
    }
    return change_flag;
}
Beispiel #2
0
/* find the shared PE mapping for a given mapping */
static struct file *get_shared_file( struct mapping *mapping )
{
    struct mapping *ptr;

    LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
        if (is_same_file( ptr->file, mapping->file ))
            return (struct file *)grab_object( ptr->shared_file );
    return NULL;
}
Beispiel #3
0
extern boolean is_recursive_link (const char* const dirName)
{
	boolean result = FALSE;
	fileStatus *status = file_stat (dirName);
	if (status->isSymbolicLink)
	{
		char* const path = file_get_absolute_name (dirName);
		while (path [strlen (path) - 1] == PATH_SEPARATOR)
			path [strlen (path) - 1] = '\0';
		while (! result  &&  strlen (path) > (size_t) 1)
		{
			char *const separator = strrchr (path, PATH_SEPARATOR);
			if (separator == NULL)
				break;
			else if (separator == path)  /* backed up to root directory */
				*(separator + 1) = '\0';
			else
				*separator = '\0';
			result = is_same_file (path, dirName);
		}
		xfree(path);
	}
	return result;
}
Beispiel #4
0
static void check_same_file(const char * file1, const char * file2) {
  if (is_same_file(file1, file2)) {
    fatal("source and destination are the same");
  }
}
Beispiel #5
0
void jscoverage_instrument(const char * source,
                           const char * destination,
                           int verbose,
                           char ** exclude,
                           int num_exclude,
                           char ** no_instrument,
                           int num_no_instrument)
{
  assert(source != NULL);
  assert(destination != NULL);

  g_verbose = verbose;

  /* check if they are the same */
  check_same_file(source, destination);

  /* check if source directory is an ancestor of destination directory */
  check_contains_file(source, destination);

  /* check that the source exists and is a directory */
  struct stat buf;
  xstat(source, &buf);
  if (! S_ISDIR(buf.st_mode)) {
    fatal("not a directory: %s", source);
  }

  /* if the destination directory exists, check that it is a jscoverage directory */
  if (stat(destination, &buf) == 0) {
    /* it exists */
    if (! S_ISDIR(buf.st_mode)) {
      fatal("not a directory: %s", destination);
    }
    if (! directory_is_empty(destination)) {
      char * expected_file = NULL;
      if (jscoverage_mode == JSCOVERAGE_MOZILLA) {
        char * modules_directory = make_path(destination, "modules");
        expected_file = make_path(modules_directory, "jscoverage.jsm");
        free(modules_directory);
      }
      else {
        expected_file = make_path(destination, "jscoverage.html");
      }
      if (stat(expected_file, &buf) == -1) {
        fatal("refusing to overwrite directory: %s", destination);
      }
      free(expected_file);
    }
  }
  else if (errno == ENOENT) {
    xmkdir(destination);
  }
  else {
    fatal("cannot stat directory: %s", destination);
  }

  /* copy the resources */
  if (jscoverage_mode == JSCOVERAGE_MOZILLA) {
    char * chrome_directory = make_path(destination, "chrome");
    char * jscoverage_chrome_directory = make_path(chrome_directory, "jscoverage");
    mkdirs(jscoverage_chrome_directory);
    copy_resource("jscoverage.manifest", chrome_directory);
    copy_resource("jscoverage.html", jscoverage_chrome_directory);
    copy_resource("jscoverage.css", jscoverage_chrome_directory);
    copy_resource("jscoverage.js", jscoverage_chrome_directory);
    copy_resource("jscoverage-throbber.gif", jscoverage_chrome_directory);
    copy_resource("jscoverage-highlight.css", jscoverage_chrome_directory);
    copy_resource("jscoverage.xul", jscoverage_chrome_directory);
    copy_resource("jscoverage-overlay.js", jscoverage_chrome_directory);
    free(jscoverage_chrome_directory);
    free(chrome_directory);

    char * modules_directory = make_path(destination, "modules");
    mkdirs(modules_directory);
    copy_resource("jscoverage.jsm", modules_directory);
    free(modules_directory);
  }
  else {
    jscoverage_copy_resources(destination);
  }

  /* finally: copy the directory */
  struct DirListEntry * list = make_recursive_dir_list(source);
  for (struct DirListEntry * p = list; p != NULL; p = p->next) {
    char * s = make_path(source, p->name);
    char * d = make_path(destination, p->name);

    /* check if it's on the exclude list */
    for (int i = 0; i < num_exclude; i++) {
      char * x = make_path(source, exclude[i]);
      if (is_same_file(x, s) || contains_file(x, s)) {
        free(x);
        goto cleanup;
      }
      free(x);
    }

    char * dd = make_dirname(d);
    mkdirs(dd);
    free(dd);

    int instrument_this = 1;

    /* check if it's on the no-instrument list */
    for (int i = 0; i < num_no_instrument; i++) {
      char * ni = make_path(source, no_instrument[i]);
      if (is_same_file(ni, s) || contains_file(ni, s)) {
        instrument_this = 0;
      }
      free(ni);
    }

    instrument_file(s, d, p->name, instrument_this);

  cleanup:
    free(s);
    free(d);
  }

  free_dir_list(list);
}
Beispiel #6
0
int main(int argc, char * argv[]){
	/*
	   if(argc > 3){
	   fprintf(stderr,"Usage: reverse <input> <output>\n");
	   exit(1);
	   }
	 */

	FILE *input,*output;

	switch(argc){
		case 1: 
			input = stdin;
			output = stdout;
			break;
		case 2: 
			input = fopen(argv[1],"r");
			if(input == NULL){
				fprintf(stderr, "Error: Cannot open file '%s'\n",argv[1]);
				exit(1);
			}
			output = stdout;
			break;
		case 3:
			if(strcmp(argv[1],argv[2])==0){
				fprintf(stderr,"Input and output file must differ\n");
				exit(1);
			}

			output = fopen(argv[2],"w");
			if(output == NULL){
				fprintf(stderr,"Error: Cannot open file '%s'\n",argv[2]);
				exit(1);
			}
			input = fopen(argv[1],"r");
			if(input == NULL){
				fprintf(stderr, "Error: Cannot open file '%s'\n",argv[1]);
				exit(1);
			}
			if(is_same_file(argv[1],argv[2])){
				fprintf(stderr,"Input and output file must differ\n");
				exit(1);
			}
			break;
		default:
			fprintf(stderr,"Usage: reverse <infile> <outfile>\n");
			exit(1);
	}

	char buf[MAX_LINE_SIZE];

	struct node *list = NULL; //the linked list that contains lines read from input file; 
	struct node *prev = NULL, *p = NULL, *current = NULL;

	//read the input file by line, and store the lines into a linked list.
	while(fgets(buf,MAX_LINE_SIZE,input)!=NULL){
		current = (struct node *)malloc(sizeof(struct node));
		if(current == NULL){
			fprintf(stderr,"malloc failed!\n");
			exit(1);
		}
		int i = 0;
		for( i = 0; i < MAX_LINE_SIZE; i++){
			current->line[i] = '\n'; //initialize with EOLs, essencial for future check
		}
		if(list == NULL){
			list = current;
		}
		current->prev = prev;
		if(current->prev != NULL){
			current->prev->next = current;
		}
		strcpy(current->line,buf);
		prev = current;

	}

	//since the pointer current now points to the last element in the list
	//struct node * last = current;
	while(current != NULL){
		struct node * current_line_start = current;
		while(current_line_start->prev != NULL && !is_eol(current_line_start->prev->line[MAX_LINE_SIZE - 2])){
			//fprintf(output,"%c\n",current_line_start->prev->line[MAX_LINE_SIZE - 2]);
			current_line_start = current_line_start->prev;
		}
		p = current_line_start;
		do{
			fprintf(output,p->line);
			//handle the special case that input file doesn't end with an EOF
		/*	if( p == last && !strchr(p->line,'\n') && !strchr(p->line,'\r') ){
				fprintf(output,"\n");
			} */
			p = p -> next;
		}while(p != current->next);

		current = current_line_start->prev;

	}

	//free the whole list;
	current = list;
	while(current != NULL){
		p = current->next;
		free(current);
		current = p;
	}

	return 0; // we won't explictly close input and output because the standard didn't say anything about fclose(stdin) or fclose(stdout). Instead we shall rely on the behaviour that all stream got closed when program terminates (by returning from main or calling exit() )


}
static int read_write_file(struct filename *f1name, struct filename *f2name, unsigned char *key, int flag)
{
	struct file *fp1 =NULL;	
	struct file *fp2 =NULL;
	struct file *fp_temp = NULL;
	int rbytes, wbytes;
	char *buf = NULL;
	int keylen =16;
	int rc;
	int flag_outfile, flag_delete_temp;
	mm_segment_t fs;

	flag_outfile =0;
	flag_delete_temp =0;
	fp1 = filp_open(f1name->name,O_RDONLY,0);
	putname(f1name);
	
	if(!fp1)
	{
		printk("Error opening input file \n");
		rc = -ENOENT;
		return rc;			
	}
	
	if(IS_ERR(fp1))
	{
		printk("Error opening input file \n");
		rc = -ENOENT;
		return rc;
	}
	
	rc = is_regular_file(fp1);
	if(rc){
		printk("Input file is not regular \n");
		rc = -EISDIR;
		goto out;
	}
	
	if(!fp1->f_op){
		printk("Permission Denied \n");
		rc = -EPERM;
		goto out;
	}

	if(!fp1->f_op->read){
		printk("Read operation not permitted \n");
		rc = -EPERM;
		goto out;
	}

	flag_outfile = is_file_exists(f2name->name);
	
	if(flag_outfile)
		fp2 = filp_open(f2name->name,O_WRONLY ,fp1->f_mode);
	else
		fp2 = filp_open(f2name->name,O_CREAT | O_WRONLY ,fp1->f_mode);

	fp_temp = filp_open(strcat((char *)f2name->name,".tmp"),O_CREAT | O_WRONLY,fp1->f_mode);

	putname(f2name);

	if(!fp2 || !fp_temp){
		printk("Error opening write file\n");
		rc= -ENOENT;
		return rc;
	}

	if(IS_ERR(fp2) || IS_ERR(fp_temp)){
		printk("Error opening write file\n");
		rc= -ENOENT;
		return rc;
	}

	if(!fp2->f_op || !fp2->f_op){
                printk("Permission Denied \n");
                rc = -EPERM;
                goto out;
        }

        if(!fp2->f_op->write || !fp2->f_op->write){
                printk("Write operation not permitted \n");
                rc = -EPERM;
                goto out;
        }
	
	rc = is_same_file(fp1,fp2);
        if(rc){
                printk("Input and output files are same \n");
                rc = -EINVAL;
                goto out;
        }
	
	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if(IS_ERR(buf)){
		printk("Error while allocationg temporary buffer for reading and writing \n");
                rc = -PTR_ERR(key);
                goto out;
        }

	/* Add info about key in preamble during encryption */
	if(flag == 1)
	{	
		rc=add_preamble(fp_temp,key,keylen);
		if(rc< 0){
			printk("Error adding preamble to the outfile\n");
			goto out;
		}
	}

	/* Verify hashed key stored in preamble during decryption */
	if(flag == 0)
	{
		rc=verify_preamble(fp1,key,keylen);
		if(rc<0){
			printk("Error verifying preamble in the encrypted file\n");
			goto out;
		}
	}
	
	/* read and write actual data */
	while(1){
		fs=get_fs();
		set_fs(get_ds());
		
		rbytes = fp1->f_op->read(fp1,buf,PAGE_SIZE,&fp1->f_pos);
		
		if(rbytes < 0){
			printk("Failed reading input file\n");
			set_fs(fs);
			rc= -EIO;
			goto out;
		}

		if (rbytes == 0){
			printk("Reached end of file while reading \n");
			rc= 0;
			goto out_rename;
		}
		
		/* If flag is set as 1, input file is encrypted */
		if(flag == 1){
			rc=encrypt_decrypt_file(buf,key,rbytes,flag);
			if(rc < 0){
				printk("Encrypt failure\n");
				goto out;
			}
		}
		/* If flag is set as 0, input file is decrypted */
		else if(flag == 0){
			rc=encrypt_decrypt_file(buf,key,rbytes,flag);
			if(rc < 0){
				printk("Decrypt failure \n");
				goto out;
			}
		}

		wbytes = fp_temp->f_op->write(fp_temp,buf,rbytes,&fp_temp->f_pos);
		
		if(wbytes < 0){
			printk("Failed writing output file\n");
                        set_fs(fs);
                        rc= -EIO;
                        goto out;
		}
		set_fs(fs);
	}
	
out_rename:
	flag_delete_temp=1;
	rc = rename_temp_file(fp_temp,fp2);
	if(rc)
		printk("Rename operation failed \n");

out:	
	if(flag_delete_temp==0){
		if(rc<0){
			if(delete_partial_file(fp_temp))
				printk("Deleting partial temp file failed \n");
		}
	}
	printk("flag_outfile = %d \n",flag_outfile);
	if(flag_outfile==0){
		if(rc < 0){
			if(delete_partial_file(fp2))
				printk("Deleting out file failed\n");
        	}
	}
	if(fp_temp)
		filp_close(fp_temp,NULL);
	if(buf)
		kfree(buf);
	if(fp2)
		filp_close(fp2,NULL);
	if(fp1)
		filp_close(fp1,NULL);
	if(IS_ERR(f2name))
		putname(f2name);
	if(IS_ERR(f1name))
		putname(f1name);
	return rc;
}
Beispiel #8
0
/*
 * Internal function for ebzip_unzip_file() and ebzip_unzip_sebxa_start().
 * If it succeeds, 0 is returned.  Otherwise -1 is returned.
 */
static int
ebzip_unzip_file_internal(const char *out_file_name, const char *in_file_name,
    Zio_Code in_zio_code, int index_page)
{
    Zio in_zio;
    unsigned char *buffer = NULL;
    off_t total_length;
    int out_file = -1;
    ssize_t length;
    struct stat in_status, out_status;
    unsigned int crc = 1;
    int progress_interval;
    int total_slices;
    int i;

    zio_initialize(&in_zio);

    /*
     * Simply copy a file, when an input file is not compressed.
     */
    if (in_zio_code == ZIO_PLAIN)
	return ebzip_copy_file(out_file_name, in_file_name);

    /*
     * Output file name information.
     */
    if (!ebzip_quiet_flag) {
	fprintf(stderr, _("==> uncompress %s <==\n"), in_file_name);
	fprintf(stderr, _("output to %s\n"), out_file_name);
	fflush(stderr);
    }

    /*
     * Get status of the input file.
     */
    if (stat(in_file_name, &in_status) < 0 || !S_ISREG(in_status.st_mode)) {
        fprintf(stderr, _("%s: no such file: %s\n"), invoked_name,
            in_file_name);
        goto failed;
    }

    /*
     * Do nothing if the `in_file_name' and `out_file_name' are the same.
     */
    if (is_same_file(out_file_name, in_file_name)) {
	if (!ebzip_quiet_flag) {
	    fprintf(stderr,
		_("the input and output files are the same, skipped.\n\n"));
	    fflush(stderr);
	}
	return 0;
    }

    /*
     * Allocate memories for in/out buffers.
     */
    buffer = (unsigned char *)malloc(EB_SIZE_PAGE << ZIO_MAX_EBZIP_LEVEL);
    if (buffer == NULL) {
	fprintf(stderr, _("%s: memory exhausted\n"), invoked_name);
	goto failed;
    }

    /*
     * If the file `out_file_name' already exists, confirm and unlink it.
     */
    if (!ebzip_test_flag
	&& stat(out_file_name, &out_status) == 0
	&& S_ISREG(out_status.st_mode)) {
	if (ebzip_overwrite_mode == EBZIP_OVERWRITE_NO) {
	    if (!ebzip_quiet_flag) {
		fputs(_("already exists, skip the file\n\n"), stderr);
		fflush(stderr);
	    }
	    return 0;
	} else if (ebzip_overwrite_mode == EBZIP_OVERWRITE_CONFIRM) {
	    int y_or_n;

	    fprintf(stderr, _("\nthe file already exists: %s\n"),
		out_file_name);
	    y_or_n = query_y_or_n(_("do you wish to overwrite (y or n)? "));
	    fputc('\n', stderr);
	    fflush(stderr);
	    if (!y_or_n)
		return 0;
        }
	if (unlink(out_file_name) < 0) {
	    fprintf(stderr, _("%s: failed to unlink the file: %s\n"),
		invoked_name, out_file_name);
	    goto failed;
	}
    }

    /*
     * Open files.
     */
    if (zio_open(&in_zio, in_file_name, in_zio_code) < 0) {
	fprintf(stderr, _("%s: failed to open the file: %s\n"),
	    invoked_name, in_file_name);
	goto failed;
    }
    if (in_zio_code == ZIO_SEBXA) {
	off_t index_location;
	off_t index_base;
	off_t zio_start_location;
	off_t zio_end_location;

	if (get_sebxa_indexes(in_file_name, index_page, &index_location,
	    &index_base, &zio_start_location, &zio_end_location) < 0) {
	    goto failed;
	}
	zio_set_sebxa_mode(&in_zio, index_location, index_base,
	    zio_start_location, zio_end_location);
    }

    if (!ebzip_test_flag) {
	trap_file_name = out_file_name;
#ifdef SIGHUP
	signal(SIGHUP, trap);
#endif
	signal(SIGINT, trap);
#ifdef SIGQUIT
	signal(SIGQUIT, trap);
#endif
#ifdef SIGTERM
	signal(SIGTERM, trap);
#endif

#ifdef O_CREAT
	out_file = open(out_file_name, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY,
	    0666 ^ get_umask());
#else
	out_file = creat(out_file_name, 0666 ^ get_umask());
#endif
	if (out_file < 0) {
	    fprintf(stderr, _("%s: failed to open the file: %s\n"),
		invoked_name, out_file_name);
	    goto failed;
	}
	trap_file = out_file;
    }

    /*
     * Read a slice from the input file, uncompress it if required,
     * and then write it to the output file.
     */
    total_length = 0;
    total_slices = (in_zio.file_size + in_zio.slice_size - 1)
	/ in_zio.slice_size;
    progress_interval = EBZIP_PROGRESS_INTERVAL_FACTOR;
    if (((total_slices + 999) / 1000) > progress_interval)
	progress_interval = ((total_slices + 999) / 1000);

    for (i = 0; i < total_slices; i++) {
	/*
	 * Read a slice.
	 */
	if (zio_lseek(&in_zio, total_length, SEEK_SET) < 0) {
	    fprintf(stderr, _("%s: failed to seek the file: %s\n"),
		invoked_name, in_file_name);
	    goto failed;
	}
	length = zio_read(&in_zio, (char *)buffer, in_zio.slice_size);
	if (length < 0) {
	    fprintf(stderr, _("%s: failed to read from the file: %s\n"),
		invoked_name, in_file_name);
	    goto failed;
	} else if (length == 0) {
	    fprintf(stderr, _("%s: unexpected EOF: %s\n"),
		invoked_name, in_file_name);
	    goto failed;
	} else if (length != in_zio.slice_size
	    && total_length + length != in_zio.file_size) {
	    fprintf(stderr, _("%s: unexpected EOF: %s\n"),
		invoked_name, in_file_name);
	    goto failed;
	}

	/*
	 * Update CRC.  (Calculate adler32 again.)
	 */
	if (in_zio.code == ZIO_EBZIP1)
	    crc = adler32((uLong)crc, (Bytef *)buffer, (uInt)length);

	/*
	 * Write the slice to `out_file'.
	 */
	if (!ebzip_test_flag) {
	    if (write(out_file, buffer, length) != length) {
		fprintf(stderr, _("%s: failed to write to the file, %s: %s\n"),
		    invoked_name, strerror(errno), out_file_name);
		goto failed;
	    }
	}
	total_length += length;

	/*
	 * Output status information unless `quiet' mode.
	 */
	if (!ebzip_quiet_flag && (i + 1) % progress_interval == 0) {
#if defined(PRINTF_LL_MODIFIER)
	    fprintf(stderr, _("%4.1f%% done (%llu / %llu bytes)\n"),
		(double) (i + 1) * 100.0 / (double) total_slices,
		(unsigned long long) total_length,
		(unsigned long long) in_zio.file_size);
#elif defined(PRINTF_I64_MODIFIER)
	    fprintf(stderr, _("%4.1f%% done (%I64u / %I64u bytes)\n"),
		(double) (i + 1) * 100.0 / (double) total_slices,
		(unsigned __int64) total_length,
		(unsigned __int64) in_zio.file_size);
#else
	    fprintf(stderr, _("%4.1f%% done (%lu / %lu bytes)\n"),
		(double) (i + 1) * 100.0 / (double) total_slices,
		(unsigned long) total_length,
		(unsigned long) in_zio.file_size);
#endif
	    fflush(stderr);
	}
    }

    /*
     * Output the result unless quiet mode.
     */
    if (!ebzip_quiet_flag) {
#if defined(PRINTF_LL_MODIFIER)
	fprintf(stderr, _("completed (%llu / %llu bytes)\n"),
	    (unsigned long long) in_zio.file_size,
	    (unsigned long long) in_zio.file_size);
#elif defined(PRINTF_I64_MODIFIER)
	fprintf(stderr, _("completed (%I64u / %I64u bytes)\n"),
	    (unsigned __int64) in_zio.file_size,
	    (unsigned __int64) in_zio.file_size);
#else
	fprintf(stderr, _("%lu -> %lu bytes\n\n"),
	    (unsigned long) in_status.st_size,
	    (unsigned long) total_length);
#endif
	fflush(stderr);
    }

    /*
     * Close files.
     */
    zio_close(&in_zio);
    zio_finalize(&in_zio);

    if (!ebzip_test_flag) {
	close(out_file);
	out_file = -1;
	trap_file = -1;
	trap_file_name = NULL;
#ifdef SIGHUP
	signal(SIGHUP, SIG_DFL);
#endif
	signal(SIGINT, SIG_DFL);
#ifdef SIGQUIT
	signal(SIGQUIT, SIG_DFL);
#endif
#ifdef SIGTERM
	signal(SIGTERM, SIG_DFL);
#endif
    }

    /*
     * Check for CRC.
     */
    if (in_zio.code == ZIO_EBZIP1 && in_zio.crc != crc) {
	fprintf(stderr, _("%s: CRC error: %s\n"), invoked_name, out_file_name);
	goto failed;
    }

    /*
     * Delete an original file unless the keep flag is set.
     */
    if (!ebzip_test_flag && !ebzip_keep_flag)
	unlink_files_add(in_file_name);

    /*
     * Set owner, group, permission, atime and mtime of `out_file'.
     * We ignore return values of `chown', `chmod' and `utime'.
     */
    if (!ebzip_test_flag) {
	struct utimbuf utim;

	utim.actime = in_status.st_atime;
	utim.modtime = in_status.st_mtime;
	utime(out_file_name, &utim);
    }

    /*
     * Dispose memories.
     */
    free(buffer);

    return 0;

    /*
     * An error occurs...
     */
  failed:
    if (buffer != NULL)
	free(buffer);

    zio_close(&in_zio);
    zio_finalize(&in_zio);

    if (0 <= out_file) {
	close(out_file);
	trap_file = -1;
	trap_file_name = NULL;
#ifdef SIGHUP
	signal(SIGHUP, SIG_DFL);
#endif
	signal(SIGINT, SIG_DFL);
#ifdef SIGQUIT
	signal(SIGQUIT, SIG_DFL);
#endif
#ifdef SIGTERM
	signal(SIGTERM, SIG_DFL);
#endif
    }

    fputc('\n', stderr);
    fflush(stderr);

    return -1;
}
Beispiel #9
0
int vzctl2_env_unreg(struct vzctl_env_handle *h, int flags)
{
	char buf[STR_SIZE];
	char host[STR_SIZE];
	int ret;
	const char *ve_root;
	const char *ve_private = h->env_param->fs->ve_private;

	/* preserve compatibility
	 * VZ_REG_SKIP_HA_CLUSTER is alias for VZ_REG_SKIP_CLUSTER
	 */
	if (flags & VZ_REG_SKIP_HA_CLUSTER)
		flags |= VZ_REG_SKIP_CLUSTER;

	if (is_env_run(h))
		return vzctl_err(VZCTL_E_ENV_RUN, 0,
			"Container is running, Stop Container before proceeding.");

	if (access(ve_private, F_OK) && errno == ENOENT) {
		ret = unregister_env_conf(h);
		if (ret)
			return ret;
		goto out;
	}

	if (vzctl2_env_layout_version(ve_private) < VZCTL_LAYOUT_4)
		return 0;

	ret = vzctl_check_owner_quiet(ve_private, buf, sizeof(buf), host, sizeof(host));
	if (ret == VZCTL_E_ENV_MANAGE_DISABLED) {
		logger(0, 0, "Owner check failed on the server '%s':"
				" Container (%s) is registered for '%s'",
				buf, ve_private, host);
		ret = unregister_env_conf(h);
		if (ret)
			return ret;
		goto out;
	} else if (ret)
		return ret;

	ve_root = h->env_param->fs->ve_root;
	if (ve_root != NULL && vzctl2_env_is_mounted(h) == 1) {
		if (vzctl2_env_umount(h, 0))
			return vzctl_err(VZCTL_E_FS_MOUNTED, 0,
				"Container is mounted, Unmount Container "
				"before proceeding.");
	}

	if (!(flags & VZ_UNREG_PRESERVE)) {
		/* Remove VEID from /ve_private/ve.conf */
		vzctl2_env_set_param(h, "VEID", NULL);
		if (vzctl2_env_save(h))
			return VZCTL_E_UNREGISTER;

		/* Remove VE_PRIVATE/.owner */
		snprintf(buf, sizeof(buf), "%s/" VZCTL_VE_OWNER, ve_private);
		unlink(buf);
	}

	/* cleanup name */
	if (h->env_param->name->name != NULL) {
		char name_path[PATH_MAX];
		char veconf[PATH_MAX];

		snprintf(veconf, sizeof(veconf), "%s/" VZCTL_VE_CONF, ve_private);
		snprintf(name_path, sizeof(name_path), ENV_NAME_DIR "%s",
				h->env_param->name->name);
		if (is_same_file(name_path, veconf))
			unlink(name_path);
	}

	/* Remove /etc/vz/conf/VEID.conf */
	unregister_env_conf(h);

	if (!(flags & VZ_REG_SKIP_CLUSTER) &&
			is_shared_fs(ve_private) &&
			shaman_del_resource(EID(h)))
		logger(0, 0,"Warning: Failed to unregister the Container on HA cluster");

out:
	vzctl2_destroy_net_stat(h, 0);
	vzctl2_send_state_evt(EID(h), VZCTL_ENV_UNREGISTERED);
	logger(0, 0, "Container unregistered succesfully");

	return 0;
}