Esempio n. 1
0
void gfal2_test_plugin_access_file()
{
	GError* tmp_err=NULL;
	gfal_handle handle = gfal_initG(&tmp_err);
	assert_true_with_message(handle != NULL, " must init properly");
	if(handle == NULL)	
		return;
	gfal_plugins_instance(handle, &tmp_err);
	assert_true_with_message(tmp_err== NULL, " must be instance properly");
	if(tmp_err)
		return;
	test_mock_lfc(handle, &tmp_err);
	assert_true_with_message(tmp_err== NULL, " must be mocked properly");
	if(tmp_err)
			return;
#if USE_MOCK
	will_respond(lfc_mock_access, 0, want_string(path, TEST_LFC_VALID_ACCESS+4), want(mode, F_OK));
	will_respond(lfc_mock_access, ENOENT, want_string(path, TEST_LFC_NOEXIST_ACCESS+4), want(mode, F_OK));
	always_return(lfc_mock_access, EINVAL);
#endif
	int ret = gfal_plugins_accessG(handle, TEST_LFC_VALID_ACCESS, F_OK, &tmp_err);
	assert_true_with_message(ret == 0, " must be a valid access");

	
	ret = gfal_plugins_accessG(handle, TEST_LFC_NOEXIST_ACCESS, F_OK, &tmp_err);	
	assert_true_with_message(ret!=0 && tmp_err!=NULL && tmp_err->code == ENOENT, " must be a non-existing file %d %ld %d", (int)ret, (long)tmp_err, (int) (tmp_err)?(tmp_err->code):0);
	
	g_clear_error(&tmp_err);
	gfal_handle_freeG(handle);
}
Esempio n. 2
0
faction *gm_addfaction(const char *email, plane * p, region * r)
{
  attrib *a;
  unit *u;
  faction *f = calloc(1, sizeof(faction));

  assert(p != NULL);

  /* GM faction */
  a_add(&f->attribs, make_key(atoi36("quest")));
  f->banner = _strdup("quest faction");
  f->name = _strdup("quest faction");
  f->passw = _strdup(itoa36(rng_int()));
  if (set_email(&f->email, email) != 0) {
    log_error("Invalid email address for faction %s: %s\n", itoa36(f->no), email);
  }
  f->race = new_race[RC_TEMPLATE];
  f->age = 0;
  f->lastorders = turn;
  f->alive = true;
  f->locale = default_locale;
  f->options =
    want(O_COMPRESS) | want(O_REPORT) | want(O_COMPUTER) | want(O_ADRESSEN);
  {
    faction *xist;
    int id = atoi36("gm00") - 1;
    do {
      xist = findfaction(++id);
    } while (xist);

    f->no = id;
    addlist(&factions, f);
    fhash(f);
  }

  /* generic permissions */
  a = a_add(&f->attribs, a_new(&at_permissions));
  if (a) {
    attrib *ap = (attrib *) a->data.v;
    const char *keys[] =
      { "gmterf", "gmtele", "gmgive", "gmskil", "gmtake", "gmmsgr", "gmmsgu",
        "gmgate", 0 };
    const char **key_p = keys;
    while (*key_p) {
      add_key(&ap, atoi36(*key_p));
      ++key_p;
    }
    a_add(&ap, make_atgmcreate(resource2item(r_silver)));

    a->data.v = ap;
  }

  /* one initial unit */
  u = create_unit(r, f, 1, new_race[RC_TEMPLATE], 1, "quest master", NULL);
  u->irace = new_race[RC_GNOME];

  return f;
}
Esempio n. 3
0
File: pppoe.c Progetto: aahud/harvey
int
pktread(int timeout, int fd, void *buf, int nbuf, int (*want)(uint8_t*))
{
	int n, t2;
	n = -1;
	for(t2=timeout; t2<16000; t2*=2){
		while((n = aread(t2, fd, buf, nbuf)) > 0){
			if(malformed(buf, n, EtherPppoeDiscovery)){
				if(debug)
					fprint(2, "dropping pkt: %r\n");
				continue;
			}
			if(debug)
				dumppkt(buf);
			if(!want(buf)){
				if(debug)
					fprint(2, "dropping unwanted pkt: %r\n");
				continue;
			}
			break;
		}
		if(n > 0)
			break;
	}
	return n;
}
Esempio n. 4
0
void test_mock_guid_open_invalid(const char * guid1){
#if USE_MOCK
	gfal_handle handle = gfal_posix_instance();
	gfal_plugins_instance(handle,NULL);
		
	will_respond(lfc_mock_getlinks, ENOENT, want_string(guid, ((char*)guid1)+5), want(path, NULL), want_non_null(nbentries), want_non_null(linkinfos));	
#endif	
}
Esempio n. 5
0
static void guardian_faction(plane * pl, int id)
{
  region *r;
  faction *f = findfaction(id);

  if (!f) {
    f = calloc(1, sizeof(faction));
    f->banner = _strdup("Sie dienen dem großen Wyrm");
    f->passw = _strdup(itoa36(rng_int()));
    set_email(&f->email, "*****@*****.**");
    f->name = _strdup("Igjarjuks Kundschafter");
    f->race = new_race[RC_ILLUSION];
    f->age = turn;
    f->locale = find_locale("de");
    f->options =
      want(O_COMPRESS) | want(O_REPORT) | want(O_COMPUTER) | want(O_ADRESSEN) |
      want(O_DEBUG);

    f->no = id;
    addlist(&factions, f);
    fhash(f);
  }
  if (f->race != new_race[RC_ILLUSION]) {
    assert(!"guardian id vergeben");
    exit(0);
  }
  f->lastorders = turn;
  f->alive = true;
  for (r = regions; r; r = r->next)
    if (getplane(r) == pl && rterrain(r) != T_FIREWALL) {
      unit *u;
      freset(r, RF_ENCOUNTER);
      for (u = r->units; u; u = u->next) {
        if (u->faction == f)
          break;
      }
      if (u)
        continue;
      u = createunit(r, f, 1, new_race[RC_GOBLIN]);
      set_string(&u->name, "Igjarjuks Auge");
      set_item(u, I_RING_OF_INVISIBILITY, 1);
      set_order(&u->thisorder, NULL);
      fset(u, UFL_ANON_FACTION);
      set_money(u, 1000);
    }
}
Esempio n. 6
0
void test_mock_guid_open_valid(const char * guid1){
#if USE_MOCK
	int i1;
	test_mock_lfc_open_valid(TEST_LFC_OPEN_EXIST);
	define_linkinfos= calloc(sizeof(struct lfc_linkinfo),3);
	define_numberlinkinfos=3;
	for(i1=0; i1< define_numberlinkinfos; ++i1)
		g_strlcpy(define_linkinfos[i1].path, TEST_LFC_OPEN_EXIST+4, 2048);
	will_respond(lfc_mock_getlinks, 0, want_string(guid, ((char*)guid1)+5), want(path, NULL), want_non_null(nbentries), want_non_null(linkinfos));	
#endif	
}
Esempio n. 7
0
void test_mock_srm_open_valid(char** tab, char** tab_turl, int* res){
#if USE_MOCK
	test_rfio_mock_all();
	setup_mock_srm();
	gfal_handle handle = gfal_posix_instance();
	gfal_plugins_instance(handle,NULL);
	
	define_mock_endpoints(TEST_SRM_DPM_FULLENDPOINT_URL); // mock the mds for the srm endpoitn resolution


	will_respond(srm_mock_srm_context_init, 0, want_non_null(context), want_string(srm_endpoint, TEST_SRM_DPM_FULLENDPOINT_URL));
	define_mock_srmv2_pinfilestatus(1, tab, NULL, tab_turl, res);
	will_respond(srm_mock_srm_prepare_to_get, 1, want_non_null(context), want_non_null(input), want_non_null(output));

	will_respond(rfio_mock_open, 15, want_non_null(path));
	will_respond(rfio_mock_close, 0, want(fd, 15));
#endif
}
Esempio n. 8
0
void test_mock_srm_open_write_valid(char** tab, char** tab_turl, int* res){
#if USE_MOCK
	test_rfio_mock_all();
	setup_mock_srm();
	gfal_handle handle = gfal_posix_instance();
	gfal_plugins_instance(handle,NULL);
	int status[] = { 0,0 };
	char* turls[] = { "rfio://mockedturl", NULL };
	char* surls[] = { "srm://mockedsurl", NULL };
	
	define_mock_endpoints(TEST_SRM_DPM_FULLENDPOINT_URL); // mock the mds for the srm endpoitn resolution


	will_respond(srm_mock_srm_context_init, 0, want_non_null(context), want_string(srm_endpoint, TEST_SRM_DPM_FULLENDPOINT_URL));
	define_mock_srmv2_putoutput(1, tab, NULL, tab_turl, res);
	will_respond(srm_mock_srm_prepare_to_put, 1, want_non_null(context), want_non_null(input), want_non_null(output));

	will_respond(rfio_mock_open, 15, want_non_null(path));

	define_put_done(1, surls, NULL, turls, status);
	will_respond(srm_mock_srm_put_done,1, want_non_null(context));
	will_respond(rfio_mock_close, 0, want(fd, 15));
#endif
}
Esempio n. 9
0
/*
 * wtmp --
 *	read through the wtmp file
 */
void
wtmp(const char *file, int namesz, int linesz, int hostsz)
{
	struct utmp	*bp;		/* current structure */
	TTY	*T;			/* tty list entry */
	struct stat	stb;		/* stat of file for sz */
	time_t	delta;			/* time difference */
	off_t	bl;
	int	bytes, wfd;
	char	*ct;
	const char *crmsg;
	size_t  len = sizeof(*buf) * MAXUTMP;

	if ((buf = malloc(len)) == NULL)
		err(1, "Cannot allocate utmp buffer");

	crmsg = NULL;

	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
		err(1, "%s", file);
	bl = (stb.st_size + len - 1) / len;

	buf[FIRSTVALID].ut_timefld = time(NULL);
	(void)signal(SIGINT, onintr);
	(void)signal(SIGQUIT, onintr);

	while (--bl >= 0) {
		if (lseek(wfd, bl * len, SEEK_SET) == -1 ||
		    (bytes = read(wfd, buf, len)) == -1)
			err(1, "%s", file);
		for (bp = &buf[bytes / sizeof(*buf) - 1]; bp >= buf; --bp) {
			/*
			 * if the terminal line is '~', the machine stopped.
			 * see utmp(5) for more info.
			 */
			if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
				/* everybody just logged out */
				for (T = ttylist; T; T = T->next)
					T->logout = -bp->ut_timefld;
				currentout = -bp->ut_timefld;
				crmsg = strncmp(bp->ut_name, "shutdown",
				    namesz) ? "crash" : "shutdown";
				if (want(bp, NO)) {
					ct = fmttime(bp->ut_timefld, fulltime);
					printf("%-*.*s  %-*.*s %-*.*s %s\n",
					    namesz, namesz, bp->ut_name,
					    linesz, linesz, bp->ut_line,
					    hostsz, hostsz, bp->ut_host, ct);
					if (maxrec != -1 && !--maxrec)
						return;
				}
				continue;
			}
			/*
			 * if the line is '{' or '|', date got set; see
			 * utmp(5) for more info.
			 */
			if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
			    && !bp->ut_line[1]) {
				if (want(bp, NO)) {
					ct = fmttime(bp->ut_timefld, fulltime);
				printf("%-*.*s  %-*.*s %-*.*s %s\n",
				    namesz, namesz,
				    bp->ut_name,
				    linesz, linesz,
				    bp->ut_line,
				    hostsz, hostsz,
				    bp->ut_host,
				    ct);
					if (maxrec && !--maxrec)
						return;
				}
				continue;
			}
			/* find associated tty */
			for (T = ttylist;; T = T->next) {
				if (!T) {
					/* add new one */
					T = addtty(bp->ut_line);
					break;
				}
				if (!strncmp(T->tty, bp->ut_line, LINESIZE))
					break;
			}
			if (TYPE(bp) == SIGNATURE)
				continue;
			if (bp->ut_name[0] && want(bp, YES)) {
				ct = fmttime(bp->ut_timefld, fulltime);
				printf("%-*.*s  %-*.*s %-*.*s %s ",
				    namesz, namesz, bp->ut_name,
				    linesz, linesz, bp->ut_line,
				    hostsz, hostsz, bp->ut_host,
				    ct);
				if (!T->logout)
					puts("  still logged in");
				else {
					if (T->logout < 0) {
						T->logout = -T->logout;
						printf("- %s", crmsg);
					}
					else
						printf("- %s",
						    fmttime(T->logout,
						    fulltime | TIMEONLY));
					delta = T->logout - bp->ut_timefld;
					if (delta < SECSPERDAY)
						printf("  (%s)\n",
						    fmttime(delta,
						    fulltime | TIMEONLY | GMT));
					else
						printf(" (%ld+%s)\n",
						    delta / SECSPERDAY,
						    fmttime(delta,
						    fulltime | TIMEONLY | GMT));
				}
				if (maxrec != -1 && !--maxrec)
					return;
			}
			T->logout = bp->ut_timefld;
		}
	}
	fulltime = 1;	/* show full time */
	crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
	if ((ct = strrchr(file, '/')) != NULL)
		ct++;
	printf("\n%s begins %s\n", ct ? ct : file, crmsg);
}
Esempio n. 10
0
/*
 * read through the wtmp file
 */
void
wtmp(void)
{
	time_t	delta, total = 0;
	int	timesize, wfd, snapfound = 0;
	char	*ct, *crmsg, tim[40];
	struct utmp	*bp;
	struct stat	stb;
	ssize_t	bytes;
	off_t	bl;
	struct ttytab	*T;

	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
		err(1, "%s", file);
	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);

	if (fulltime)
		timesize = 8;	/* HH:MM:SS */
	else
		timesize = 5;	/* HH:MM */

	(void)time(&buf[0].ut_time);
	(void)signal(SIGINT, onintr);
	(void)signal(SIGQUIT, onintr);

	while (--bl >= 0) {
		if (lseek(wfd, bl * sizeof(buf), SEEK_SET) == -1 ||
		    (bytes = read(wfd, buf, sizeof(buf))) == -1)
			err(1, "%s", file);
		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
			/*
			 * if the terminal line is '~', the machine stopped.
			 * see utmp(5) for more info.
			 */
			if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
				/* everybody just logged out */
				for (T = ttylist; T; T = T->next)
					T->logout = -bp->ut_time;
				currentout = -bp->ut_time;
				crmsg = strncmp(bp->ut_name, "shutdown",
				    UT_NAMESIZE) ? "crash" : "shutdown";

				/*
				 * if we're in snapshot mode, we want to
				 * exit if this shutdown/reboot appears
				 * while we we are tracking the active
				 * range
				 */
				if (snaptime && snapfound) {
					close(wfd);
					return;
				}

				/*
				 * don't print shutdown/reboot entries
				 * unless flagged for
				 */
				if (want(bp, NO)) {
					if (seconds) {
						snprintf(tim, sizeof tim, "%ld",
						    (long)bp->ut_time);
					} else {
						ct = ctime(&bp->ut_time);
						snprintf(tim, sizeof tim,
						    "%10.10s %*.*s", ct,
						    timesize, timesize, ct + 11);
					}
					printf("%-*.*s %-*.*s %-*.*s %s \n",
					    NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
					    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
					    HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
					    tim);

					if (maxrec != -1 && !--maxrec) {
						close(wfd);
						return;
					}
				}
				continue;
			}

			/*
			 * if the line is '{' or '|', date got set; see
			 * utmp(5) for more info.
			 */
			if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') &&
			    !bp->ut_line[1]) {
				if (want(bp, NO)) {
					if (seconds) {
						snprintf(tim, sizeof tim, "%ld",
						    (long)bp->ut_time);
					} else {
						ct = ctime(&bp->ut_time);
						snprintf(tim, sizeof tim,
						    "%10.10s %*.*s", ct,
						    timesize, timesize, ct + 11);
					}
					printf("%-*.*s %-*.*s %-*.*s %s \n",
					    NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
					    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
					    HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
					    tim);

					if (maxrec && !--maxrec) {
						close(wfd);
						return;
					}
				}
				continue;
			}

			/* find associated tty */
			for (T = ttylist;; T = T->next) {
				if (!T) {
					/* add new one */
					T = addtty(bp->ut_line);
					break;
				}
				if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
					break;
			}

			/*
			 * print record if not in snapshot mode and wanted
			 * or in snapshot mode and in snapshot range
			 */
			if (bp->ut_name[0] &&
			    ((want(bp, YES)) || (bp->ut_time < snaptime &&
			    (T->logout > snaptime || !T->logout ||
			    T->logout < 0)))) {
				snapfound = 1;
				if (seconds) {
					snprintf(tim, sizeof tim, "%ld",
					    (long)bp->ut_time);
				} else {
					ct = ctime(&bp->ut_time);
					snprintf(tim, sizeof tim,
					    "%10.10s %*.*s", ct,
					    timesize, timesize, ct + 11);
				}
				printf("%-*.*s %-*.*s %-*.*s %s ",
				    NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
				    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
				    HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
				    tim);

				if (!T->logout)
					puts("  still logged in");
				else {
					if (T->logout < 0) {
						T->logout = -T->logout;
						printf("- %s", crmsg);
					} else {
						if (seconds)
							printf("- %ld",
							    (long)T->logout);
						else
							printf("- %*.*s",
							    timesize, timesize,
							    ctime(&T->logout)+11);
					}
					delta = T->logout - bp->ut_time;
					if (seconds)
						printf("  (%ld)\n", (long)delta);
					else {
						if (delta < SECSPERDAY)
							printf("  (%*.*s)\n",
							    timesize, timesize,
							    asctime(gmtime(&delta))+11);
						else
							printf(" (%ld+%*.*s)\n",
							    delta / SECSPERDAY,
							    timesize, timesize,
							    asctime(gmtime(&delta))+11);
					}
					if (calculate)
						total += delta;
				}
				if (maxrec != -1 && !--maxrec) {
					close(wfd);
					return;
				}
			}
			T->logout = bp->ut_time;
		}
	}
	close(wfd);
	if (calculate) {
		if ((total / SECSPERDAY) > 0) {
			int days = (total / SECSPERDAY);
			total -= (days * SECSPERDAY);

			printf("\nTotal time: %d days, %*.*s\n",
			    days, timesize, timesize,
			    asctime(gmtime(&total))+11);
		} else
			printf("\nTotal time: %*.*s\n",
			    timesize, timesize,
			    asctime(gmtime(&total))+11);
	}
	ct = ctime(&buf[0].ut_time);
	printf("\nwtmp begins %10.10s %*.*s %4.4s\n", ct, timesize, timesize,
	    ct + 11, ct + 20);
}
Esempio n. 11
0
void
radwtmp()
{
        int wfd;
        struct stat stb;
        int bl;
        struct radutmp *bp;
        int bytes;
        struct tm *tm;
        char ct[256];
        WTMP *pp;
	int acct_enabled = 0;
		
        if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) {
                grad_log(GRAD_LOG_ERR|GRAD_LOG_PERROR, 
                         _("can't open file %s"), file);
                exit(1);
        }
        bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
        
        grad_set_signal(SIGINT, sig_int);
        stop = 0;

        /*time(&buf[0].ut_time);*/
        
        while (!stop && --bl >= 0) {
                if (lseek(wfd, (off_t)(bl * sizeof(buf)), SEEK_SET) == -1 ||
                    (bytes = read(wfd, buf, sizeof(buf))) == -1)
                        grad_log(GRAD_LOG_ERR, "%s", file);
                for (bp = &buf[bytes / sizeof(buf[0]) - 1]; !stop && bp >= buf;
		     --bp) {
                        switch (bp->type) {
                        case P_LOGIN:
                                if (pp = find_logout(bp)) {
                                        if (want(bp)) {
                                                print_entry(pp, bp, 0);
                                                if (maxrec != -1 && !--maxrec)
                                                        return;
                                        }
                                        delete_logout(pp, bp);
                                } else if (pp = find_restart(bp)) {
                                        if (want(bp)) {
                                                print_entry(pp, bp, 0);
                                                if (maxrec != -1 && !--maxrec)
                                                        return;
                                        }
                                } else if (pp = find_login(bp)) {
                                        /* Ignore duplicate logins */
                                        if (strncmp(pp->ut.session_id,
                                                   bp->session_id,
                                                   RUT_IDSIZE) == 0)
                                                break; 
                                        /*
                                         * This login misses logout
                                         */
                                        if (want(bp)) {
                                                print_entry(pp, bp,
                                                            mark_missing_stops);
                                                if (maxrec != -1 && !--maxrec)
                                                        return;
                                        }
                                        /* Update login information */
                                        pp->ut = *bp;
                                } else {
                                        if (want(bp)) {
                                                print_entry(NULL, bp, 0);
                                                if (maxrec != -1 && !--maxrec)
                                                        return;
                                        }
                                }
                                break;
                        case P_IDLE:
                                /*if (!find_logout_sid(bp))*/
                                        add_logout(bp);
                                break;
                        case P_NAS_SHUTDOWN:
                        case P_NAS_START:
                                add_nas_restart(bp);
                                if (want(bp)) {
                                        print_reboot_entry(bp);
                                        if (maxrec != -1 && !--maxrec)
                                                return;
                                }
                                break;
			case P_ACCT_DISABLED:
				if (!acct_enabled) {
					printf(_("System accounting is disabled\n"));
					stop = 1;
				} else
					print_acct_toggle(bp);
				break;
			case P_ACCT_ENABLED:
				acct_enabled = 1;
				print_acct_toggle(bp);
				break;
                        default:
                                break;
                        }
                }
        }
        
        tm = localtime(&buf[0].time);
        strftime(ct, sizeof(ct), "%c", tm);
        printf(_("\nradwtmp begins %s\n"), ct);
}
Esempio n. 12
0
Ensure parameter_name_gives_true_if_matching() {
    Constraint *any_old_want = want(label, 37);
    assert_equal(is_constraint_parameter(any_old_want, "wrong_label"), 0);
    assert_equal(is_constraint_parameter(any_old_want, "label"), 1);
    destroy_constraint(any_old_want);
}
Esempio n. 13
0
Ensure can_construct_and_destroy_an_want_constraint() {
    Constraint *any_old_want = want(label, 37);
    destroy_constraint(any_old_want);
}
Esempio n. 14
0
Ensure equal_pointers_compare_true_with_a_want_constraint() {
    Constraint *want_pointed_at_37 = want(label, (void *)37);
    assert_equal(compare_constraint(want_pointed_at_37, (void *)37), 1);
    assert_equal(compare_constraint(want_pointed_at_37, (void *)36), 0);
    destroy_constraint(want_pointed_at_37);
}
Esempio n. 15
0
 void validate(const FLAC__StreamMetadata_StreamInfo &si)
 {
     want(si.sample_rate > 0);
     want(si.channels > 0 && si.channels < 9);
     want(si.bits_per_sample >= 8 && si.bits_per_sample <= 32);
 }
Esempio n. 16
0
/*
 * wtmp --
 *	read through the wtmp file
 */
static void
wtmp(void) {
	register struct utmp	*bp;		/* current structure */
	register TTY	*T;			/* tty list entry */
	long	delta;				/* time difference */
	char *crmsg = NULL;
	char *ct = NULL;
	int fd;
	struct utmp *utl;
	struct stat st;
	int utl_len;
	int listnr = 0;
	int i;

	utmpname(file);

	{
#if defined(_HAVE_UT_TV)
		struct timeval tv;
		gettimeofday(&tv, NULL);
		utmpbuf.ut_tv.tv_sec = tv.tv_sec;
		utmpbuf.ut_tv.tv_usec = tv.tv_usec;
#else
		time_t t;
		time(&t);
		utmpbuf.ut_time = t;
#endif
	}

	(void)signal(SIGINT, onintr);
	(void)signal(SIGQUIT, onintr);

	if ((fd = open(file,O_RDONLY)) < 0)
		exit(1);
	fstat(fd, &st);
	utl_len = st.st_size;
	utl = mmap(NULL, utl_len, PROT_READ|PROT_WRITE,
		   MAP_PRIVATE|MAP_FILE, fd, 0);
	if (utl == NULL)
		exit(1);
	listnr = utl_len/sizeof(struct utmp);

	if(listnr) 
		ct = utmp_ctime(&utl[0]);

	for(i = listnr - 1; i >= 0; i--) {
		bp = utl+i;
		/*
		 * if the terminal line is '~', the machine stopped.
		 * see utmp(5) for more info.
		 */
		if (!strncmp(bp->ut_line, "~", LMAX)) {
		    /* 
		     * utmp(5) also mentions that the user 
		     * name should be 'shutdown' or 'reboot'.
		     * Not checking the name causes e.g. runlevel
		     * changes to be displayed as 'crash'. -thaele
		     */
		    if (!strncmp(bp->ut_user, "reboot", NMAX) ||
			!strncmp(bp->ut_user, "shutdown", NMAX)) {	
			/* everybody just logged out */
			for (T = ttylist; T; T = T->next)
			    T->logout = -bp->ut_time;
		    }

		    currentout = -bp->ut_time;
		    crmsg = (strncmp(bp->ut_name, "shutdown", NMAX)
			    ? "crash" : "down ");
		    if (!bp->ut_name[0])
			(void)strcpy(bp->ut_name, "reboot");
		    if (want(bp, NO)) {
			ct = utmp_ctime(bp);
			if(bp->ut_type != LOGIN_PROCESS) {
			    print_partial_line(bp);
			    putchar('\n');
			}
			if (maxrec && !--maxrec)
			    return;
		    }
		    continue;
		}
		/* find associated tty */
		for (T = ttylist;; T = T->next) {
		    if (!T) {
			/* add new one */
			T = addtty(bp->ut_line);
			break;
		    }
		    if (!strncmp(T->tty, bp->ut_line, LMAX))
			break;
		}
		if (bp->ut_name[0] && bp->ut_type != LOGIN_PROCESS
		    && bp->ut_type != DEAD_PROCESS
		    && want(bp, YES)) {

		    print_partial_line(bp);

		    if (!T->logout)
			puts(_("  still logged in"));
		    else {
			if (T->logout < 0) {
			    T->logout = -T->logout;
			    printf("- %s", crmsg);
			}
			else
			    printf("- %5.5s", ctime(&T->logout)+11);
			delta = T->logout - bp->ut_time;
			if (delta < SECDAY)
			    printf("  (%5.5s)\n", asctime(gmtime(&delta))+11);
			else
			    printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta))+11);
		    }
		    if (maxrec != -1 && !--maxrec)
			return;
		}
		T->logout = bp->ut_time;
		utmpbuf.ut_time = bp->ut_time;
	}
	munmap(utl,utl_len);
	close(fd);
	if(ct) printf(_("\nwtmp begins %s"), ct); 	/* ct already ends in \n */
}
Esempio n. 17
0
    int
main(int ac, char **av)
{
    int i, j;
    int aflag = 0;
    int uflag = 0;
    int fpos;    /* current position in time format buffer */
    int chrcnt;    /* # of chars formatted by current sprintf */
    int bl, wtmp;
    char *ct;
    char *ut_host;
    char *ut_user;
    struct utmpx *bp;
    time_t otime;
    struct stat stb;
    int print = 0;
    char *crmsg = (char *)0;
    long outrec = 0;
    long maxrec = 0x7fffffffL;
    char *wtmpfile = "/var/adm/wtmpx";
    size_t hostf_len;

    (void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)        /* Should be defined by cc -D */
#define    TEXT_DOMAIN "SYS_TEST"        /* Use this only if it weren't. */
#endif
    (void) textdomain(TEXT_DOMAIN);

    (void) time(&buf[0].ut_xtime);
    ac--, av++;
    argc = ac;
    argv = av;
    names = malloc(argc * sizeof (char *));
    if (names == NULL) {
        perror("first");
        exit(2);
    }
    names_num = 0;
    for (i = 0; i < argc; i++) {
        if (argv[i][0] == '-') {

            /* -[0-9]*   sets max # records to print */
            if (isdigit(argv[i][1])) {
                maxrec = atoi(argv[i]+1);
                continue;
            }

            for (j = 1; argv[i][j] != '\0'; ++j) {
                switch (argv[i][j]) {

                    /* -f name sets filename of wtmp file */
                    case 'f':
                        if (argv[i][j+1] != '\0') {
                            wtmpfile = &argv[i][j+1];
                        } else if (i+1 < argc) {
                            wtmpfile = argv[++i];
                        } else {
                            (void) fprintf(stderr,
                                    gettext("first: argument to "
                                        "-f is missing\n"));
                            (void) fprintf(stderr,
                                    gettext(USAGE));
                            exit(1);
                        }
                        goto next_word;

                        /* -n number sets max # records to print */
                    case 'n': {
                              char *arg;

                              if (argv[i][j+1] != '\0') {
                                  arg = &argv[i][j+1];
                              } else if (i+1 < argc) {
                                  arg = argv[++i];
                              } else {
                                  (void) fprintf(stderr,
                                          gettext("first: argument to "
                                              "-n is missing\n"));
                                  (void) fprintf(stderr,
                                          gettext(USAGE));
                                  exit(1);
                              }

                              if (!isdigit(*arg)) {
                                  (void) fprintf(stderr,
                                          gettext("first: argument to "
                                              "-n is not a number\n"));
                                  (void) fprintf(stderr,
                                          gettext(USAGE));
                                  exit(1);
                              }
                              maxrec = atoi(arg);
                              goto next_word;
                          }

                          /* -a displays hostname last on the line */
                    case 'a':
                          aflag++;
                          break;

                          /* -u displays times in UNIX epoch format */
                    case 'u':
                          uflag++;
                          break;

                    default:
                          (void) fprintf(stderr, gettext(USAGE));
                          exit(1);
                }
            }

next_word:
            continue;
        }

        if (strlen(argv[i]) > 2 || strcmp(argv[i], "~") == 0 ||
                getpwnam(argv[i]) != NULL) {
            /* Not a tty number. */
            names[names_num] = argv[i];
            ++names_num;
        } else {
            /* tty number.  Prepend "tty". */
            names[names_num] = strspl("tty", argv[i]);
            ++names_num;
        }
    }

    wtmp = open(wtmpfile, O_RDONLY | O_LARGEFILE);
    if (wtmp < 0) {
        perror(wtmpfile);
        exit(1);
    }
    (void) fstat(wtmp, &stb);
    bl = (stb.st_size + sizeof (buf)-1) / sizeof (buf);
    if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
        (void) signal(SIGINT, onintr);
        (void) signal(SIGQUIT, onintr);
    }
    lines = CHUNK_SIZE;
    ttnames = calloc(lines, sizeof (char *));
    logouts = calloc(lines, sizeof (time_t));
    if (ttnames == NULL || logouts == NULL) {
        (void) fprintf(stderr, gettext("Out of memory \n "));
        exit(2);
    }
    for (bl--; bl >= 0; bl--) {
        (void) lseek(wtmp, (off_t)(bl * sizeof (buf)), 0);
        bp = &buf[read(wtmp, buf, sizeof (buf)) / sizeof (buf[0]) - 1];
        for (; bp >= buf; bp--) {
            if (want(bp, &ut_host, &ut_user)) {
                for (i = 0; i <= lines; i++) {
                    if (i == lines)
                        reallocate_buffer();
                    if (ttnames[i] == NULL) {
                        memory_alloc(i);
                        /*
                         * LMAX+HMAX+NMAX+3 bytes have been
                         * allocated for ttnames[i].
                         * If bp->ut_line is longer than LMAX,
                         * ut_host is longer than HMAX,
                         * and ut_user is longer than NMAX,
                         * truncate it to fit ttnames[i].
                         */
                        (void) strlcpy(ttnames[i], bp->ut_line,
                                LMAX+1);
                        (void) strlcpy(ttnames[i]+LMAX+1,
                                ut_host, HMAX+1);
                        (void) strlcpy(ttnames[i]+LMAX+HMAX+2,
                                ut_user, NMAX+1);
                        record_time(&otime, &print,
                                i, bp);
                        break;
                    } else if (linehostnameq(ttnames[i],
                                bp->ut_line, ut_host, ut_user)) {
                        record_time(&otime,
                                &print, i, bp);
                        break;
                    }
                }
            }
            if (print) {
                if (strncmp(bp->ut_line, "ftp", 3) == 0)
                    bp->ut_line[3] = '\0';
                if (strncmp(bp->ut_line, "uucp", 4) == 0)
                    bp->ut_line[4] = '\0';

                ct = ctime(&bp->ut_xtime);
                (void) printf(gettext("%-*.*s  %-*.*s "),
                        NMAX, NMAX, bp->ut_name,
                        LMAX, LMAX, bp->ut_line);
                hostf_len = strlen(bp->ut_host);
                (void) snprintf(hostf, sizeof (hostf),
                        "%-*.*s", hostf_len, hostf_len,
                        bp->ut_host);
                if(uflag) {
                    fpos = snprintf(timef, sizeof (timef),
                            "%10lu ", bp->ut_xtime);
                } else {
                    fpos = snprintf(timef, sizeof (timef),
                            "%10.10s %13.13s ", /* MJC 8 extra chars */
                            ct, 11 + ct);
                }
                if (!lineq(bp->ut_line, "system boot") &&
                        !lineq(bp->ut_line, "system down")) {
                    if (otime == 0 &&
                            bp->ut_type == USER_PROCESS) {

                        if (fpos < sizeof (timef)) {
                            /* timef still has room */
                            (void) snprintf(timef + fpos, sizeof (timef) - fpos,
                                    gettext("  still logged in"));
                        }

                    } else {
                        time_t delta;
                        if (otime < 0) {
                            otime = -otime;
                            /*
                             * TRANSLATION_NOTE
                             * See other notes on "down"
                             * and "- %5.5s".
                             * "-" means "until".  This
                             * is displayed after the
                             * starting time as in:
                             *     16:20 - down
                             * You probably don't want to
                             * translate this.  Should you
                             * decide to translate this,
                             * translate "- %5.5s" too.
                             */

                            if (fpos < sizeof (timef)) {
                                /* timef still has room */
                                if(uflag) {
                                    chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
                                            gettext("- %-10s"), crmsg);
                                } else {
                                    chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
                                            gettext("- %-24s"), crmsg);
                                }
                                fpos += chrcnt;
                            }

                        } else {

                            if (fpos < sizeof (timef)) {
                                /* timef still has room */
                                if(uflag) {
                                    chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
                                            gettext("- %10lu"), otime);
                                } else {
                                    chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
                                            gettext("- %10.10s %13.13s"), ctime(&otime), ctime(&otime) + 11); /* MJC 19 extra chars */
                                }
                                fpos += chrcnt;
                            }

                        }
                        delta = otime - bp->ut_xtime;
                        if (delta < SECDAY) {

                            if (fpos < sizeof (timef)) {
                                /* timef still has room */
                                (void) snprintf(timef + fpos, sizeof (timef) - fpos,
                                        gettext("  (%5.5s)"), asctime(gmtime(&delta)) + 11);
                            }

                        } else {

                            if (fpos < sizeof (timef)) {
                                /* timef still has room */
                                (void) snprintf(timef + fpos, sizeof (timef) - fpos,
                                        gettext(" (%ld+%5.5s)"), delta / SECDAY,
                                        asctime(gmtime(&delta)) + 11);
                            }

                        }
                    }
                }
                if (aflag)
                    (void) printf("%-62.62s %-.*s\n", /* MJC */
                            timef, strlen(hostf), hostf);
                else
                    (void) printf("%-16.16s %-.62s\n", /* MJC */
                            hostf, timef);
                (void) fflush(stdout);
                if (++outrec >= maxrec)
                    exit(0);
            }
            /*
             * when the system is down or crashed.
             */
            if (bp->ut_type == BOOT_TIME) {
                for (i = 0; i < lines; i++)
                    logouts[i] = -bp->ut_xtime;
                bootxtime = -bp->ut_xtime;
                /*
                 * TRANSLATION_NOTE
                 * Translation of this "down " will replace
                 * the %s in "- %s".  "down" is used instead
                 * of the real time session was ended, probably
                 * because the session ended by a sudden crash.
                 */
                crmsg = gettext("down ");
            }
            print = 0;    /* reset the print flag */
        }
    }
    if(uflag) {
        (void) printf(gettext("\nwtmp begins %lu \n"), buf[0].ut_xtime);
    } else {
        ct = ctime(&buf[0].ut_xtime);
        (void) printf(gettext("\nwtmp begins %24.24s \n"), ct);
    }

    /* free() called to prevent lint warning about names */
    free(names);

    return (0);
}
Esempio n. 18
0
static int doit(char *q,char qtype[2])
{
  unsigned int bpos;
  unsigned int anpos;
  unsigned int aupos;
  unsigned int arpos;
  char *control;
  char *wild;
  int flaggavesoa;
  int flagfound;
  int r;
  int flagns;
  int flagauthoritative;
  char x[20];
  uint16 u16;
  char addr[8][4];
  int addrnum;
  uint32 addrttl;
  int i;

  anpos = response_len;

  control = q;
  for (;;) {
    flagns = 0;
    flagauthoritative = 0;
    cdb_findstart(&c);
    while (r = find(control,0)) {
      if (r == -1) return 0;
      if (byte_equal(type,2,DNS_T_SOA)) flagauthoritative = 1;
      if (byte_equal(type,2,DNS_T_NS)) flagns = 1;
    }
    if (flagns) break;
    if (!*control) return 0; /* q is not within our bailiwick */
    control += *control;
    control += 1;
  }

  if (!flagauthoritative) {
    response[2] &= ~4;
    goto AUTHORITY; /* q is in a child zone */
  }


  flaggavesoa = 0;
  flagfound = 0;
  wild = q;

  for (;;) {
    addrnum = 0;
    addrttl = 0;
    cdb_findstart(&c);
    while (r = find(wild,wild != q)) {
      if (r == -1) return 0;
      flagfound = 1;
      if (flaggavesoa && byte_equal(type,2,DNS_T_SOA)) continue;
      if (byte_diff(type,2,qtype) && byte_diff(qtype,2,DNS_T_ANY) && byte_diff(type,2,DNS_T_CNAME)) continue;
      if (byte_equal(type,2,DNS_T_A) && (dlen - dpos == 4)) {
	addrttl = ttl;
	i = dns_random(addrnum + 1);
	if (i < 8) {
	  if ((i < addrnum) && (addrnum < 8))
	    byte_copy(addr[addrnum],4,addr[i]);
	  byte_copy(addr[i],4,data + dpos);
	}
	if (addrnum < 1000000) ++addrnum;
	continue;
      }
      if (!response_rstart(q,type,ttl)) return 0;
      if (byte_equal(type,2,DNS_T_NS) || byte_equal(type,2,DNS_T_CNAME) || byte_equal(type,2,DNS_T_PTR)) {
	if (!doname()) return 0;
      }
      else if (byte_equal(type,2,DNS_T_MX)) {
	if (!dobytes(2)) return 0;
	if (!doname()) return 0;
      }
      else if (byte_equal(type,2,DNS_T_SOA)) {
	if (!doname()) return 0;
	if (!doname()) return 0;
	if (!dobytes(20)) return 0;
        flaggavesoa = 1;
      }
      else
        if (!response_addbytes(data + dpos,dlen - dpos)) return 0;
      response_rfinish(RESPONSE_ANSWER);
    }
    for (i = 0;i < addrnum;++i)
      if (i < 8) {
	if (!response_rstart(q,DNS_T_A,addrttl)) return 0;
	if (!response_addbytes(addr[i],4)) return 0;
	response_rfinish(RESPONSE_ANSWER);
      }

    if (flagfound) break;
    if (wild == control) break;
    if (!*wild) break; /* impossible */
    wild += *wild;
    wild += 1;
  }

  if (!flagfound)
    response_nxdomain();


  AUTHORITY:
  aupos = response_len;

  if (flagauthoritative && (aupos == anpos)) {
    cdb_findstart(&c);
    while (r = find(control,0)) {
      if (r == -1) return 0;
      if (byte_equal(type,2,DNS_T_SOA)) {
        if (!response_rstart(control,DNS_T_SOA,ttl)) return 0;
	if (!doname()) return 0;
	if (!doname()) return 0;
	if (!dobytes(20)) return 0;
        response_rfinish(RESPONSE_AUTHORITY);
        break;
      }
    }
  }
  else
    if (want(control,DNS_T_NS)) {
      cdb_findstart(&c);
      while (r = find(control,0)) {
        if (r == -1) return 0;
        if (byte_equal(type,2,DNS_T_NS)) {
          if (!response_rstart(control,DNS_T_NS,ttl)) return 0;
	  if (!doname()) return 0;
          response_rfinish(RESPONSE_AUTHORITY);
        }
      }
    }

  arpos = response_len;

  bpos = anpos;
  while (bpos < arpos) {
    bpos = dns_packet_skipname(response,arpos,bpos); if (!bpos) return 0;
    bpos = dns_packet_copy(response,arpos,bpos,x,10); if (!bpos) return 0;
    if (byte_equal(x,2,DNS_T_NS) || byte_equal(x,2,DNS_T_MX)) {
      if (byte_equal(x,2,DNS_T_NS)) {
        if (!dns_packet_getname(response,arpos,bpos,&d1)) return 0;
      }
      else
        if (!dns_packet_getname(response,arpos,bpos + 2,&d1)) return 0;
      case_lowerb(d1,dns_domain_length(d1));
      if (want(d1,DNS_T_A)) {
	cdb_findstart(&c);
	while (r = find(d1,0)) {
          if (r == -1) return 0;
	  if (byte_equal(type,2,DNS_T_A)) {
            if (!response_rstart(d1,DNS_T_A,ttl)) return 0;
	    if (!dobytes(4)) return 0;
            response_rfinish(RESPONSE_ADDITIONAL);
	  }
        }
      }
    }
    uint16_unpack_big(x + 8,&u16);
    bpos += u16;
  }

  if (flagauthoritative && (response_len > 512)) {
    byte_zero(response + RESPONSE_ADDITIONAL,2);
    response_len = arpos;
    if (response_len > 512) {
      byte_zero(response + RESPONSE_AUTHORITY,2);
      response_len = aupos;
    }
  }

  return 1;
}
Esempio n. 19
0
Ensure equal_integers_compare_true_with_a_want_constraint() {
    Constraint *want_37 = want(label, 37);
    assert_equal(compare_constraint(want_37, 37), 1);
    destroy_constraint(want_37);
}