Ejemplo n.º 1
0
void
print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
{
	char    ctime_buf[100], otime_buf[100];

	cvt_time(ksemaptr->u.sem_otime, otime_buf);
	cvt_time(ksemaptr->u.sem_ctime, ctime_buf);

	printf("s %12d %12d %s %-8s %-8s",
	    IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
	    (int)ksemaptr->u.sem_perm.key,
	    fmt_perm(ksemaptr->u.sem_perm.mode),
	    user_from_uid(ksemaptr->u.sem_perm.uid, 0),
	    group_from_gid(ksemaptr->u.sem_perm.gid, 0));

	if (option & CREATOR)
		printf(" %-8s %-8s",
		    user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
		    group_from_gid(ksemaptr->u.sem_perm.cgid, 0));

	if (option & BIGGEST)
		printf(" %12d",
		    ksemaptr->u.sem_nsems);

	if (option & TIME)
		printf(" %s %s",
		    otime_buf,
		    ctime_buf);

	printf("\n");
}
Ejemplo n.º 2
0
Archivo: rm.c Proyecto: Open343/bitrig
static int
check(char *path, char *name, struct stat *sp)
{
	int ch, first;
	char modep[15];

	/*
	 * If it's not a symbolic link and it's unwritable and we're
	 * talking to a terminal, ask.  Symbolic links are excluded
	 * because their permissions are meaningless.  Check stdin_ok
	 * first because we may not have stat'ed the file.
	 */
	if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK) ||
	    errno != EACCES)
		return (1);
	strmode(sp->st_mode, modep);
	(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
	    modep + 1, modep[9] == ' ' ? "" : " ",
	    user_from_uid(sp->st_uid, 0),
	    group_from_gid(sp->st_gid, 0), path);
	(void)fflush(stderr);

	first = ch = getchar();
	while (ch != '\n' && ch != EOF)
		ch = getchar();
	return (first == 'y' || first == 'Y');
}
Ejemplo n.º 3
0
void
printlong(char *name,			/* filename to print */
	char *accpath,			/* current valid path to filename */
	struct stat *sb)		/* stat buffer */
{
	char modep[15];

	(void)printf("%7lu %6lld ", (u_long)sb->st_ino,
	    (long long)sb->st_blocks);
	(void)strmode(sb->st_mode, modep);
	(void)printf("%s %3lu %-*s %-*s ", modep, (unsigned long)sb->st_nlink,
	    LOGIN_NAME_MAX, user_from_uid(sb->st_uid, 0), LOGIN_NAME_MAX,
	    group_from_gid(sb->st_gid, 0));

	if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
		(void)printf("%3llu,%5llu ",
		    (unsigned long long)major(sb->st_rdev),
		    (unsigned long long)minor(sb->st_rdev));
	else
		(void)printf("%9lld ", (long long)sb->st_size);
	printtime(sb->st_mtime);
	(void)printf("%s", name);
	if (S_ISLNK(sb->st_mode))
		printlink(accpath);
	(void)putchar('\n');
}
Ejemplo n.º 4
0
static void
show_connlist(void) {
	void *p;
	int cnt;
	char buf[200];
	struct ncp_conn_stat *ncsp;

	printf("Active NCP connections:\n");
	p = ncp_conn_list();
	if (p == NULL) {
		printf("None\n");
		return;
	}
	printf(" refid server:user(connid), owner:group(mode), refs, <state>\n");
	cnt = *(int*)p;
	ncsp = (struct ncp_conn_stat*)(((int*)p)+1);
	while(cnt--) {
		printf("%6d %s:%s(%d), %s:%s(%o), %d, %s",
		    ncsp->connRef, ncsp->li.server,ncsp->user,ncsp->connid,
		    user_from_uid(ncsp->owner, 0), 
		    group_from_gid(ncsp->group, 0), 
		    ncsp->li.access_mode,
		    ncsp->ref_cnt,
		    ncp_printb(buf, ncsp->flags, conn_statenames));
		printf("\n");
		ncsp++;
	}
	free(p);
	printf("\n");
}
Ejemplo n.º 5
0
void
showpigs(void)
{
	int i, j, y, k;
	const char *uname, *pname;
	char pidname[30];

	if (pt == NULL)
		return;

	qsort(pt, nproc, sizeof (struct p_times), compar);
	y = 1;
	i = nproc;
	if (i > wnd->_maxy-1)
		i = wnd->_maxy-1;
	for (k = 0; i > 0 && pt[k].pt_pctcpu > 0.01; i--, y++, k++) {
		uname = user_from_uid(pt[k].pt_kp->ki_uid, 0);
		pname = pt[k].pt_kp->ki_comm;
		wmove(wnd, y, 0);
		wclrtoeol(wnd);
		mvwaddstr(wnd, y, 0, uname);
		snprintf(pidname, sizeof(pidname), "%10.10s", pname);
		mvwaddstr(wnd, y, 9, pidname);
		wmove(wnd, y, 20);
		for (j = pt[k].pt_pctcpu * 50 + 0.5; j > 0; j--)
			waddch(wnd, 'X');
	}
	wmove(wnd, y, 0); wclrtobot(wnd);
}
Ejemplo n.º 6
0
void
runame(const struct kinfo_proc *kp, VARENT *ve)
{
	VAR *v;

	v = ve->var;
	(void)printf("%-*s",
	    (int)v->width, user_from_uid(kp->p_ruid, 0));
}
Ejemplo n.º 7
0
void
print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
{
	char    atime_buf[100], dtime_buf[100], ctime_buf[100];

	cvt_time(kshmptr->u.shm_atime, atime_buf);
	cvt_time(kshmptr->u.shm_dtime, dtime_buf);
	cvt_time(kshmptr->u.shm_ctime, ctime_buf);

	printf("m %12d %12d %s %-8s %-8s",
	    IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
	    (int)kshmptr->u.shm_perm.key,
	    fmt_perm(kshmptr->u.shm_perm.mode),
	    user_from_uid(kshmptr->u.shm_perm.uid, 0),
	    group_from_gid(kshmptr->u.shm_perm.gid, 0));

	if (option & CREATOR)
		printf(" %-8s %-8s",
		    user_from_uid(kshmptr->u.shm_perm.cuid, 0),
		    group_from_gid(kshmptr->u.shm_perm.cgid, 0));

	if (option & OUTSTANDING)
		printf(" %12d",
		    kshmptr->u.shm_nattch);

	if (option & BIGGEST)
		printf(" %12zu",
		    kshmptr->u.shm_segsz);

	if (option & PID)
		printf(" %12d %12d",
		    kshmptr->u.shm_cpid,
		    kshmptr->u.shm_lpid);

	if (option & TIME)
		printf(" %s %s %s",
		    atime_buf,
		    dtime_buf,
		    ctime_buf);

	printf("\n");
}
Ejemplo n.º 8
0
void
print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
{
	char    stime_buf[100], rtime_buf[100], ctime_buf[100];

	cvt_time(kmsqptr->u.msg_stime, stime_buf);
	cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
	cvt_time(kmsqptr->u.msg_ctime, ctime_buf);

	printf("q %12d %12d %s %-8s %-8s",
	    IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
	    (int)kmsqptr->u.msg_perm.key,
	    fmt_perm(kmsqptr->u.msg_perm.mode),
	    user_from_uid(kmsqptr->u.msg_perm.uid, 0),
	    group_from_gid(kmsqptr->u.msg_perm.gid, 0));

	if (option & CREATOR)
		printf(" %-8s %-8s",
		    user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
		    group_from_gid(kmsqptr->u.msg_perm.cgid, 0));

	if (option & OUTSTANDING)
		printf(" %12lu %12lu",
		    kmsqptr->u.msg_cbytes,
		    kmsqptr->u.msg_qnum);

	if (option & BIGGEST)
		printf(" %20lu", kmsqptr->u.msg_qbytes);

	if (option & PID)
		printf(" %12d %12d",
		    kmsqptr->u.msg_lspid,
		    kmsqptr->u.msg_lrpid);

	if (option & TIME)
		printf(" %s %s %s",
		    stime_buf,
		    rtime_buf,
		    ctime_buf);

	printf("\n");
}
Ejemplo n.º 9
0
Archivo: rm.c Proyecto: dezelin/kBuild
static int
check(char *path, char *name, struct stat *sp)
{
	int ch, first;
	char modep[15], *flagsp;

	/* Check -i first. */
	if (iflag)
		(void)fprintf(stderr, "remove %s? ", path);
	else {
		/*
		 * If it's not a symbolic link and it's unwritable and we're
		 * talking to a terminal, ask.  Symbolic links are excluded
		 * because their permissions are meaningless.  Check stdin_ok
		 * first because we may not have stat'ed the file.
		 * Also skip this check if the -P option was specified because
		 * we will not be able to overwrite file contents and will
		 * barf later.
		 */
		if (!stdin_ok || S_ISLNK(sp->st_mode) || Pflag ||
		    (!access(name, W_OK) &&
#ifdef SF_APPEND
		    !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
		    (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))
#else
                    1)
#endif
                    )
			return (1);
		bsd_strmode(sp->st_mode, modep);
#ifdef SF_APPEND
		if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
			exit(err(1, "fflagstostr"));
		(void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
		              modep + 1, modep[9] == ' ' ? "" : " ",
		              user_from_uid(sp->st_uid, 0),
		              group_from_gid(sp->st_gid, 0),
		              *flagsp ? flagsp : "", *flagsp ? " " : "",
		              path);
		free(flagsp);
#else
		(void)flagsp;
		(void)fprintf(stderr, "override %s%s %d/%d for %s? ",
		              modep + 1, modep[9] == ' ' ? "" : " ",
		              sp->st_uid, sp->st_gid, path);
#endif
	}
	(void)fflush(stderr);

	first = ch = getchar();
	while (ch != '\n' && ch != EOF)
		ch = getchar();
	return (first == 'y' || first == 'Y');
}
Ejemplo n.º 10
0
static void
write_normal_file(const char *name, struct archive *archive,
    struct archive_entry_linkresolver *resolver,
    const char *owner, const char *group)
{
	char buf[16384];
	ssize_t buf_len;
	struct archive_entry *entry, *sparse_entry;
	struct stat st;

	if (lstat(name, &st) == -1)
		err(2, "lstat failed for file %s", name);

	entry = archive_entry_new();
	archive_entry_set_pathname(entry, name);
	archive_entry_copy_stat(entry, &st);

	if (owner != NULL) {
		uid_t uid;

		archive_entry_set_uname(entry, owner);
		if (uid_from_user(owner, &uid) == -1)
			errx(2, "user %s unknown", owner);
		archive_entry_set_uid(entry, uid);
	} else {
		archive_entry_set_uname(entry, user_from_uid(st.st_uid, 1));
	}

	if (group != NULL) {
		gid_t gid;

		archive_entry_set_gname(entry, group);
		if (gid_from_group(group, &gid) == -1)
			errx(2, "group %s unknown", group);
		archive_entry_set_gid(entry, gid);
	} else {
		archive_entry_set_gname(entry, group_from_gid(st.st_gid, 1));
	}

	if ((st.st_mode & S_IFMT) == S_IFLNK) {
		buf_len = readlink(name, buf, sizeof buf);
		if (buf_len < 0)
			err(2, "cannot read symlink %s", name);
		buf[buf_len] = '\0';
		archive_entry_set_symlink(entry, buf);
	}

	archive_entry_linkify(resolver, &entry, &sparse_entry);

	if (entry != NULL)
		write_entry(archive, entry);
	if (sparse_entry != NULL)
		write_entry(archive, sparse_entry);
}
Ejemplo n.º 11
0
/***** kill or nice a process */
static void hurt_proc(int tty, int uid, int pid, char *cmd){
  int failed;
  int saved_errno;
  char dn_buf[1000];
  dev_to_tty(dn_buf, 999, tty, pid, ABBREV_DEV);
  if(i_flag){
    char buf[8];
    fprintf(stderr, "%-8.8s %-8.8s %5d %-16.16s   ? ",
      (char*)dn_buf,user_from_uid(uid),pid,cmd
    );
    if(!fgets(buf,7,stdin)){
      printf("\n");
      exit(0);
    }
    if(*buf!='y' && *buf!='Y') return;
  }
  /* do the actual work */
  if(program==PROG_SKILL) failed=kill(pid,sig_or_pri);
  else                    failed=setpriority(PRIO_PROCESS,pid,sig_or_pri);
  saved_errno = errno;
  if(w_flag && failed){
    fprintf(stderr, "%-8.8s %-8.8s %5d %-16.16s   ",
      (char*)dn_buf,user_from_uid(uid),pid,cmd
    );
    errno = saved_errno;
    perror("");
    return;
  }
  if(i_flag) return;
  if(v_flag){
    printf("%-8.8s %-8.8s %5d %-16.16s\n",
      (char*)dn_buf,user_from_uid(uid),pid,cmd
    );
    return;
  }
  if(n_flag){
   printf("%d\n",pid);
   return;
  }
}
Ejemplo n.º 12
0
void
usracct_print(void)
{
	DBT key, data;
	struct userinfo uistore, *ui = &uistore;
	double t;
	int rv;

	rv = DB_SEQ(usracct_db, &key, &data, R_FIRST);
	if (rv < 0)
		warn("retrieving user accounting stats");

	while (rv == 0) {
		memcpy(ui, data.data, sizeof(struct userinfo));

		printf("%-8s %9llu ",
		    user_from_uid(ui->ui_uid, 0),
		    (unsigned long long)ui->ui_calls);

		t = (double) (ui->ui_utime + ui->ui_stime) /
		    (double) AHZ;
		if (t < 0.0001)		/* kill divide by zero */
			t = 0.0001;

		printf("%12.2f%s ", t / 60.0, "cpu");

		/* ui->ui_calls is always != 0 */
		if (dflag)
			printf("%12llu%s",
			    (unsigned long long)(ui->ui_io / ui->ui_calls),
			    "avio");
		else
			printf("%12llu%s",
			    (unsigned long long)ui->ui_io, "tio");

		/* t is always >= 0.0001; see above */
		if (kflag)
			printf("%12llu%s", (unsigned long long)(ui->ui_mem / t),
			    "k");
		else
			printf("%12llu%s", (unsigned long long)ui->ui_mem,
			    "k*sec");

		printf("\n");

		rv = DB_SEQ(usracct_db, &key, &data, R_NEXT);
		if (rv < 0)
			warn("retrieving user accounting stats");
	}
}
Ejemplo n.º 13
0
check(char *path, char *name, struct stat *sp)
#endif
{
	int ch, first;
#ifdef __GNO__
       static
#endif
	char modep[15], flagsp[128];

	/* Check -i first. */
	if (iflag)
		(void)fprintf(stderr, "remove %s? ", path);
	else {
		/*
		 * If it's not a symbolic link and it's unwritable and we're
		 * talking to a terminal, ask.  Symbolic links are excluded
		 * because their permissions are meaningless.  Check stdin_ok
		 * first because we may not have stat'ed the file.
		 */
#ifndef __GNO__
		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
		    !access(name, W_OK) &&
		    !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
		    (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))
			return (1);
		strmode(sp->st_mode, modep);
		strcpy(flagsp, flags_to_string(sp->st_flags, NULL));
		if (*flagsp)
			strcat(flagsp, " ");
		(void)fprintf(stderr, "override %s%s%s/%s %sfor %s? ",
		    modep + 1, modep[9] == ' ' ? "" : " ",
		    user_from_uid(sp->st_uid, 0),
		    group_from_gid(sp->st_gid, 0),
		    *flagsp ? flagsp : "",
		    path);
#else
		if (!stdin_ok || !access(name,W_OK))
			return (1);
		(void)fprintf(stderr, "override protection for %s? ",
		    path);
#endif
	}
	(void)fflush(stderr);

	first = ch = getchar();
	while (ch != '\n' && ch != EOF)
		ch = getchar();
	return (first == 'y' || first == 'Y');
}
Ejemplo n.º 14
0
static void
print_shareinfo(struct smb_share_info *sip)
{
	char buf[200];

	iprintf(4, "Share:    %s", sip->sname);
	printf("(%s:%s) %o", user_from_uid(sip->uid, 0), 
	    group_from_gid(sip->gid, 0), sip->mode);
	printf("\n");
	if (!verbose)
		return;
	iprintf(8, "flags:    0x%04x %s\n", sip->flags,
	    smb_printb(buf, sip->flags, ss_flags));
	iprintf(8, "usecount: %d\n", sip->usecount);
}
Ejemplo n.º 15
0
/*
 * drwxr-xr-x    5 markus   markus       1024 Jan 13 18:39 .ssh
 */
char *
ls_file(const char *name, const struct stat *st, int remote, int si_units)
{
	int ulen, glen, sz = 0;
	struct tm *ltime = localtime(&st->st_mtime);
	char *user, *group;
	char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
	char sbuf[FMT_SCALED_STRSIZE];
	time_t now;


	strmode(st->st_mode, mode);
	if (!remote) {
		user = user_from_uid(st->st_uid, 0);
	} else {
		snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid);
		user = ubuf;
	}
	if (!remote) {
		group = group_from_gid(st->st_gid, 0);
	} else {
		snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid);
		group = gbuf;
	}
	if (ltime != NULL) {
		now = time(NULL);
		if (now - (365*24*60*60)/2 < st->st_mtime &&
		    now >= st->st_mtime)
			sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
		else
			sz = strftime(tbuf, sizeof tbuf, "%b %e  %Y", ltime);
	}
	if (sz == 0)
		tbuf[0] = '\0';
	ulen = MAX(strlen(user), 8);
	glen = MAX(strlen(group), 8);
	if (si_units) {
		fmt_scaled((long long)st->st_size, sbuf);
		snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode,
		    (u_int)st->st_nlink, ulen, user, glen, group,
		    sbuf, tbuf, name);
	} else {
		snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
		    (u_int)st->st_nlink, ulen, user, glen, group,
		    (unsigned long long)st->st_size, tbuf, name);
	}
	return xstrdup(buf);
}
Ejemplo n.º 16
0
int
requested(char *argv[], struct acct *acp)
{
	const char *p;

	do {
		p = user_from_uid(acp->ac_uid, 0);
		if (!strcmp(p, *argv))
			return (1);
		if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
			return (1);
		if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
			return (1);
	} while (*++argv);
	return (0);
}
Ejemplo n.º 17
0
static int
check(const char *path, const char *name, struct stat *sp)
{
    int ch, first;
    char modep[15], *flagsp;

    /* Check -i first. */
    if (iflag)
        (void)fprintf(stderr, "remove %s? ", path);
    else {
        /*
         * If it's not a symbolic link and it's unwritable and we're
         * talking to a terminal, ask.  Symbolic links are excluded
         * because their permissions are meaningless.  Check stdin_ok
         * first because we may not have stat'ed the file.
         */
        if (!stdin_ok || S_ISLNK(sp->st_mode) ||
                (!access(name, W_OK) &&
                 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
                 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
            return (1);
        strmode(sp->st_mode, modep);
        if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
            err(1, "fflagstostr");
        if (Pflag)
            errx(1,
                 "%s: -P was specified, but file is not writable",
                 path);
        (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
                      modep + 1, modep[9] == ' ' ? "" : " ",
                      user_from_uid(sp->st_uid, 0),
                      group_from_gid(sp->st_gid, 0),
                      *flagsp ? flagsp : "", *flagsp ? " " : "",
                      path);
        free(flagsp);
    }
    (void)fflush(stderr);

    first = ch = getchar();
    while (ch != '\n' && ch != EOF)
        ch = getchar();
    return (first == 'y' || first == 'Y');
}
Ejemplo n.º 18
0
void
printlong(char *name, char *accpath, struct stat *sb)
{
	char modep[15];

	(void)printf("%6lu %8"PRId64" ", (u_long) sb->st_ino, sb->st_blocks);
	(void)strmode(sb->st_mode, modep);
	(void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, MAXLOGNAME - 1,
	    user_from_uid(sb->st_uid, 0), MAXLOGNAME - 1,
	    group_from_gid(sb->st_gid, 0));

	if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
		(void)printf("%#8jx ", (uintmax_t)sb->st_rdev);
	else
		(void)printf("%8"PRId64" ", sb->st_size);
	printtime(sb->st_mtime);
	(void)printf("%s", name);
	if (S_ISLNK(sb->st_mode))
		printlink(accpath);
	(void)putchar('\n');
}
Ejemplo n.º 19
0
static void
print_vcinfo(struct smb_vc_info *vip)
{
	char buf[200];

	printf("VC: \\\\%s\\%s\n", vip->srvname, vip->vcname);
	printf("(%s:%s) %o", user_from_uid(vip->uid, 0), 
	    group_from_gid(vip->gid, 0), vip->mode);
	printf("\n");
	if (!verbose)
		return;
	iprintf(4, "state:    %s\n", iod_state[vip->iodstate]);
	iprintf(4, "flags:    0x%04x %s\n", vip->flags,
	    smb_printb(buf, vip->flags, vc_flags));
	iprintf(4, "usecount: %d\n", vip->usecount);
	iprintf(4, "dialect:  %d (%s)\n", vip->sopt.sv_proto, conn_proto[vip->sopt.sv_proto]);
	iprintf(4, "smode:    %d\n", vip->sopt.sv_sm);
	iprintf(4, "caps:     0x%04x %s\n", vip->sopt.sv_caps,
	    smb_printb(buf, vip->sopt.sv_caps, conn_caps));
	iprintf(4, "maxmux:   %d\n", vip->sopt.sv_maxmux);
	iprintf(4, "maxvcs:   %d\n", vip->sopt.sv_maxvcs);
}
Ejemplo n.º 20
0
Archivo: ls.c Proyecto: UNGLinux/Obase
void
printlong(char *name, char *accpath, struct stat *sb)
{
	char modep[15];

	(void)printf("%6u %4lld ", sb->st_ino, (long long)sb->st_blocks);
	(void)strmode(sb->st_mode, modep);
	(void)printf("%s %3u %-*.*s %-*.*s ", modep, sb->st_nlink, 
	    NAME_WIDTH, UT_NAMESIZE, user_from_uid(sb->st_uid, 0), 
	    NAME_WIDTH, UT_NAMESIZE, group_from_gid(sb->st_gid, 0));

	if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
		(void)printf("%3d, %3d ", major(sb->st_rdev),
		    minor(sb->st_rdev));
	else
		(void)printf("%8lld ", (long long)sb->st_size);
	printtime(sb->st_mtime);
	(void)printf("%s", name);
	if (S_ISLNK(sb->st_mode))
		printlink(accpath);
	(void)putchar('\n');
}
Ejemplo n.º 21
0
static int
check(char *path, char *name, struct stat *sp)
{
	int ch, first;
	char modep[15];

	/* Check -i first. */
	if (iflag)
		(void)fprintf(stderr, "remove '%s'? ", path);
	else {
		/*
		 * If it's not a symbolic link and it's unwritable and we're
		 * talking to a terminal, ask.  Symbolic links are excluded
		 * because their permissions are meaningless.  Check stdin_ok
		 * first because we may not have stat'ed the file.
		 */
		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
		    !(access(name, W_OK) && (errno != ETXTBSY)))
			return (1);
		strmode(sp->st_mode, modep);
		if (Pflag) {
			warnx(
			    "%s: -P was specified but file could not"
			    " be overwritten", path);
			return 0;
		}
		(void)fprintf(stderr, "override %s%s%s:%s for '%s'? ",
		    modep + 1, modep[9] == ' ' ? "" : " ",
		    user_from_uid(sp->st_uid, 0),
		    group_from_gid(sp->st_gid, 0), path);
	}
	(void)fflush(stderr);

	first = ch = getchar();
	while (ch != '\n' && ch != EOF)
		ch = getchar();
	return (first == 'y' || first == 'Y');
}
Ejemplo n.º 22
0
static void
update_ids(struct memory_file *file)
{
	if (file->owner != NULL) {
		uid_t uid;

		if (uid_from_user(file->owner, &uid) == -1)
			errx(2, "user %s unknown", file->owner);
		file->st.st_uid = uid;
	} else {
		file->owner = xstrdup(user_from_uid(file->st.st_uid, 1));
	}

	if (file->group != NULL) {
		gid_t gid;

		if (gid_from_group(file->group, &gid) == -1)
			errx(2, "group %s unknown", file->group);
		file->st.st_gid = gid;
	} else {
		file->group = xstrdup(group_from_gid(file->st.st_gid, 1));
	}
}
Ejemplo n.º 23
0
Archivo: mv.c Proyecto: antoineL/netbsd
static int
do_move(char *from, char *to)
{
	struct stat sb;
	char modep[15];

	/*
	 * (1)	If the destination path exists, the -f option is not specified
	 *	and either of the following conditions are true:
	 *
	 *	(a) The permissions of the destination path do not permit
	 *	    writing and the standard input is a terminal.
	 *	(b) The -i option is specified.
	 *
	 *	the mv utility shall write a prompt to standard error and
	 *	read a line from standard input.  If the response is not
	 *	affirmative, mv shall do nothing more with the current
	 *	source file...
	 */
	if (!fflg && !access(to, F_OK)) {
		int ask = 1;
		int ch;

		if (iflg) {
			if (access(from, F_OK)) {
				warn("rename %s", from);
				return (1);
			}
			(void)fprintf(stderr, "overwrite %s? ", to);
		} else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
			if (access(from, F_OK)) {
				warn("rename %s", from);
				return (1);
			}
			strmode(sb.st_mode, modep);
			(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
			    modep + 1, modep[9] == ' ' ? "" : " ",
			    user_from_uid(sb.st_uid, 0),
			    group_from_gid(sb.st_gid, 0), to);
		} else
			ask = 0;
		if (ask) {
			if ((ch = getchar()) != EOF && ch != '\n') {
				int ch2;
				while ((ch2 = getchar()) != EOF && ch2 != '\n')
					continue;
			}
			if (ch != 'y' && ch != 'Y')
				return (0);
		}
	}

	/*
	 * (2)	If rename() succeeds, mv shall do nothing more with the
	 *	current source file.  If it fails for any other reason than
	 *	EXDEV, mv shall write a diagnostic message to the standard
	 *	error and do nothing more with the current source file.
	 *
	 * (3)	If the destination path exists, and it is a file of type
	 *	directory and source_file is not a file of type directory,
	 *	or it is a file not of type directory, and source file is
	 *	a file of type directory, mv shall write a diagnostic
	 *	message to standard error, and do nothing more with the
	 *	current source file...
	 */
	if (!rename(from, to)) {
		if (vflg)
			printf("%s -> %s\n", from, to);
		return (0);
	}

	if (errno != EXDEV) {
		warn("rename %s to %s", from, to);
		return (1);
	}

	/*
	 * (4)	If the destination path exists, mv shall attempt to remove it.
	 *	If this fails for any reason, mv shall write a diagnostic
	 *	message to the standard error and do nothing more with the
	 *	current source file...
	 */
	if (!lstat(to, &sb)) {
		if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
			warn("can't remove %s", to);
			return (1);
		}
	}

	/*
	 * (5)	The file hierarchy rooted in source_file shall be duplicated
	 *	as a file hierarchy rooted in the destination path...
	 */
	if (lstat(from, &sb)) {
		warn("%s", from);
		return (1);
	}

	return (S_ISREG(sb.st_mode) ?
	    fastcopy(from, to, &sb) : copy(from, to));
}
Ejemplo n.º 24
0
/*
 * dump_nodes --
 *	dump the NODEs from `cur', based in the directory `dir'.
 *	if pathlast is none zero, print the path last, otherwise print
 *	it first.
 */
void
dump_nodes(const char *dir, NODE *root, int pathlast)
{
	NODE	*cur;
	char	path[MAXPATHLEN];
	const char *name;
	char	*str;
	char	*p, *q;

	for (cur = root; cur != NULL; cur = cur->next) {
		if (cur->type != F_DIR && !matchtags(cur))
			continue;

		if (snprintf(path, sizeof(path), "%s%s%s",
		    dir, *dir ? "/" : "", cur->name)
		    >= (int)sizeof(path))
			mtree_err("Pathname too long.");

		if (!pathlast)
			printf("%s", vispath(path));

#define MATCHFLAG(f)	((keys & (f)) && (cur->flags & (f)))
		if (MATCHFLAG(F_TYPE))
			appendfield(pathlast, "type=%s", nodetype(cur->type));
		if (MATCHFLAG(F_UID | F_UNAME)) {
			if (keys & F_UNAME &&
			    (name = user_from_uid(cur->st_uid, 1)) != NULL)
				appendfield(pathlast, "uname=%s", name);
			else
				appendfield(pathlast, "uid=%u", cur->st_uid);
		}
		if (MATCHFLAG(F_GID | F_GNAME)) {
			if (keys & F_GNAME &&
			    (name = group_from_gid(cur->st_gid, 1)) != NULL)
				appendfield(pathlast, "gname=%s", name);
			else
				appendfield(pathlast, "gid=%u", cur->st_gid);
		}
		if (MATCHFLAG(F_MODE))
			appendfield(pathlast, "mode=%#o", cur->st_mode);
		if (MATCHFLAG(F_DEV) &&
		    (cur->type == F_BLOCK || cur->type == F_CHAR))
			appendfield(pathlast, "device=%#llx", (long long)cur->st_rdev);
		if (MATCHFLAG(F_NLINK))
			appendfield(pathlast, "nlink=%d", cur->st_nlink);
		if (MATCHFLAG(F_SLINK))
			appendfield(pathlast, "link=%s", vispath(cur->slink));
		if (MATCHFLAG(F_SIZE))
			appendfield(pathlast, "size=%lld", (long long)cur->st_size);
		if (MATCHFLAG(F_TIME))
			appendfield(pathlast, "time=%lld.%09ld",
			    (long long)cur->st_mtimespec.tv_sec,
			    cur->st_mtimespec.tv_nsec);
		if (MATCHFLAG(F_CKSUM))
			appendfield(pathlast, "cksum=%lu", cur->cksum);
		if (MATCHFLAG(F_MD5))
			appendfield(pathlast, "%s=%s", MD5KEY, cur->md5digest);
		if (MATCHFLAG(F_RMD160))
			appendfield(pathlast, "%s=%s", RMD160KEY,
			    cur->rmd160digest);
		if (MATCHFLAG(F_SHA1))
			appendfield(pathlast, "%s=%s", SHA1KEY,
			    cur->sha1digest);
		if (MATCHFLAG(F_SHA256))
			appendfield(pathlast, "%s=%s", SHA256KEY,
			    cur->sha256digest);
		if (MATCHFLAG(F_SHA384))
			appendfield(pathlast, "%s=%s", SHA384KEY,
			    cur->sha384digest);
		if (MATCHFLAG(F_SHA512))
			appendfield(pathlast, "%s=%s", SHA512KEY,
			    cur->sha512digest);
		if (MATCHFLAG(F_FLAGS)) {
			str = flags_to_string(cur->st_flags, "none");
			appendfield(pathlast, "flags=%s", str);
			free(str);
		}
		if (MATCHFLAG(F_IGN))
			appendfield(pathlast, "ignore");
		if (MATCHFLAG(F_OPT))
			appendfield(pathlast, "optional");
		if (MATCHFLAG(F_TAGS)) {
			/* don't output leading or trailing commas */
			p = cur->tags;
			while (*p == ',')
				p++;
			q = p + strlen(p);
			while(q > p && q[-1] == ',')
				q--;
			appendfield(pathlast, "tags=%.*s", (int)(q - p), p);
		}
		puts(pathlast ? vispath(path) : "");

		if (cur->child)
			dump_nodes(path, cur->child, pathlast);
	}
}
Ejemplo n.º 25
0
int
do_move(char *from, char *to)
{
	struct stat sb, fsb;
	char modep[15];

	/* Source path must exist (symlink is OK). */
	if (lstat(from, &fsb)) {
		warn("%s", from);
		return (1);
	}

	/*
	 * (1)	If the destination path exists, the -f option is not specified
	 *	and either of the following conditions are true:
	 *
	 *	(a) The permissions of the destination path do not permit
	 *	    writing and the standard input is a terminal.
	 *	(b) The -i option is specified.
	 *
	 *	the mv utility shall write a prompt to standard error and
	 *	read a line from standard input.  If the response is not
	 *	affirmative, mv shall do nothing more with the current
	 *	source file...
	 */
	if (!fflg && !access(to, F_OK)) {
		int ask = 1;
		int ch, first;

		if (iflg && !access(from, F_OK)) {
			(void)fprintf(stderr, "overwrite %s? ", to);
		} else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
			strmode(sb.st_mode, modep);
			(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
			    modep + 1, modep[9] == ' ' ? "" : " ",
			    user_from_uid(sb.st_uid, 0),
			    group_from_gid(sb.st_gid, 0), to);
		} else
			ask = 0;
		if (ask) {
			first = ch = getchar();
			while (ch != '\n' && ch != EOF)
				ch = getchar();
			if (first != 'y' && first != 'Y')
				return (0);
		}
	}

	/*
	 * (2)	If rename() succeeds, mv shall do nothing more with the
	 *	current source file.  If it fails for any other reason than
	 *	EXDEV, mv shall write a diagnostic message to the standard
	 *	error and do nothing more with the current source file.
	 *
	 * (3)	If the destination path exists, and it is a file of type
	 *	directory and source_file is not a file of type directory,
	 *	or it is a file not of type directory, and source file is
	 *	a file of type directory, mv shall write a diagnostic
	 *	message to standard error, and do nothing more with the
	 *	current source file...
	 */
	if (!rename(from, to))
		return (0);

	if (errno != EXDEV) {
		warn("rename %s to %s", from, to);
		return (1);
	}

	/* Disallow moving a mount point. */
	if (S_ISDIR(fsb.st_mode)) {
		struct statfs sfs;
		char path[MAXPATHLEN];

		if (realpath(from, path) == NULL) {
			warnx("cannot resolve %s", from);
			return (1);
		}
		if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) {
			warnx("cannot rename a mount point");
			return (1);
		}
	}

	/*
	 * (4)	If the destination path exists, mv shall attempt to remove it.
	 *	If this fails for any reason, mv shall write a diagnostic
	 *	message to the standard error and do nothing more with the
	 *	current source file...
	 */
	if (!lstat(to, &sb)) {
		if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
			warn("can't remove %s", to);
			return (1);
		}
	}

	/*
	 * (5)	The file hierarchy rooted in source_file shall be duplicated
	 *	as a file hierarchy rooted in the destination path...
	 */
	return (S_ISREG(fsb.st_mode) ?
	    fastcopy(from, to, &fsb) : copy(from, to));
}
Ejemplo n.º 26
0
void
runame(const struct kinfo_proc *kp, VARENT *ve)
{
	mbswprint(user_from_uid(kp->p_ruid, 0), ve->var->width,
	    ve->next != NULL);
}
Ejemplo n.º 27
0
char *
win32_ls_file(const char *name, LPWIN32_FIND_DATA file, int remote, int si_units)
{
	int ulen, glen, sz = 0;
	//struct tm *ltime = localtime(&st->st_mtime);
	char *user, *group;
	char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
	char sbuf[FMT_SCALED_STRSIZE];
	SYSTEMTIME now;
	SYSTEMTIME ftime;
	
	time_t mtime = filetime_to_time_t( file->ftLastWriteTime );
	BOOL time_conv_ok = FileTimeToSystemTime( &file->ftLastWriteTime, &ftime);
	struct tm *ltime = localtime( &mtime );
	
    if (!time_conv_ok) {
		error("Failed to convert file time to localtime");
	}
	
	strmode(0644, mode);
	if (!remote) {
		user = user_from_uid(0, 0);
	} else {
		snprintf(ubuf, sizeof ubuf, "%u", 0);
		user = ubuf;
	}
	
	if (!remote) {
		group = group_from_gid(0, 0);
	} else {
		snprintf(gbuf, sizeof gbuf, "%u", 0);
		group = gbuf;
	}
	
	if (time_conv_ok) {
		//now = time(NULL);
		GetSystemTime(&now);
		
		if ( (time_diff(now, ftime) / 10000000ULL) < (365*24*60*60) ) {
		//if (now - (365*24*60*60)/2 < st->st_mtime &&
		  //  now >= st->st_mtime)
			sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime);
		} else {
			sz = strftime(tbuf, sizeof tbuf, "%b %e  %Y", ltime);
		}
	}
	if (sz == 0)
		tbuf[0] = '\0';
	ulen = MAX(strlen(user), 8);
	glen = MAX(strlen(group), 8);
	long long size = (file->nFileSizeHigh * (MAXDWORD+1)) + file->nFileSizeLow;
		
	if (si_units) {
		fmt_scaled(size, sbuf);
		snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode,
		    1 /*nlink -- FIXME */, ulen, user, glen, group,
		    sbuf, tbuf, name);
	} else {
		snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
		    1 /*nlink -- FIXME */, ulen, user, glen, group,
		    size, tbuf, name);
	}
	return xstrdup(buf);
}
Ejemplo n.º 28
0
static int
statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
      u_long *pflags)
{
	FTSENT *p;
	gid_t sgid;
	uid_t suid;
	mode_t smode;
	u_long sflags = 0;
	const char *name;
	gid_t savegid;
	uid_t saveuid;
	mode_t savemode;
	u_long saveflags;
	u_short maxgid, maxuid, maxmode, maxflags;
	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
	static int first = 1;

	savegid = *pgid;
	saveuid = *puid;
	savemode = *pmode;
	saveflags = *pflags;
	if ((p = fts_children(t, 0)) == NULL) {
		if (errno)
			mtree_err("%s: %s", RP(parent), strerror(errno));
		return (1);
	}

	memset(g, 0, sizeof(g));
	memset(u, 0, sizeof(u));
	memset(m, 0, sizeof(m));
	memset(f, 0, sizeof(f));

	maxuid = maxgid = maxmode = maxflags = 0;
	for (; p; p = p->fts_link) {
		smode = p->fts_statp->st_mode & MBITS;
		if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
			savemode = smode;
			maxmode = m[smode];
		}
		sgid = p->fts_statp->st_gid;
		if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
			savegid = sgid;
			maxgid = g[sgid];
		}
		suid = p->fts_statp->st_uid;
		if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
			saveuid = suid;
			maxuid = u[suid];
		}

#if HAVE_STRUCT_STAT_ST_FLAGS
		sflags = FLAGS2INDEX(p->fts_statp->st_flags);
		if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
			saveflags = p->fts_statp->st_flags;
			maxflags = f[sflags];
		}
#endif
	}
	/*
	 * If the /set record is the same as the last one we do not need to
	 * output a new one.  So first we check to see if anything changed.
	 * Note that we always output a /set record for the first directory.
	 */
	if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
	    ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
	    ((keys & F_MODE) && (*pmode != savemode)) || 
	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
	    first) {
		first = 0;
		printf("/set type=file");
		if (keys & (F_UID | F_UNAME)) {
			if (keys & F_UNAME &&
			    (name = user_from_uid(saveuid, 1)) != NULL)
				printf(" uname=%s", name);
			else /* if (keys & F_UID) */
				printf(" uid=%lu", (u_long)saveuid);
		}
		if (keys & (F_GID | F_GNAME)) {
			if (keys & F_GNAME &&
			    (name = group_from_gid(savegid, 1)) != NULL)
				printf(" gname=%s", name);
			else /* if (keys & F_UID) */
				printf(" gid=%lu", (u_long)savegid);
		}
		if (keys & F_MODE)
			printf(" mode=%#lo", (u_long)savemode);
		if (keys & F_NLINK)
			printf(" nlink=1");
		if (keys & F_FLAGS)
			printf(" flags=%s",
			    flags_to_string(saveflags, "none"));
		printf("\n");
		*puid = saveuid;
		*pgid = savegid;
		*pmode = savemode;
		*pflags = saveflags;
	}
	return (0);
}
Ejemplo n.º 29
0
static void
statf(FTSENT *p)
{
	u_int32_t len, val;
	int fd, indent;
	const char *name;
#if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
	char *digestbuf;
#endif

	indent = printf("%s%s",
	    S_ISDIR(p->fts_statp->st_mode) ? "" : "    ", vispath(p->fts_name));

	if (indent > INDENTNAMELEN)
		indent = MAXLINELEN;
	else
		indent += printf("%*s", INDENTNAMELEN - indent, "");

	if (!S_ISREG(p->fts_statp->st_mode))
		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
		if (keys & F_UNAME &&
		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
			output(&indent, "uname=%s", name);
		else /* if (keys & F_UID) */
			output(&indent, "uid=%u", p->fts_statp->st_uid);
	}
	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
		if (keys & F_GNAME &&
		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
			output(&indent, "gname=%s", name);
		else /* if (keys & F_GID) */
			output(&indent, "gid=%u", p->fts_statp->st_gid);
	}
	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
	if (keys & F_DEV &&
	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
		output(&indent, "device=%#llx",
		    (long long)p->fts_statp->st_rdev);
	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
		output(&indent, "size=%lld", (long long)p->fts_statp->st_size);
	if (keys & F_TIME)
#if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
		output(&indent, "time=%ld.%ld",
		    (long)p->fts_statp->st_mtimespec.tv_sec,
		    p->fts_statp->st_mtimespec.tv_nsec);
#else
		output(&indent, "time=%ld.%ld",
		    (long)p->fts_statp->st_mtime, (long)0);
#endif
	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
		    crc(fd, &val, &len))
			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
		close(fd);
		output(&indent, "cksum=%lu", (long)val);
	}
#ifndef NO_MD5
	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
		if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL)
			mtree_err("%s: MD5File failed: %s", p->fts_accpath, strerror(errno));
		output(&indent, "md5=%s", digestbuf);
		free(digestbuf);
	}
#endif	/* ! NO_MD5 */
#ifndef NO_RMD160
	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
		if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL)
			mtree_err("%s: RMD160File failed: %s", p->fts_accpath, strerror(errno));
		output(&indent, "rmd160=%s", digestbuf);
		free(digestbuf);
	}
#endif	/* ! NO_RMD160 */
#ifndef NO_SHA1
	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
		if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL)
			mtree_err("%s: SHA1File failed: %s", p->fts_accpath, strerror(errno));
		output(&indent, "sha1=%s", digestbuf);
		free(digestbuf);
	}
#endif	/* ! NO_SHA1 */
#ifndef NO_SHA2
	if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
		if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL)
			mtree_err("%s: SHA256_File failed: %s", p->fts_accpath, strerror(errno));
		output(&indent, "sha256=%s", digestbuf);
		free(digestbuf);
	}
	if (keys & F_SHA384 && S_ISREG(p->fts_statp->st_mode)) {
		if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL)
			mtree_err("%s: SHA384_File failed: %s", p->fts_accpath, strerror(errno));
		output(&indent, "sha384=%s", digestbuf);
		free(digestbuf);
	}
	if (keys & F_SHA512 && S_ISREG(p->fts_statp->st_mode)) {
		if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL)
			mtree_err("%s: SHA512_File failed: %s", p->fts_accpath, strerror(errno));
		output(&indent, "sha512=%s", digestbuf);
		free(digestbuf);
	}
#endif	/* ! NO_SHA2 */
	if (keys & F_SLINK &&
	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
		output(&indent, "link=%s", vispath(rlink(p->fts_accpath)));
#if HAVE_STRUCT_STAT_ST_FLAGS
	if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
		output(&indent, "flags=%s",
		    flags_to_string(p->fts_statp->st_flags, "none"));
#endif
	putchar('\n');
}
Ejemplo n.º 30
0
static void
print_sockets (unsigned short family, int tcp, struct buffer *buffer)
{
  const char *proc_file;
  FILE *fp;

  if (family == AF_INET)
    proc_file = tcp ? "/proc/net/tcp" : "/proc/net/udp";
  else if (family == AF_INET6)
    proc_file = tcp ? "/proc/net/tcp6" : "/proc/net/udp6";
  else
    return;

  fp = gdb_fopen_cloexec (proc_file, "r");
  if (fp)
    {
      char buf[8192];

      do
	{
	  if (fgets (buf, sizeof (buf), fp))
	    {
	      uid_t uid;
	      unsigned int local_port, remote_port, state;
	      char local_address[NI_MAXHOST], remote_address[NI_MAXHOST];
	      int result;

#if NI_MAXHOST <= 32
#error "local_address and remote_address buffers too small"
#endif

	      result = sscanf (buf,
			       "%*d: %32[0-9A-F]:%X %32[0-9A-F]:%X %X %*X:%*X %*X:%*X %*X %d %*d %*u %*s\n",
			       local_address, &local_port,
			       remote_address, &remote_port,
			       &state,
			       &uid);
	      
	      if (result == 6)
		{
		  union socket_addr locaddr, remaddr;
		  size_t addr_size;
		  char user[UT_NAMESIZE];
		  char local_service[NI_MAXSERV], remote_service[NI_MAXSERV];

		  if (family == AF_INET)
		    {
		      sscanf (local_address, "%X",
			      &locaddr.sin.sin_addr.s_addr);
		      sscanf (remote_address, "%X",
			      &remaddr.sin.sin_addr.s_addr);
		      
		      locaddr.sin.sin_port = htons (local_port);
		      remaddr.sin.sin_port = htons (remote_port);

		      addr_size = sizeof (struct sockaddr_in);
		    }
		  else
		    {
		      sscanf (local_address, "%8X%8X%8X%8X",
			      locaddr.sin6.sin6_addr.s6_addr32,
			      locaddr.sin6.sin6_addr.s6_addr32 + 1,
			      locaddr.sin6.sin6_addr.s6_addr32 + 2,
			      locaddr.sin6.sin6_addr.s6_addr32 + 3);
		      sscanf (remote_address, "%8X%8X%8X%8X",
			      remaddr.sin6.sin6_addr.s6_addr32,
			      remaddr.sin6.sin6_addr.s6_addr32 + 1,
			      remaddr.sin6.sin6_addr.s6_addr32 + 2,
			      remaddr.sin6.sin6_addr.s6_addr32 + 3);

		      locaddr.sin6.sin6_port = htons (local_port);
		      remaddr.sin6.sin6_port = htons (remote_port);
		      
		      locaddr.sin6.sin6_flowinfo = 0;
		      remaddr.sin6.sin6_flowinfo = 0;
		      locaddr.sin6.sin6_scope_id = 0;
		      remaddr.sin6.sin6_scope_id = 0;

		      addr_size = sizeof (struct sockaddr_in6);
		    }
	      
		  locaddr.sa.sa_family = remaddr.sa.sa_family = family;
		      
		  result = getnameinfo (&locaddr.sa, addr_size,
					local_address, sizeof (local_address),
					local_service, sizeof (local_service),
					NI_NUMERICHOST | NI_NUMERICSERV
					| (tcp ? 0 : NI_DGRAM));
		  if (result)
		    continue;
		  
		  result = getnameinfo (&remaddr.sa, addr_size,
					remote_address,
					sizeof (remote_address),
					remote_service,
					sizeof (remote_service),
					NI_NUMERICHOST | NI_NUMERICSERV
					| (tcp ? 0 : NI_DGRAM));
		  if (result)
		    continue;
		  
		  user_from_uid (user, sizeof (user), uid);
		  
		  buffer_xml_printf (
		      buffer,
		      "<item>"
		      "<column name=\"local address\">%s</column>"
		      "<column name=\"local port\">%s</column>"
		      "<column name=\"remote address\">%s</column>"
		      "<column name=\"remote port\">%s</column>"
		      "<column name=\"state\">%s</column>"
		      "<column name=\"user\">%s</column>"
		      "<column name=\"family\">%s</column>" 
		      "<column name=\"protocol\">%s</column>"
		      "</item>",
		      local_address,
		      local_service,
		      remote_address,
		      remote_service,
		      format_socket_state (state),
		      user,
		      (family == AF_INET) ? "INET" : "INET6",
		      tcp ? "STREAM" : "DGRAM");
		}
	    }
	}
      while (!feof (fp));

      fclose (fp);
    }
}