Пример #1
0
static void watch_file(const char *filename, struct stat *old)
{
	do {
		roll_file(filename, old);
		xusleep(250000);
	} while(1);
}
Пример #2
0
void
unlinkdUnlink(const char *path)
{
    char buf[MAXPATHLEN];
    int l;
    int x;
    static int queuelen = 0;
    if (unlinkd_wfd < 0) {
	debug_trap("unlinkdUnlink: unlinkd_wfd < 0");
	safeunlink(path, 0);
	return;
    }
    /*
     * If the queue length is greater than our limit, then
     * we pause for up to 10ms, hoping that unlinkd
     * has some feedback for us.  Maybe it just needs a slice
     * of the CPU's time.
     */
    if (queuelen >= UNLINKD_QUEUE_LIMIT)
	xusleep(10000);
    /*
     * If there is at least one outstanding unlink request, then
     * try to read a response.  If there's nothing to read we'll
     * get an EWOULDBLOCK or whatever.  If we get a response, then
     * decrement the queue size by the number of newlines read.
     */
    if (queuelen > 0) {
	int x;
	int i;
	char rbuf[512];
#ifdef _SQUID_MSWIN_
	x = recv(unlinkd_rfd, rbuf, 511, 0);
#else
	x = read(unlinkd_rfd, rbuf, 511);
#endif
	if (x > 0) {
	    rbuf[x] = '\0';
	    for (i = 0; i < x; i++)
		if ('\n' == rbuf[i])
		    queuelen--;
	    assert(queuelen >= 0);
	}
    }
    l = strlen(path);
    assert(l < MAXPATHLEN);
    xstrncpy(buf, path, MAXPATHLEN);
    buf[l++] = '\n';
#ifdef _SQUID_MSWIN_
    x = send(unlinkd_wfd, buf, l, 0);
#else
    x = write(unlinkd_wfd, buf, l);
#endif
    if (x < 0) {
	debug(2, 1) ("unlinkdUnlink: write FD %d failed: %s\n",
	    unlinkd_wfd, xstrerror());
	safeunlink(path, 0);
	return;
    } else if (x != l) {
	debug(2, 1) ("unlinkdUnlink: FD %d only wrote %d of %d bytes\n",
	    unlinkd_wfd, x, l);
	safeunlink(path, 0);
	return;
    }
    statCounter.unlink.requests++;
    statCounter.syscalls.disk.unlinks++;
    queuelen++;
}
void
startup(int argc, char *argv[])
{
	Dampkt		dam_p;
	char		tmp;
	char		*ep = &tmp;
	ulong		start = 0;
	long		sluimer;
	int		period = 0;
	int		tmpperiod, tmp_alpha, tmp_beta, tmp_ftable_alpha,
			tmp_eij, tmp_maxhops;


	strncpy(dam_myID, (char *)NIC_OUI, DAM_ID_LEN);
	hdlr_install();
	print("DAM node [%s] installed vector code.\n", dam_myID);

	devmac_ctl(NIC_NCR_WRITE, NIC_CMD_POWERUP, 0);
	print("DAM node [%s] powered up NIC.\n", dam_myID);

	dam_period = DAM_PROTOCOL_PERIOD;
	if (argc == 6)
	{
		tmpperiod = strtol(argv[0], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid DAM period supplied as argument.\n");
		}
		else
		{
			dam_period = tmpperiod;
			printf("Set dam_period to [%d] usecs\n", dam_period);
		}

		tmp_alpha = strtol(argv[1], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR alpha supplied as argument.\n");
		}
		
		tmp_beta = strtol(argv[2], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR beta supplied as argument.\n");
		}

		tmp_ftable_alpha = strtol(argv[3], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR ftable_alpha supplied as argument.\n");
		}

		tmp_eij = strtol(argv[4], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR eij supplied as argument.\n");
		}

		tmp_maxhops = strtol(argv[5], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR maxhops supplied as argument.\n");
		}

		Ear = ear_init(dam_myID, 0, tmp_alpha, tmp_beta, tmp_ftable_alpha,
				tmp_eij, tmp_maxhops,
			(uchar)devloc_getxloc(), (uchar)devloc_getyloc(), (uchar)devloc_getzloc());
		fprintf(stderr, "DAM node [%s] completed EAR init with runtime args:\n"
			"\talpha=%d\n\tbeta=%d\n\tftable_alpha=%d\n\teij=%d\n\tmaxhops=%d\n\n\n\n", 
			dam_myID, tmp_alpha, tmp_beta, tmp_ftable_alpha, tmp_eij, tmp_maxhops);

	}
	else
	{
		Ear = ear_init(dam_myID, 0, 1, 50, 1, 1 /* eij */, 8 /* maxhops */,
			(uchar)devloc_getxloc(), (uchar)devloc_getyloc(), (uchar)devloc_getzloc());
		print("DAM node [%s] completed EAR init using defaults.\n\n\n", dam_myID);
	}


	/*							*/
	/*	Implemented to mirror description in paper	*/
	/*	All variables beginning w/ dam_ correspond	*/
	/*	to variables in the paper's algorithm descr.	*/
	/*							*/
	while (1)
	{
		start = devrtc_getusecs();
		dam_p.timestamp = start;

		/*									*/
		/*	Write the log for the previous period. We want all actions to	*/
		/*	be in the timed loop, and though this log writing may seem to	*/
		/*	be not inherent to application, you can think of it as some	*/
		/*	post-peak detection actions that the algorithm must perform.	*/
		/*									*/
		if ((period > 0) && dam_participating)
		{
			if (strlen(dam_sink) > 0)
			{
				int		id;
				char		tmp;
				char		*ep = &tmp;

				id = strtol(dam_leaderID, &ep, 0);
				ear_response(Ear, dam_sink, NULL, 0, id);
			}
//print("\n\ndam_sink = [%s]\n\n\n", dam_sink);
		
			write_log(period - 1);
		}

		/*								*/
		/*	The values in this case are in Lux (see test.m)		*/
		/*	Noise floor is 0.1 Lux. DAM_THRESHOLD_ELECTION is	*/
		/*	thus set to 10 Lux.					*/
		/*								*/
		dam_myPr = devsignal_read(LIGHT_SENSOR);

		/*								*/
		/*	Algorithm description in PARC paper does not reset	*/
		/*	maxPrHeard. For each DAM period, until a packet is	*/
		/*	received, or our local reading is > threshold, the	*/
		/*	maxPrHeard should be 0.					*/
		/*								*/
		dam_maxPrHeard = 0;

		if (dam_myPr > DAM_THRESHOLD_ELECTION)
		{
LOGMARK(12);
			dam_participating = TRUE;
			dam_maxPrHeard = dam_myPr;
			strncpy(dam_leaderID, dam_myID, DAM_ID_LEN);
			dam_p.maxPr = dam_p.transPr = dam_myPr;
			strncpy((char *)dam_p.transID, dam_myID, DAM_ID_LEN);
			strncpy((char *)dam_p.maxID, dam_myID, DAM_ID_LEN);

			dam_broadcast(&dam_p);
LOGMARK(13);
		}
		else
		{
			dam_participating = FALSE;
		}

		sluimer = dam_period - (devrtc_getusecs() - start);
		sluimer = max(sluimer, 0);
LOGMARK(2);
		xusleep(sluimer);
LOGMARK(3);
		period++;
	}

	return;		
}
Пример #4
0
int main(int argc, char **argv)
{
    struct rtcwake_control ctl = {
        .mode_str = "suspend",		/* default mode */
        .adjfile = _PATH_ADJTIME,
        .clock_mode = CM_AUTO
    };
    char *devname = DEFAULT_RTC_DEVICE;
    unsigned seconds = 0;
    int suspend = SYSFS_MODE;
    int rc = EXIT_SUCCESS;
    int t;
    int fd;
    time_t alarm = 0;
    enum {
        OPT_DATE = CHAR_MAX + 1,
        OPT_LIST
    };
    static const struct option long_options[] = {
        {"adjfile",     required_argument,      0, 'A'},
        {"auto",	no_argument,		0, 'a'},
        {"dry-run",	no_argument,		0, 'n'},
        {"local",	no_argument,		0, 'l'},
        {"utc",		no_argument,		0, 'u'},
        {"verbose",	no_argument,		0, 'v'},
        {"version",	no_argument,		0, 'V'},
        {"help",	no_argument,		0, 'h'},
        {"mode",	required_argument,	0, 'm'},
        {"device",	required_argument,	0, 'd'},
        {"seconds",	required_argument,	0, 's'},
        {"time",	required_argument,	0, 't'},
        {"date",	required_argument,	0, OPT_DATE},
        {"list-modes",	no_argument,		0, OPT_LIST},
        {0,		0,			0, 0  }
    };
    static const ul_excl_t excl[] = {
        { 'a', 'l', 'u' },
        { 's', 't', OPT_DATE },
    };
    int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

    setlocale(LC_ALL, "");
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);
    atexit(close_stdout);

    while ((t = getopt_long(argc, argv, "A:ahd:lm:ns:t:uVv",
                            long_options, NULL)) != EOF) {
        err_exclusive_options(t, long_options, excl, excl_st);
        switch (t) {
        case 'A':
            /* for better compatibility with hwclock */
            ctl.adjfile = optarg;
            break;
        case 'a':
            ctl.clock_mode = CM_AUTO;
            break;
        case 'd':
            devname = optarg;
            break;
        case 'l':
            ctl.clock_mode = CM_LOCAL;
            break;

        case OPT_LIST:
            list_modes(&ctl);
            return EXIT_SUCCESS;

        case 'm':
            if ((suspend = get_rtc_mode(&ctl, optarg)) < 0)
                errx(EXIT_FAILURE, _("unrecognized suspend state '%s'"), optarg);
            ctl.mode_str = optarg;
            break;
        case 'n':
            ctl.dryrun = 1;
            break;
        case 's':
            /* alarm time, seconds-to-sleep (relative) */
            seconds = strtou32_or_err(optarg, _("invalid seconds argument"));
            break;
        case 't':
            /* alarm time, time_t (absolute, seconds since epoch) */
            alarm = strtou32_or_err(optarg, _("invalid time argument"));
            break;
        case OPT_DATE:
        {   /* alarm time, see timestamp format from manual */
            usec_t p;
            if (parse_timestamp(optarg, &p) < 0)
                errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg);
            alarm = (time_t) (p / 1000000);
            break;
        }
        case 'u':
            ctl.clock_mode = CM_UTC;
            break;
        case 'v':
            ctl.verbose = 1;
            break;
        case 'V':
            printf(UTIL_LINUX_VERSION);
            exit(EXIT_SUCCESS);
        case 'h':
            usage(stdout);
        default:
            usage(stderr);
        }
    }

    if (ctl.clock_mode == CM_AUTO) {
        if (read_clock_mode(&ctl) < 0) {
            printf(_("%s: assuming RTC uses UTC ...\n"),
                   program_invocation_short_name);
            ctl.clock_mode = CM_UTC;
        }
    }

    if (ctl.verbose)
        printf("%s",  ctl.clock_mode == CM_UTC ? _("Using UTC time.\n") :
               _("Using local time.\n"));

    if (!alarm && !seconds && suspend != DISABLE_MODE && suspend != SHOW_MODE)
        errx(EXIT_FAILURE, _("must provide wake time (see --seconds, --time and --date options)"));

    /* device must exist and (if we'll sleep) be wakeup-enabled */
    fd = open_dev_rtc(devname);

    if (suspend != ON_MODE && suspend != NO_MODE && !is_wakeup_enabled(devname))
        errx(EXIT_FAILURE, _("%s not enabled for wakeup events"), devname);

    /* relative or absolute alarm time, normalized to time_t */
    if (get_basetimes(&ctl, fd) < 0)
        exit(EXIT_FAILURE);

    if (ctl.verbose)
        printf(_("alarm %ld, sys_time %ld, rtc_time %ld, seconds %u\n"),
               alarm, ctl.sys_time, ctl.rtc_time, seconds);

    if (suspend != DISABLE_MODE && suspend != SHOW_MODE) {
        /* perform alarm setup when the show or disable modes are not set */
        if (alarm) {
            if (alarm < ctl.sys_time)
                errx(EXIT_FAILURE, _("time doesn't go backward to %s"),
                     ctime(&alarm));
            alarm += ctl.sys_time - ctl.rtc_time;
        } else
            alarm = ctl.rtc_time + seconds + 1;

        if (setup_alarm(&ctl, fd, &alarm) < 0)
            exit(EXIT_FAILURE);

        if (suspend == NO_MODE || suspend == ON_MODE)
            printf(_("%s: wakeup using %s at %s"),
                   program_invocation_short_name, devname,
                   ctime(&alarm));
        else
            printf(_("%s: wakeup from \"%s\" using %s at %s"),
                   program_invocation_short_name, ctl.mode_str, devname,
                   ctime(&alarm));
        fflush(stdout);
        xusleep(10 * 1000);
    }

    switch (suspend) {
    case NO_MODE:
        if (ctl.verbose)
            printf(_("suspend mode: no; leaving\n"));
        ctl.dryrun = 1;	/* to skip disabling alarm at the end */
        break;
    case OFF_MODE:
    {
        char *arg[5];
        int i = 0;

        if (ctl.verbose)
            printf(_("suspend mode: off; executing %s\n"),
                   _PATH_SHUTDOWN);
        arg[i++] = _PATH_SHUTDOWN;
        arg[i++] = "-h";
        arg[i++] = "-P";
        arg[i++] = "now";
        arg[i]   = NULL;
        if (!ctl.dryrun) {
            execv(arg[0], arg);
            warn(_("failed to execute %s"), _PATH_SHUTDOWN);
            rc = EXIT_FAILURE;
        }
        break;
    }
    case ON_MODE:
    {
        unsigned long data;

        if (ctl.verbose)
            printf(_("suspend mode: on; reading rtc\n"));
        if (!ctl.dryrun) {
            do {
                t = read(fd, &data, sizeof data);
                if (t < 0) {
                    warn(_("rtc read failed"));
                    break;
                }
                if (ctl.verbose)
                    printf("... %s: %03lx\n", devname, data);
            } while (!(data & RTC_AF));
        }
        break;
    }
    case DISABLE_MODE:
        /* just break, alarm gets disabled in the end */
        if (ctl.verbose)
            printf(_("suspend mode: disable; disabling alarm\n"));
        break;
    case SHOW_MODE:
        if (ctl.verbose)
            printf(_("suspend mode: show; printing alarm info\n"));
        if (print_alarm(&ctl, fd))
            rc = EXIT_FAILURE;
        ctl.dryrun = 1;	/* don't really disable alarm in the end, just show */
        break;
    default:
        if (ctl.verbose)
            printf(_("suspend mode: %s; suspending system\n"), ctl.mode_str);
        sync();
        suspend_system(&ctl);
    }

    if (!ctl.dryrun) {
        struct rtc_wkalrm wake;

        if (ioctl(fd, RTC_WKALM_RD, &wake) < 0) {
            warn(_("read rtc alarm failed"));
            rc = EXIT_FAILURE;
        } else {
            wake.enabled = 0;
            if (ioctl(fd, RTC_WKALM_SET, &wake) < 0) {
                warn(_("disable rtc alarm interrupt failed"));
                rc = EXIT_FAILURE;
            }
        }
    }

    close(fd);
    return rc;
}