コード例 #1
0
ファイル: main.c プロジェクト: AgamAgarwal/minix
/*===========================================================================*
 *				construct_tree				     *
 *===========================================================================*/
static void construct_tree(struct inode *dir, struct file *files)
{
	/* Construct a tree of static files from a null-terminated array of
	 * file structures, recursively creating directories which have their
	 * associated data point to child file structures.
	 */
	struct file *file;
	struct inode *node;
	struct inode_stat stat;

	stat.uid = SUPER_USER;
	stat.gid = SUPER_USER;
	stat.size = 0;
	stat.dev = NO_DEV;

	for (file = files; file->name != NULL; file++) {
		stat.mode = file->mode;

		node = add_inode(dir, file->name, NO_INDEX, &stat, (index_t) 0,
			(cbdata_t) file->data);

		assert(node != NULL);

		if (S_ISDIR(file->mode))
			construct_tree(node, (struct file *) file->data);
	}
}
コード例 #2
0
void
find_net_sockets(struct inode_list **ino_list,
		 struct ip_connections *conn_list, const char *protocol,
		 dev_t netdev)
{
	FILE *fp;
	char pathname[200], line[BUFSIZ];
	unsigned long loc_port, rmt_port;
	unsigned long rmt_addr, scanned_inode;
	ino_t inode;
	struct ip_connections *conn_tmp;

	if (snprintf(pathname, 200, "/proc/net/%s", protocol) < 0)
		return;

	if ((fp = fopen(pathname, "r")) == NULL) {
		fprintf(stderr, _("Cannot open protocol file \"%s\": %s\n"),
			pathname, strerror(errno));
		return;
	}
	while (fgets(line, BUFSIZ, fp) != NULL) {
		if (sscanf
		    (line,
		     "%*u: %*x:%lx %08lx:%lx %*x %*x:%*x %*x:%*x %*x %*d %*d %lu",
		     &loc_port, &rmt_addr, &rmt_port, &scanned_inode) != 4)
			continue;
#ifdef DEBUG
		printf("Found IPv4 *:%lu with %s:%lu\n", loc_port,
		       inet_ntoa(*((struct in_addr *)&rmt_addr)), rmt_port);
#endif				/* DEBUG */
		inode = scanned_inode;
		for (conn_tmp = conn_list; conn_tmp != NULL;
		     conn_tmp = conn_tmp->next) {
#ifdef DEBUG
			printf("  Comparing with *.%lu %s:%lu\n",
			       conn_tmp->lcl_port,
			       inet_ntoa(conn_tmp->rmt_address),
			       conn_tmp->rmt_port);
#endif
			if ((conn_tmp->lcl_port == 0
			     || conn_tmp->lcl_port == loc_port)
			    && (conn_tmp->rmt_port == 0
				|| conn_tmp->rmt_port == rmt_port)
			    && (conn_tmp->rmt_address.s_addr == INADDR_ANY
				||
				(memcmp
				 (&(conn_tmp->rmt_address), &(rmt_addr),
				  4) == 0))) {
				/* add inode to list */
#ifdef DEBUG
				printf("Added inode!\n");
#endif				/* DEBUG */
				add_inode(ino_list, conn_tmp->name, netdev,
					  inode);
			}
		}

	}
	return;
}
コード例 #3
0
ファイル: devfs.c プロジェクト: bithinalangot/seakernel
void init_dev_fs()
{
	devfs_root = (struct inode*)kmalloc(sizeof(struct inode));
	_strcpy(devfs_root->name, "dev");
	devfs_root->i_ops = &devfs_inode_ops;
	devfs_root->parent = current_task->thread->root;
	devfs_root->mode = S_IFDIR | 0x1FF;
	devfs_root->num = -1;
	rwlock_create(&devfs_root->rwl);
	/* Create device nodes */
	char tty[6] = "tty";
	int i;
	for(i=1;i<10;i++) {
		sprintf(tty, "tty%d", i);
		devfs_add(devfs_root, tty, S_IFCHR, 3, i);
	}
	devfs_add(devfs_root, "tty", S_IFCHR, 4, 0);
	devfs_add(devfs_root, "null", S_IFCHR, 0, 0);
	devfs_add(devfs_root, "zero", S_IFCHR, 1, 0);
	devfs_add(devfs_root, "com0", S_IFCHR, 5, 0);
	/* Mount the filesystem */
	add_inode(current_task->thread->root, devfs_root);
#if CONFIG_MODULES
	add_kernel_symbol(devfs_add);
	add_kernel_symbol(devfs_remove);
#endif
}
コード例 #4
0
ファイル: tree.c プロジェクト: josepedrazap/trabajo2
/*
 * Construct one file in a PID directory, if a file with the given name should
 * exist at all.
 */
static void
make_one_pid_entry(struct inode * parent, char * name, int slot)
{
	struct inode *node;
	struct inode_stat stat;
	int i;

	/* Don't readd if it is already there. */
	node = get_inode_by_name(parent, name);
	if (node != NULL)
		return;

	/* Only add the file if it is a known, registered name. */
	for (i = 0; pid_files[i].name != NULL; i++) {
		if (!strcmp(name, pid_files[i].name)) {
			make_stat(&stat, slot, i);

			node = add_inode(parent, name, i, &stat, (index_t)0,
			    (cbdata_t)0);

			if (node == NULL)
				out_of_inodes();

			break;
		}
	}
}
コード例 #5
0
ファイル: rssfs.c プロジェクト: thomasbrus/besturingssystemen
/* Returns the inode of the directory based on the date of RSS entry (pubDate). 
   If it does not have a pubDate atribute, it will return the inode of the directory with name "no_pubDate". */
struct inode *get_inode_by_date (char *pubDate) {
  struct inode *result;
  struct inode *temp;
  struct inode_stat dir_stat;
  char month[4];
  char year[5];

  // Setting the variables of the struct in order to create a new directory.
  dir_stat.mode = S_IFDIR;
  dir_stat.uid = 0;
  dir_stat.gid = 0;
  dir_stat.size = 0;
  dir_stat.dev = NO_DEV;

  if (strcmp (pubDate, "") == 0){
    // The RSS entry does not have an attribute called pubDate.
    result = get_inode_by_name(get_root_inode(), "no_pubDate");
    if (result == 0){
      result = add_inode(get_root_inode(), "no_pubDate", NO_INDEX, &dir_stat, 0, 0);
    }
  } else {
    // Extract the month from pubDate.
    strncpy(month, pubDate+8, 3);
    // Setting the end of the string.
    month[3] = 0;
    // Extract the year from pubDate.
    strncpy(year, pubDate+12, 4);
    year[4] = 0;

    temp = get_inode_by_name(get_root_inode(), year);

    if (temp == 0){
      // Make a new directory.
      temp = add_inode(get_root_inode(), year, NO_INDEX, &dir_stat, 0, 0);
      result = add_inode(temp, month, NO_INDEX, &dir_stat, 0, 0);
    } else {
      // The directory already exists.
      result = get_inode_by_name(temp, month);
      if (result == 0){
        // Make a new subdirectory.
        result = add_inode(temp, month, NO_INDEX, &dir_stat, 0, 0);
      }
    }
  }

  return result;
}
コード例 #6
0
ファイル: traverse.c プロジェクト: paulmadore/ccrypt
static void traverse_file(char *filename) {
  struct stat buf;
  int st;
  int link = 0;
  int r;
  
  st = lstat(filename, &buf);
  if (!st && S_ISLNK(buf.st_mode)) {  /* is a symbolic link */
    link = 1;
    st = stat(filename, &buf);
  }
  if (st || !S_ISDIR(buf.st_mode)) {
    file_action(filename);
    return;
  }
  
  /* is a directory */
  if (cmd.recursive<=1 && link==1) { /* ignore link */
    if (cmd.verbose>=0) {
      fprintf(stderr, "%s: %s: directory is a symbolic link -- ignored\n", cmd.name, filename);
      symlink_warnings++;
    }
    return;
  }

  if (cmd.recursive==0) {  /* ignore */
    if (cmd.verbose>=0) {
      fprintf(stderr, "%s: %s: is a directory -- ignored\n", cmd.name, filename);
      isreg_warnings++;
    }
    return;
  } 

  r = known_inode(buf.st_ino, buf.st_dev);

  if (r != -1) { /* already traversed */
    if (cmd.verbose>0) {
      fprintf(stderr, "Already visited directory %s -- skipped.\n", filename);
    }
    return;
  }
  
  add_inode(buf.st_ino, buf.st_dev, 1);

  /* recursively traverse directory */
  {
    char **filelist;
    int count;

    get_filelist(filename, &filelist, &count);
    traverse_files(filelist, count);
    free_filelist(filelist, count);
  }
  return;
}
コード例 #7
0
ファイル: idle.c プロジェクト: bithinalangot/seakernel
struct inode *set_as_kernel_task(char *name)
{
	struct inode *i = (struct inode *)kmalloc(sizeof(struct inode));
	rwlock_create(&i->rwl);
	strncpy(i->name, name, INAME_LEN);
	add_inode(kproclist, i);
	raise_flag(TF_KTASK);
	strncpy((char *)current_task->command, name, 128);
	printk(1, "[kernel]: Added '%s' as kernel task\n", name);
	return i;
}
コード例 #8
0
ファイル: copyout.c プロジェクト: pinard/paxutils
static void
writeout_defered_file (struct new_cpio_header *header_param,
		       int handle)
{
  int input_handle;
  struct new_cpio_header header;
  struct utimbuf times;		/* for setting file times */

  /* Initialize this in case it has members we don't know to set.  */
  memset (&times, 0, sizeof (struct utimbuf));

  header = *header_param;


  input_handle = open (header_param->c_name, O_RDONLY | O_BINARY, 0);
  if (input_handle < 0)
    {
      error (0, errno, "%s", header_param->c_name);
      return;
    }

  if (archive_format == CRC_ASCII_FORMAT)
    header.c_chksum = read_for_checksum (input_handle,
					   header.c_filesize,
					   header_param->c_name);

  (*header_writer) (&header, handle);
  copy_files_disk_to_tape (input_handle, handle, header.c_filesize,
			   header_param->c_name);

#ifndef __MSDOS__
  if (archive_format == V7_FORMAT || archive_format == POSIX_FORMAT
      || archive_format == GNUTAR_FORMAT)
    add_inode (header.c_ino, header.c_name, header.c_dev_maj,
	       header.c_dev_min);
#endif

  tape_pad_output (handle, header.c_filesize);

  if (close (input_handle) < 0)
    error (0, errno, "%s", header_param->c_name);
  if (reset_access_time_option)
    {
      times.actime = header.c_mtime;
      times.modtime = header.c_mtime;
      /* Silently ignore EROFS because reading the file won't have upset its
	 timestamp if it's on a read-only filesystem.  */
      if (utime (header.c_name, &times) < 0 && errno != EROFS)
	error (0, errno, _("%s: error resetting file access time"),
	       header.c_name);
    }
}
コード例 #9
0
ファイル: devfs.c プロジェクト: bithinalangot/seakernel
struct inode *devfs_create(struct inode *base, char *name, mode_t mode)
{
	struct inode *i;
	i = (struct inode*)kmalloc(sizeof(struct inode));
	strncpy(i->name, name, INAME_LEN);
	i->i_ops = &devfs_inode_ops;
	i->parent = devfs_root;
	i->mode = mode | 0x1FF;
	i->num = add_atomic(&devfs_nodescount, 1);
	rwlock_create(&i->rwl);
	add_inode(base, i);
	return i;
}
コード例 #10
0
ファイル: incore_ino.c プロジェクト: chandanr/xfsprogs-dev
ino_tree_node_t *
set_inode_free_alloc(struct xfs_mount *mp, xfs_agnumber_t agno, xfs_agino_t ino)
{
	ino_tree_node_t *ino_rec;

	ino_rec = add_inode(mp, agno, ino);

	ASSERT(ino_rec != NULL);
	ASSERT(ino >= ino_rec->ino_startnum &&
		ino - ino_rec->ino_startnum < XFS_INODES_PER_CHUNK);

	set_inode_free(ino_rec, ino - ino_rec->ino_startnum);

	return(ino_rec);
}
コード例 #11
0
int parse_file(struct names *this_name, struct inode_list **ino_list)
{
	if (stat(this_name->filename, &(this_name->st)) != 0) {
      if (errno == ENOENT)
        fprintf(stderr, _("Specified filename %s does not exist.\n"), this_name->filename);
      else
		fprintf(stderr, _("Cannot stat %s: %s\n"), this_name->filename, strerror(errno));
		return -1;
	}
#ifdef DEBUG
	printf("adding file %s %lX %lX\n", this_name->filename,
	       (unsigned long)this_name->st.st_dev, (unsigned long)this_name->st.st_ino);
#endif				/* DEBUG */
	add_inode(ino_list, this_name, this_name->st.st_dev, this_name->st.st_ino);
	return 0;
}
コード例 #12
0
int
parse_unixsockets(struct names *this_name, struct inode_list **ino_list,
		  struct unixsocket_list *sun_head)
{
	struct unixsocket_list *sun_tmp;
	dev_t net_dev;

	net_dev = find_net_dev();

	for (sun_tmp = sun_head; sun_tmp != NULL; sun_tmp = sun_tmp->next) {
		if (sun_tmp->dev == this_name->st.st_dev && sun_tmp->inode == this_name->st.st_ino) {
			add_inode(ino_list, this_name, net_dev,
				  sun_tmp->net_inode);
			return 0;
		}
	}
	return 0;
}
コード例 #13
0
ファイル: incore_ino.c プロジェクト: chandanr/xfsprogs-dev
/*
 * if ino doesn't exist, it must be properly aligned -- on a
 * filesystem block boundary or XFS_INODES_PER_CHUNK boundary,
 * whichever alignment is larger.
 */
ino_tree_node_t *
set_inode_used_alloc(struct xfs_mount *mp, xfs_agnumber_t agno, xfs_agino_t ino)
{
	ino_tree_node_t *ino_rec;

	/*
	 * check alignment -- the only way to detect this
	 * is too see if the chunk overlaps another chunk
	 * already in the tree
	 */
	ino_rec = add_inode(mp, agno, ino);

	ASSERT(ino_rec != NULL);
	ASSERT(ino >= ino_rec->ino_startnum &&
		ino - ino_rec->ino_startnum < XFS_INODES_PER_CHUNK);

	set_inode_used(ino_rec, ino - ino_rec->ino_startnum);

	return(ino_rec);
}
コード例 #14
0
ファイル: tree.c プロジェクト: josepedrazap/trabajo2
/*
 * Construct all files in a PID directory.
 */
static void
make_all_pid_entries(struct inode * parent, int slot)
{
	struct inode *node;
	struct inode_stat stat;
	int i;

	for (i = 0; pid_files[i].name != NULL; i++) {
		node = get_inode_by_index(parent, i);
		if (node != NULL)
			continue;

		make_stat(&stat, slot, i);

		node = add_inode(parent, pid_files[i].name, i, &stat,
		    (index_t)0, (cbdata_t)0);

		if (node == NULL)
			out_of_inodes();
	}
}
コード例 #15
0
ファイル: fuser.c プロジェクト: BezTebya/busybox-w32
static void scan_proc_net(const char *path, unsigned port)
{
	char line[MAX_LINE + 1];
	long long uint64_inode;
	unsigned tmp_port;
	FILE *f;
	struct stat st;
	int fd;

	/* find socket dev */
	st.st_dev = 0;
	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd >= 0) {
		fstat(fd, &st);
		close(fd);
	}

	f = fopen_for_read(path);
	if (!f)
		return;

	while (fgets(line, MAX_LINE, f)) {
		char addr[68];
		if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
				"%*x:%*x %*x %*d %*d %llu",
				addr, &tmp_port, &uint64_inode) == 3
		) {
			int len = strlen(addr);
			if (len == 8 && (option_mask32 & OPT_IP6))
				continue;
			if (len > 8 && (option_mask32 & OPT_IP4))
				continue;
			if (tmp_port == port) {
				st.st_ino = uint64_inode;
				add_inode(&st);
			}
		}
	}
	fclose(f);
}
コード例 #16
0
ファイル: fuser.c プロジェクト: Jeanlst/Onlive-Source-Backup
static inode_list *scan_proc_net(const char *proto,
				unsigned port, inode_list *ilist)
{
	char path[20], line[MAX_LINE + 1];
	ino_t tmp_inode;
	dev_t tmp_dev;
	long long uint64_inode;
	unsigned tmp_port;
	FILE *f;

	tmp_dev = find_socket_dev();

	sprintf(path, "/proc/net/%s", proto);
	f = fopen_for_read(path);
	if (!f)
		return ilist;

	while (fgets(line, MAX_LINE, f)) {
		char addr[68];
		if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
				"%*x:%*x %*x %*d %*d %llu",
				addr, &tmp_port, &uint64_inode) == 3
		) {
			int len = strlen(addr);
			if (len == 8 && (option_mask32 & OPT_IP6))
				continue;
			if (len > 8 && (option_mask32 & OPT_IP4))
				continue;
			if (tmp_port == port) {
				tmp_inode = uint64_inode;
				ilist = add_inode(ilist, tmp_dev, tmp_inode);
			}
		}
	}
	fclose(f);
	return ilist;
}
コード例 #17
0
ファイル: rssfs.c プロジェクト: thomasbrus/besturingssystemen
/* init_hook is called when the file system is mounted. At this point, VTreeFS has initialized itself, and it is possible to add inodes to the tree. */
static void initialize_hook(void) {
  int i = 0;
  // This integer tracks the number of RSS entries in order to prevent mutiple files with the same name in the same directory.
  int no_guid = 0;
  struct inode_stat file_stat;

  // Setting the variables in order to create a new file.
  file_stat.mode = S_IFREG | 0444;
  file_stat.uid = 0;
  file_stat.gid = 0;
  file_stat.size = 0;
  file_stat.dev = NO_DEV;

  for (i = 0; my_rss_body->item[i]; i++) {
    char *title_with_slash;
    char filename[16];
    if (strcmp(my_rss_body->item[i]->guid, "") == 0){
      // The RSS entry does not have a guid attribute.
      char index[5];
      // Convert no_guid to a string.
      sprintf (index, "%d", no_guid);
      strcpy(filename, "no_guid_");
      strcat(filename, index);
      strcat(filename, ".json");
      no_guid ++;
    } else {
      title_with_slash = strrchr(my_rss_body->item[i]->guid, '/');

      strncpy(filename, title_with_slash + 1, 10);
      filename[10] = 0;
      strcat (filename, ".json");
    }

    add_inode(get_inode_by_date(my_rss_body->item[i]->pubDate), filename, NO_INDEX, &file_stat, 0, (cbdata_t) i);
  }
}
コード例 #18
0
ファイル: tree.c プロジェクト: josepedrazap/trabajo2
/*
 * Regenerate the set of PID directories in the root directory of the file
 * system.  Add new directories and delete old directories as appropriate;
 * leave unchanged those that should remain the same.
 */
static void
construct_pid_dirs(void)
{
	/*
	 * We have to make two passes.  Otherwise, we would trigger a vtreefs
	 * assert when we add an entry for a PID before deleting the previous
	 * entry for that PID.  While rare, such rapid PID reuse does occur in
	 * practice.
	 */
	struct inode *root, *node;
	struct inode_stat stat;
	char name[PNAME_MAX+1];
	pid_t pid;
	int i;

	root = get_root_inode();

	/* First pass: delete old entries. */
	for (i = 0; i < NR_PROCS + NR_TASKS; i++) {
		/* Do we already have an inode associated with this slot? */
		node = get_inode_by_index(root, i);
		if (node == NULL)
			continue;

		/*
		 * If the process slot is not in use, delete the associated
		 * inode.
		 */
		if (!slot_in_use(i)) {
			delete_inode(node);

			continue;
		}

		/* Otherwise, get the process ID. */
		if (i < NR_TASKS)
			pid = (pid_t)(i - NR_TASKS);
		else
			pid = mproc[i - NR_TASKS].mp_pid;

		/*
		 * If there is an old entry, see if the pid matches the current
		 * entry, and the owner is still the same.  Otherwise, delete
		 * the old entry first.  We reconstruct the entire subtree even
		 * if only the owner changed, for security reasons: if a
		 * process could keep open a file or directory across the owner
		 * change, it might be able to access information it shouldn't.
		 */
		if (pid != (pid_t)get_inode_cbdata(node) ||
		    !check_owner(node, i))
			delete_inode(node);
	}

	/* Second pass: add new entries. */
	for (i = 0; i < NR_PROCS + NR_TASKS; i++) {
		/* If the process slot is not in use, skip this slot. */
		if (!slot_in_use(i))
			continue;

		/*
		 * If we have an inode associated with this slot, we have
		 * already checked it to be up-to-date above.
		 */
		if (get_inode_by_index(root, i) != NULL)
			continue;

		/* Get the process ID. */
		if (i < NR_TASKS)
			pid = (pid_t)(i - NR_TASKS);
		else
			pid = mproc[i - NR_TASKS].mp_pid;

		/* Add the entry for the process slot. */
		snprintf(name, PNAME_MAX + 1, "%d", pid);

		make_stat(&stat, i, NO_INDEX);

		node = add_inode(root, name, i, &stat, nr_pid_entries,
		    (cbdata_t)pid);

		if (node == NULL)
			out_of_inodes();
	}
}
コード例 #19
0
ファイル: fuser.c プロジェクト: Jeanlst/Onlive-Source-Backup
int fuser_main(int argc UNUSED_PARAM, char **argv)
{
	pid_list *plist;
	inode_list *ilist;
	char **pp;
	dev_t dev;
	ino_t inode;
	unsigned port;
	int opt;
	int success;
	int killsig;
/*
fuser [options] FILEs or PORT/PROTOs
Find processes which use FILEs or PORTs
        -m      Find processes which use same fs as FILEs
        -4      Search only IPv4 space
        -6      Search only IPv6 space
        -s      Silent: just exit with 0 if any processes are found
        -k      Kill found processes (otherwise display PIDs)
        -SIGNAL Signal to send (default: TERM)
*/
	/* Handle -SIGNAL. Oh my... */
	killsig = SIGTERM;
	pp = argv;
	while (*++pp) {
		char *arg = *pp;
		if (arg[0] != '-')
			continue;
		if (arg[1] == '-' && arg[2] == '\0') /* "--" */
			break;
		if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
			continue; /* it's "-4" or "-6" */
		opt = get_signum(&arg[1]);
		if (opt < 0)
			continue;
		/* "-SIGNAL" option found. Remove it and bail out */
		killsig = opt;
		do {
			pp[0] = arg = pp[1];
			pp++;
		} while (arg);
		break;
	}

	opt = getopt32(argv, OPTION_STRING);
	argv += optind;

	ilist = NULL;
	pp = argv;
	while (*pp) {
		char *proto = parse_net_arg(*pp, &port);
		if (proto) { /* PORT/PROTO */
			ilist = scan_proc_net(proto, port, ilist);
			free(proto);
		} else { /* FILE */
			if (!file_to_dev_inode(*pp, &dev, &inode))
				bb_perror_msg_and_die("can't open %s", *pp);
			ilist = add_inode(ilist, dev, inode);
		}
		pp++;
	}

	plist = scan_proc_pids(ilist); /* changes dir to "/proc" */

	if (!plist)
		return EXIT_FAILURE;
	success = 1;
	if (opt & OPT_KILL) {
		success = kill_pid_list(plist, killsig);
	} else if (!(opt & OPT_SILENT)) {
		success = print_pid_list(plist);
	}
	return (success != 1); /* 0 == success */
}
コード例 #20
0
ファイル: traverse.c プロジェクト: paulmadore/ccrypt
/* this function is called to act on a file in overwrite mode. */
static void action_overwrite(char *infile, char *outfile) {
  int st;
  struct stat buf;
  int do_chmod = 0;
  int r;
  int fd;
  int save_errno;
  
  /* read file attributes */
  st = stat(infile, &buf);
  if (st) {
    fprintf(stderr, "%s: %s: %s\n", cmd.name, infile, strerror(errno));
    io_errors++;
    return;
  }

  /* check whether this file is write protected */
  if ((buf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) {
    /* file is write-protected. In this case, we prompt the user to
       see if they want to operate on it anyway. Or if they give the
       "-f" option, we just do it without asking. */
    if (!cmd.force) {
      fprintf(stderr, "%s: %s write-protected file %s (y or n)? ", cmd.name, cmd.mode == ENCRYPT ? "encrypt" : cmd.mode == KEYCHANGE ? "perform keychange on" : "decrypt", infile);
      fflush(stderr);
      if (prompt()==0) {
	fprintf(stderr, "Not changed.\n");
	add_inode(buf.st_ino, buf.st_dev, 0);
	return;
      }
    }
    /* we will attempt to change the mode just before encrypting it. */
    do_chmod = 1;
  }
  
  /* check whether this inode was already handled under another filename */
  r = known_inode(buf.st_ino, buf.st_dev);
  if (r != -1 && cmd.verbose>0) {
    fprintf(stderr, "Already visited inode %s.\n", infile);
  }
  if (r == 0) {
    /* previous action on this inode failed - do nothing */
    return;
  } else if (r == 1) {
    /* previous action on this inode succeeded - rename only */
    goto rename;
  }
  
  /* act on this inode now */
  if (buf.st_nlink>1 && cmd.verbose>=0) {
    fprintf(stderr, "%s: warning: %s has %d links\n", cmd.name, 
	    infile, (int)buf.st_nlink);
    hardlink_warnings++;
  }
  if (do_chmod) {
    chmod(infile, buf.st_mode | S_IWUSR);
  }
  
  /* open file */
#ifdef __CYGWIN__
  fd = open(infile, O_RDWR | O_BINARY);
#else
  fd = open(infile, O_RDWR);
#endif
  if (fd == -1) {
    /* could not open file. */
    fprintf(stderr, "%s: %s: %s\n", cmd.name, infile, strerror(errno));
    io_errors++;
    add_inode(buf.st_ino, buf.st_dev, 0);
    return;
  }
  
  /* set local signal handler for SIGINT */
  signal(SIGINT, sigint_overwrite);

  /* crypt */
  switch (cmd.mode) {   /* note: can't be CAT or UNIXCRYPT */

  case ENCRYPT: default:
    if (cmd.verbose>0) {
      fprintf(stderr, "Encrypting %s\n", infile);
    }
    r = ccencrypt_file(fd, cmd.keyword);
    break;
    
  case DECRYPT:
    if (cmd.verbose>0) {
      fprintf(stderr, "Decrypting %s\n", infile);
    }
    r = ccdecrypt_file(fd, cmd.keyword);
    break;
    
  case KEYCHANGE:
    if (cmd.verbose>0) {
      fprintf(stderr, "Changing key for %s\n", infile);
    }
    r = cckeychange_file(fd, cmd.keyword, cmd.keyword2);
    break;
    
  }    
  save_errno = errno;
  
  /* restore the original file attributes for this file descriptor. 
     Ignore failures silently */
  fchown(fd, buf.st_uid, buf.st_gid);
  fchmod(fd, buf.st_mode);

  /* close file */
  close(fd);

  /* now restore original modtime */
  {
    struct utimbuf ut;
    ut.actime = buf.st_atime;
    ut.modtime = buf.st_mtime;
    
    utime(infile, &ut);
  }
  
  /* restore default signal handler */
  signal(SIGINT, SIG_DFL);

  errno = save_errno;
  if (r==-2 && (ccrypt_errno == CCRYPT_EFORMAT || ccrypt_errno == CCRYPT_EMISMATCH)) {
    fprintf(stderr, "%s: %s: %s -- unchanged\n", cmd.name, infile, ccrypt_error(r));
    key_errors++;
    add_inode(buf.st_ino, buf.st_dev, 0);
    return;
  } else if (r==-2 || r==-1) {
    fprintf(stderr, "%s: %s: %s\n", cmd.name, infile, ccrypt_error(r));
    exit(3);
  } else {
    add_inode(buf.st_ino, buf.st_dev, 1);
  }

 rename:
  /* rename file if necessary */
  if (strcmp(infile, outfile)) {
    r = rename(infile, outfile);
    if (r) {
      fprintf(stderr, "%s: could not rename %s as %s: %s\n", cmd.name, 
	      infile, outfile, strerror(errno));
      io_errors++;
    }
  }
  
  if (sigint_flag) {  /* SIGINT received while crypting - delayed exit */
    exit(6);
  }
  return;
}
コード例 #21
0
void
find_net6_sockets(struct inode_list **ino_list,
		  struct ip6_connections *conn_list, const char *protocol,
		  const dev_t netdev)
{
	FILE *fp;
	char pathname[200], line[BUFSIZ];
	unsigned long loc_port, rmt_port;
	struct in6_addr rmt_addr;
	unsigned int tmp_addr[4];
	char rmt_addr6str[INET6_ADDRSTRLEN];
	struct ip6_connections *head, *tmpptr, *tail;
	struct ip6_connections *conn_tmp;
	unsigned long scanned_inode;
	ino_t inode;

	head = tmpptr = tail = NULL;

	if (snprintf(pathname, 200, "/proc/net/%s6", protocol) < 0)
		return;

	if ((fp = fopen(pathname, "r")) == NULL) {
#ifdef DEBUG
		printf("Cannot open protocol file \"%s\": %s\n", pathname,
		       strerror(errno));
#endif				/* DEBUG */
		return;
	}
	while (fgets(line, BUFSIZ, fp) != NULL) {
		if (sscanf
		    (line,
		     "%*u: %*x:%lx %08x%08x%08x%08x:%lx %*x %*x:%*x %*x:%*x %*x %*d %*d %lu",
		     &loc_port, &(tmp_addr[0]), &(tmp_addr[1]), &(tmp_addr[2]),
		     &(tmp_addr[3]), &rmt_port, &scanned_inode) != 7)
			continue;
		inode = scanned_inode;
		rmt_addr.s6_addr32[0] = tmp_addr[0];
		rmt_addr.s6_addr32[1] = tmp_addr[1];
		rmt_addr.s6_addr32[2] = tmp_addr[2];
		rmt_addr.s6_addr32[3] = tmp_addr[3];
		inet_ntop(AF_INET6, &rmt_addr, rmt_addr6str, INET6_ADDRSTRLEN);
#ifdef DEBUG
		printf("Found IPv6 %ld with %s:%ld\n", loc_port, rmt_addr6str,
		       rmt_port);
#endif				/* DEBUG */
		for (conn_tmp = conn_list; conn_tmp != NULL;
		     conn_tmp = conn_tmp->next) {
			inet_ntop(AF_INET6, &conn_tmp->rmt_address,
				  rmt_addr6str, INET6_ADDRSTRLEN);
#ifdef DEBUG
			printf("  Comparing with *.%lu %s:%lu ...\n",
			       conn_tmp->lcl_port, rmt_addr6str,
			       conn_tmp->rmt_port);
#endif				/* DEBUG */
			if ((conn_tmp->lcl_port == 0
			     || conn_tmp->lcl_port == loc_port)
			    && (conn_tmp->rmt_port == 0
				|| conn_tmp->rmt_port == rmt_port)
			    &&
			    (memcmp(&(conn_tmp->rmt_address), &in6addr_any, 16)
			     == 0
			     ||
			     (memcmp(&(conn_tmp->rmt_address), &(rmt_addr), 16)
			      == 0))) {
				add_inode(ino_list, conn_tmp->name, netdev,
					  inode);
			}
		}
	}
}
コード例 #22
0
ファイル: fuser.c プロジェクト: oe-alliance/ofgwrite
int fuser_main(int argc UNUSED_PARAM, char **argv)
{
	// changed for ofgwrite
	applet_name = argv[0];
	char **pp;

	INIT_G();

	/* Handle -SIGNAL. Oh my... */
	pp = argv;
	while (*++pp) {
		int sig;
		char *arg = *pp;

		if (arg[0] != '-')
			continue;
		if (arg[1] == '-' && arg[2] == '\0') /* "--" */
			break;
		if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
			continue; /* it's "-4" or "-6" */
		sig = get_signum(&arg[1]);
		if (sig < 0)
			continue;
		/* "-SIGNAL" option found. Remove it and bail out */
		G.killsig = sig;
		do {
			pp[0] = arg = pp[1];
			pp++;
		} while (arg);
		break;
	}

	opt_complementary = "-1"; /* at least one param */
	getopt32(argv, OPTION_STRING);
	argv += optind;

	pp = argv;
	while (*pp) {
		/* parse net arg */
		unsigned port;
		char path[sizeof("/proc/net/TCP6")];

		strcpy(path, "/proc/net/");
		if (sscanf(*pp, "%u/%4s", &port, path + sizeof("/proc/net/")-1) == 2
		 && access(path, R_OK) == 0
		) {
			/* PORT/PROTO */
			scan_proc_net_or_maps(path, port);
		} else {
			/* FILE */
			struct stat statbuf;
			xstat(*pp, &statbuf);
			add_inode(&statbuf);
		}
		pp++;
	}

	if (scan_recursive("/proc")) {
		if (!(option_mask32 & OPT_SILENT))
			bb_putchar('\n');
		return G.kill_failed;
	}

	return EXIT_FAILURE;
}
コード例 #23
0
ファイル: fuser.c プロジェクト: BezTebya/busybox-w32
int fuser_main(int argc UNUSED_PARAM, char **argv)
{
	pid_list *plist;
	pid_t mypid;
	char **pp;
	struct stat st;
	unsigned port;
	int opt;
	int exitcode;
	int killsig;
/*
fuser [OPTIONS] FILE or PORT/PROTO
Find processes which use FILEs or PORTs
        -m      Find processes which use same fs as FILEs
        -4      Search only IPv4 space
        -6      Search only IPv6 space
        -s      Don't display PIDs
        -k      Kill found processes
        -SIGNAL Signal to send (default: KILL)
*/
	/* Handle -SIGNAL. Oh my... */
	killsig = SIGKILL; /* yes, the default is not SIGTERM */
	pp = argv;
	while (*++pp) {
		char *arg = *pp;
		if (arg[0] != '-')
			continue;
		if (arg[1] == '-' && arg[2] == '\0') /* "--" */
			break;
		if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
			continue; /* it's "-4" or "-6" */
		opt = get_signum(&arg[1]);
		if (opt < 0)
			continue;
		/* "-SIGNAL" option found. Remove it and bail out */
		killsig = opt;
		do {
			pp[0] = arg = pp[1];
			pp++;
		} while (arg);
		break;
	}

	opt_complementary = "-1"; /* at least one param */
	opt = getopt32(argv, OPTION_STRING);
	argv += optind;

	pp = argv;
	while (*pp) {
		/* parse net arg */
		char path[20], tproto[5];
		if (sscanf(*pp, "%u/%4s", &port, tproto) != 2)
			goto file;
		sprintf(path, "/proc/net/%s", tproto);
		if (access(path, R_OK) != 0) { /* PORT/PROTO */
			scan_proc_net(path, port);
		} else { /* FILE */
 file:
			xstat(*pp, &st);
			add_inode(&st);
		}
		pp++;
	}

	scan_proc_pids(); /* changes dir to "/proc" */

	mypid = getpid();
	plist = G.pid_list_head;
	while (1) {
		if (!plist)
			return EXIT_FAILURE;
		if (plist->pid != mypid)
			break;
		plist = plist->next;
	}

	exitcode = EXIT_SUCCESS;
	do {
		if (plist->pid != mypid) {
			if (opt & OPT_KILL) {
				if (kill(plist->pid, killsig) != 0) {
					bb_perror_msg("kill pid %u", (unsigned)plist->pid);
					exitcode = EXIT_FAILURE;
				}
			}
			if (!(opt & OPT_SILENT)) {
				printf("%u ", (unsigned)plist->pid);
			}
		}
		plist = plist->next;
	} while (plist);

	if (!(opt & (OPT_SILENT))) {
		bb_putchar('\n');
	}

	return exitcode;
}
コード例 #24
0
ファイル: fuser.c プロジェクト: oe-alliance/ofgwrite
static smallint scan_proc_net_or_maps(const char *path, unsigned port)
{
	FILE *f;
	char line[MAX_LINE + 1], addr[68];
	int major, minor, r;
	long long uint64_inode;
	unsigned tmp_port;
	smallint retval;
	struct stat statbuf;
	const char *fmt;
	void *f*g, *sag;

	f = fopen_for_read(path);
	if (!f)
		return 0;

	if (G.recursion_depth == PROC_NET) {
		int fd;

		/* find socket dev */
		statbuf.st_dev = 0;
		fd = socket(AF_INET, SOCK_DGRAM, 0);
		if (fd >= 0) {
			fstat(fd, &statbuf);
			close(fd);
		}

		fmt = "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x "
			"%*x:%*x %*x:%*x %*x %*d %*d %llu";
		f*g = addr;
		sag = &tmp_port;
	} else {
		fmt = "%*s %*s %*s %x:%x %llu";
		f*g = &major;
		sag = &minor;
	}

	retval = 0;
	while (fgets(line, MAX_LINE, f)) {
		r = sscanf(line, fmt, f*g, sag, &uint64_inode);
		if (r != 3)
			continue;

		statbuf.st_ino = uint64_inode;
		if (G.recursion_depth == PROC_NET) {
			r = strlen(addr);
			if (r == 8 && (option_mask32 & OPT_IP6))
				continue;
			if (r > 8 && (option_mask32 & OPT_IP4))
				continue;
			if (tmp_port == port)
				add_inode(&statbuf);
		} else {
			if (major != 0 && minor != 0 && statbuf.st_ino != 0) {
				statbuf.st_dev = makedev(major, minor);
				retval = search_dev_inode(&statbuf);
				if (retval)
					break;
			}
		}
	}
	fclose(f);

	return retval;
}
コード例 #25
0
ファイル: copyout.c プロジェクト: pinard/paxutils
static void
copy_out_one_file (int handle, dynamic_string *input_name,
		   struct stat *stat_info)
{
  struct new_cpio_header header;
  int input_handle;		/* source file descriptor */
  char *p;

  /* Set values in output header.  */
  header.c_magic = 070707;
  header.c_dev_maj = major (stat_info->st_dev);
  header.c_dev_min = minor (stat_info->st_dev);
  header.c_ino = stat_info->st_ino;
#if DOSWIN
  /* DJGPP doesn't support st_rdev.  Repair that.  */
  stat_info->st_rdev = stat_info->st_dev;
#endif
  /* For POSIX systems that don't define the S_IF macros, we can't assume that
     S_ISfoo means the standard Unix S_IFfoo bit(s) are set.  So do it
     manually, with a different name.  Bleah.  */
  header.c_mode = (stat_info->st_mode & 07777);
  if (S_ISREG (stat_info->st_mode))
    header.c_mode |= CP_IFREG;
  else if (S_ISDIR (stat_info->st_mode))
    header.c_mode |= CP_IFDIR;
#ifdef S_ISBLK
  else if (S_ISBLK (stat_info->st_mode))
    header.c_mode |= CP_IFBLK;
#endif
#ifdef S_ISCHR
  else if (S_ISCHR (stat_info->st_mode))
    header.c_mode |= CP_IFCHR;
#endif
#ifdef S_ISFIFO
  else if (S_ISFIFO (stat_info->st_mode))
    header.c_mode |= CP_IFIFO;
#endif
#ifdef S_ISLNK
  else if (S_ISLNK (stat_info->st_mode))
    header.c_mode |= CP_IFLNK;
#endif
#ifdef S_ISSOCK
  else if (S_ISSOCK (stat_info->st_mode))
    header.c_mode |= CP_IFSOCK;
#endif
#ifdef S_ISNWK
  else if (S_ISNWK (stat_info->st_mode))
    header.c_mode |= CP_IFNWK;
#endif
  header.c_uid = stat_info->st_uid;
  header.c_gid = stat_info->st_gid;
  header.c_nlink = stat_info->st_nlink;
  header.c_rdev_maj = major (stat_info->st_rdev);
  header.c_rdev_min = minor (stat_info->st_rdev);
  header.c_mtime = (stat_info->st_mtime < 0) ? 0 :stat_info->st_mtime;
  header.c_filesize = stat_info->st_size;
  header.c_chksum = 0;
  header.c_tar_linkname = NULL;

  /* Handle HPUX CDF files.  */
  possibly_munge_cdf_directory_name (input_name->string, &header);

  if ((*name_too_long) (header.c_name))
    {
      error (0, 0, _("%s: file name too long"), header.c_name);
      return;
    }

  /* FIXME: there is a memory leak here, between this and the HPUX stuff
     above.  */
  header.c_name = possibly_rename_file (header.c_name);

  /* Copy the named file to the output.  */
  switch (header.c_mode & CP_IFMT)
    {
    case CP_IFREG:
#ifndef __MSDOS__
      if (archive_format == V7_FORMAT || archive_format == POSIX_FORMAT
	  || archive_format == GNUTAR_FORMAT)
	{
	  char *otherfile;
	  if ((otherfile = find_inode_file (header.c_ino,
					    header.c_dev_maj,
					    header.c_dev_min)))
	    {
	      header.c_tar_linkname = otherfile;
	      (*header_writer) (&header, handle);
	      break;
	    }
	}
      if ((archive_format == NEW_ASCII_FORMAT
	   || archive_format == CRC_ASCII_FORMAT)
	  && header.c_nlink > 1)
	if (last_link (&header) )
	  writeout_other_defers (&header, handle);
	else
	  {
	    add_link_defer (&header);
	    break;
	  }
#endif
      input_handle = open (input_name->string,
			  O_RDONLY | O_BINARY, 0);
      if (input_handle < 0)
	{
	  error (0, errno, "%s", input_name->string);
	  return;
	}

      if (archive_format == CRC_ASCII_FORMAT)
	header.c_chksum = read_for_checksum (input_handle,
					     header.c_filesize,
					     input_name->string);

      (*header_writer) (&header, handle);
      copy_files_disk_to_tape (input_handle, handle,
			       header.c_filesize,
			       input_name->string);

#ifndef __MSDOS__
      if (archive_format == V7_FORMAT
	  || archive_format == POSIX_FORMAT
	  || archive_format == GNUTAR_FORMAT)
	add_inode (header.c_ino, header.c_name,
		   header.c_dev_maj, header.c_dev_min);
#endif

      tape_pad_output (handle, header.c_filesize);

      if (close (input_handle) < 0)
	error (0, errno, "%s", input_name->string);
      if (reset_access_time_option)
	{
	  struct utimbuf times;

	  /* Initialize this in case it has members we don't
	     know to set.  */
	  memset (&times, 0, sizeof (struct utimbuf));
	  times.actime = stat_info->st_atime;
	  times.modtime = stat_info->st_mtime;
	  /* Silently ignore EROFS because reading the file won't have upset
	     its timestamp if it's on a read-only filesystem.  */
	  if (utime (header.c_name, &times) < 0 && errno != EROFS)
	    error (0, errno, _("%s: error resetting file access time"),
		   header.c_name);
	}
      break;

    case CP_IFDIR:
      header.c_filesize = 0;
      (*header_writer) (&header, handle);
      break;

#ifndef __MSDOS__
    case CP_IFCHR:
    case CP_IFBLK:
#ifdef CP_IFSOCK
    case CP_IFSOCK:
#endif
#ifdef CP_IFIFO
    case CP_IFIFO:
#endif
      if (archive_format == V7_FORMAT)
	{
	  error (0, 0, _("%s not dumped: not a regular file"), header.c_name);
	  return;
	}
      else if (archive_format == POSIX_FORMAT
	       || archive_format == GNUTAR_FORMAT)
	{
	  char *otherfile
	    = find_inode_file (header.c_ino,
			       header.c_dev_maj, header.c_dev_min);

	  if (otherfile)
	    {
	      /* This file is linked to another file already in the archive,
		 so write it out as a hard link.  */
	      header.c_mode = (stat_info->st_mode & 07777);
	      header.c_mode |= CP_IFREG;
	      header.c_tar_linkname = otherfile;
	      (*header_writer) (&header, handle);
	      break;
	    }
	  add_inode (header.c_ino, header.c_name,
		     header.c_dev_maj, header.c_dev_min);
	}
      header.c_filesize = 0;
      (*header_writer) (&header, handle);
      break;
#endif

#ifdef CP_IFLNK
    case CP_IFLNK:
      {
	char *link_name = (char *) xmalloc (stat_info->st_size + 1);
	int link_size = readlink (input_name->string, link_name,
				  stat_info->st_size);

	if (link_size < 0)
	  {
	    error (0, errno, "%s", input_name->string);
	    free (link_name);
	    return;
	  }
	header.c_filesize = link_size;
	if (archive_format == V7_FORMAT
	    || archive_format == POSIX_FORMAT
	    || archive_format == GNUTAR_FORMAT)
	  {
	    /* FIXME: tar can do long symlinks.  */
	    if (link_size + 1 > 100)
	      error (0, 0, _("%s: symbolic link too long"),
		     header.c_name);
	    else
	      {
		link_name[link_size] = '\0';
		header.c_tar_linkname = link_name;
		(*header_writer) (&header, handle);
	      }
	  }
	else
	  {
	    (*header_writer) (&header, handle);
	    tape_buffered_write (link_name, handle, link_size);
	    tape_pad_output (handle, link_size);
	  }
	free (link_name);
      }
      break;
#endif

    default:
      error (0, 0, _("%s: unknown file type"), input_name->string);
    }

  /* FIXME shouldn't do this for each file.  Should maintain a dstring
     somewhere.  */
  free (header.c_name);
  header.c_name = NULL;
}