Ejemplo n.º 1
0
static int br_cmd_mldsnooping_set_age(int argc, char *const* argv)
{
	int err;
	struct timeval tv;
	
	if(strtotimeval(&tv, argv[2])){
		fprintf(stderr, "bad ageing time value\n");
	}
	
	err = br_mldsnooping_set_age(argv[1], &tv);
	if(err)
		fprintf(stderr, "set mld snooping age time failed\n");

	return err != 0;
}
Ejemplo n.º 2
0
static int br_cmd_setmaxage(int argc, char *const* argv)
{
	struct timeval tv;
	int err;

	if (strtotimeval(&tv, argv[2])) {
		fprintf(stderr, "bad max age value\n");
		return 1;
	}
	err = br_set_bridge_max_age(argv[1], &tv);
	if (err)
		fprintf(stderr, "set max age failed: %s\n",
			strerror(err));

	return err != 0;
}
Ejemplo n.º 3
0
static int br_cmd_set_igmpsnoop_ageing(int argc, char *const* argv)
{
	int err;
	struct timeval tv;
	
	if (strtotimeval(&tv, argv[2])) {
		fprintf(stderr, "bad ageing time value\n");
		return 1;
	}

	err = br_set_igmpsnoop_ageing_time(argv[1], &tv);
	if (err)
		fprintf(stderr, "set ageing time failed: %s\n",
			strerror(err));

	return err != 0;
}
Ejemplo n.º 4
0
static int br_cmd_setfd(int argc, char *const* argv)
{
	struct timeval tv;
	int err;

	if (strtotimeval(&tv, argv[2])) {
		fprintf(stderr, "bad forward delay value\n");
		return 1;
	}

	err = br_set_bridge_forward_delay(argv[1], &tv);
	if (err)
		fprintf(stderr, "set forward delay failed: %s\n",
			strerror(err));

	return err != 0;
}
static int br_cmd_sethello(char** argv)
{
	struct timeval tv;
	int err;

	if (strtotimeval(&tv, argv[2])) {
		fprintf(stderr, "bad hello timer value\n");
		return 1;
	}

	err = br_set_bridge_hello_time(argv[1], &tv);
	if (err)
		fprintf(stderr, "set hello timer failed: %s\n",
			strerror(err));

	return err != 0;
}
Ejemplo n.º 6
0
static unsigned long str_to_jiffies(const char *time_str)
{
	struct timeval tv;
	strtotimeval(&tv, time_str);
	return __tv_to_jiffies(&tv);
}
Ejemplo n.º 7
0
int main(int argc, char *argv[])
{
	struct itimerval timeout, old_timer;
	int have_timeout = 0;
	int type = LOCK_EX;
	int block = 0;
	int open_flags = 0;
	int fd = -1;
	int opt, ix;
	int do_close = 0;
	int status;
	/*
	 * The default exit code for lock conflict or timeout
	 * is specified in man flock.1
	 */
	int conflict_exit_code = 1;
	char **cmd_argv = NULL, *sh_c_argv[4];
	const char *filename = NULL;
	struct sigaction sa, old_sa;

	static const struct option long_options[] = {
		{"shared", no_argument, NULL, 's'},
		{"exclusive", no_argument, NULL, 'x'},
		{"unlock", no_argument, NULL, 'u'},
		{"nonblocking", no_argument, NULL, 'n'},
		{"nb", no_argument, NULL, 'n'},
		{"timeout", required_argument, NULL, 'w'},
		{"wait", required_argument, NULL, 'w'},
		{"conflict-exit-code", required_argument, NULL, 'E'},
		{"close", no_argument, NULL, 'o'},
		{"help", no_argument, NULL, 'h'},
		{"version", no_argument, NULL, 'V'},
		{NULL, 0, NULL, 0}
	};

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

	if (argc < 2)
		usage(EX_USAGE);

	memset(&timeout, 0, sizeof timeout);

	optopt = 0;
	while ((opt =
		getopt_long(argc, argv, "+sexnouw:E:hV?", long_options,
			    &ix)) != EOF) {
		switch (opt) {
		case 's':
			type = LOCK_SH;
			break;
		case 'e':
		case 'x':
			type = LOCK_EX;
			break;
		case 'u':
			type = LOCK_UN;
			break;
		case 'o':
			do_close = 1;
			break;
		case 'n':
			block = LOCK_NB;
			break;
		case 'w':
			have_timeout = 1;
			strtotimeval(optarg, &timeout.it_value);
			break;
		case 'E':
			conflict_exit_code = strtos32_or_err(optarg,
				_("invalid exit code"));
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EX_OK);
		default:
			/* optopt will be set if this was an unrecognized
			 * option, i.e.  *not* 'h' or '?
			 */
			usage(optopt ? EX_USAGE : 0);
			break;
		}
	}

	if (argc > optind + 1) {
		/* Run command */
		if (!strcmp(argv[optind + 1], "-c") ||
		    !strcmp(argv[optind + 1], "--command")) {
			if (argc != optind + 3)
				errx(EX_USAGE,
				     _("%s requires exactly one command argument"),
				     argv[optind + 1]);
			cmd_argv = sh_c_argv;
			cmd_argv[0] = getenv("SHELL");
			if (!cmd_argv[0] || !*cmd_argv[0])
				cmd_argv[0] = _PATH_BSHELL;
			cmd_argv[1] = "-c";
			cmd_argv[2] = argv[optind + 2];
			cmd_argv[3] = 0;
		} else {
			cmd_argv = &argv[optind + 1];
		}

		filename = argv[optind];
		fd = open_file(filename, &open_flags);

	} else if (optind < argc) {
		/* Use provided file descriptor */
		fd = (int)strtol_or_err(argv[optind], "bad number");
	} else {
		/* Bad options */
		errx(EX_USAGE, _("requires file descriptor, file or directory"));
	}

	if (have_timeout) {
		if (timeout.it_value.tv_sec == 0 &&
		    timeout.it_value.tv_usec == 0) {
			/* -w 0 is equivalent to -n; this has to be
			 * special-cased because setting an itimer to zero
			 * means disabled!
			 */
			have_timeout = 0;
			block = LOCK_NB;
		} else
			setup_timer(&timeout, &old_timer, &sa, &old_sa);
	}

	while (flock(fd, type | block)) {
		switch (errno) {
		case EWOULDBLOCK:
			/* -n option set and failed to lock. */
			exit(conflict_exit_code);
		case EINTR:
			/* Signal received */
			if (timeout_expired)
				/* -w option set and failed to lock. */
				exit(conflict_exit_code);
			/* otherwise try again */
			continue;
		case EIO:
			/* Probably NFSv4 where flock() is emulated by fcntl().
			 * Let's try to reopen in read-write mode.
			 */
			if (!(open_flags & O_RDWR) &&
			    type != LOCK_SH &&
			    access(filename, R_OK | W_OK) == 0) {

				close(fd);
				open_flags = O_RDWR;
				fd = open_file(filename, &open_flags);

				if (open_flags & O_RDWR)
					break;
			}
			/* go through */
		default:
			/* Other errors */
			if (filename)
				warn("%s", filename);
			else
				warn("%d", fd);
			exit((errno == ENOLCK
			      || errno == ENOMEM) ? EX_OSERR : EX_DATAERR);
		}
	}

	if (have_timeout)
		cancel_timer(&old_timer, &old_sa);

	status = EX_OK;

	if (cmd_argv) {
		pid_t w, f;
		/* Clear any inherited settings */
		signal(SIGCHLD, SIG_DFL);
		f = fork();

		if (f < 0) {
			err(EX_OSERR, _("fork failed"));
		} else if (f == 0) {
			if (do_close)
				close(fd);
			execvp(cmd_argv[0], cmd_argv);
			/* execvp() failed */
			warn("%s", cmd_argv[0]);
			_exit((errno == ENOMEM) ? EX_OSERR : EX_UNAVAILABLE);
		} else {
			do {
				w = waitpid(f, &status, 0);
				if (w == -1 && errno != EINTR)
					break;
			} while (w != f);

			if (w == -1) {
				status = EXIT_FAILURE;
				warn(_("waitpid failed"));
			} else if (WIFEXITED(status))
				status = WEXITSTATUS(status);
			else if (WIFSIGNALED(status))
				status = WTERMSIG(status) + 128;
			else
				/* WTF? */
				status = EX_OSERR;
		}
	}

	return status;
}
Ejemplo n.º 8
0
int main(int argc, char *argv[])
{
  struct itimerval timeout, old_timer;
  int have_timeout = 0;
  int type = LOCK_EX;
  int block = 0;
  int fd = -1;
  int opt, ix;
  int do_close = 0;
  int err;
  int status;
  char *eon;
  char **cmd_argv = NULL, *sh_c_argv[4];
  const char *filename = NULL;
  struct sigaction sa, old_sa;

  program = argv[0];

  if ( argc < 2 )
    usage(EX_USAGE);

  memset(&timeout, 0, sizeof timeout);

  optopt = 0;
  while ( (opt = getopt_long(argc, argv, "+sexnouw:hV?", long_options, &ix)) != EOF ) {
    switch(opt) {
    case 's':
      type = LOCK_SH;
      break;
    case 'e':
    case 'x':
      type = LOCK_EX;
      break;
    case 'u':
      type = LOCK_UN;
      break;
    case 'o':
      do_close = 1;
      break;
    case 'n':
      block = LOCK_NB;
      break;
    case 'w':
      have_timeout = 1;
      eon = strtotimeval(optarg, &timeout.it_value);
      if ( *eon )
	usage(EX_USAGE);
      break;
    case 'V':
      printf("flock (%s)\n", PACKAGE_STRING);
      exit(0);
    default:
      /* optopt will be set if this was an unrecognized option, i.e. *not* 'h' or '?' */
      usage(optopt ? EX_USAGE : 0);
      break;
    }
  }

  if ( argc > optind+1 ) {
    /* Run command */

    if ( !strcmp(argv[optind+1], "-c") ||
	 !strcmp(argv[optind+1], "--command") ) {

      if ( argc != optind+3 ) {
	fprintf(stderr, _("%s: %s requires exactly one command argument\n"),
		program, argv[optind+1]);
	exit(EX_USAGE);
      }

      cmd_argv = sh_c_argv;

      cmd_argv[0] = getenv("SHELL");
      if ( !cmd_argv[0] || !*cmd_argv[0] )
	cmd_argv[0] = _PATH_BSHELL;

      cmd_argv[1] = "-c";
      cmd_argv[2] = argv[optind+2];
      cmd_argv[3] = 0;
    } else {
      cmd_argv = &argv[optind+1];
    }

    filename = argv[optind];
    fd = open(filename, O_RDONLY|O_NOCTTY|O_CREAT, 0666);
    /* Linux doesn't like O_CREAT on a directory, even though it should be a
       no-op */
    if (fd < 0 && errno == EISDIR)
        fd = open(filename, O_RDONLY|O_NOCTTY);

    if ( fd < 0 ) {
      err = errno;
      fprintf(stderr, _("%s: cannot open lock file %s: %s\n"),
	      program, argv[optind], strerror(err));
      exit((err == ENOMEM||err == EMFILE||err == ENFILE) ? EX_OSERR :
	   (err == EROFS||err == ENOSPC) ? EX_CANTCREAT :
	   EX_NOINPUT);
    }

  } else if (optind < argc) {
    /* Use provided file descriptor */

    fd = (int)strtol(argv[optind], &eon, 10);
    if ( *eon || !argv[optind] ) {
      fprintf(stderr, _("%s: bad number: %s\n"), program, argv[optind]);
      exit(EX_USAGE);
    }

  } else {
    /* Bad options */

    fprintf(stderr, _("%s: requires file descriptor, file or directory\n"),
		program);
    exit(EX_USAGE);
  }


  if ( have_timeout ) {
    if ( timeout.it_value.tv_sec == 0 &&
	 timeout.it_value.tv_usec == 0 ) {
      /* -w 0 is equivalent to -n; this has to be special-cased
	 because setting an itimer to zero means disabled! */

      have_timeout = 0;
      block = LOCK_NB;
    } else {
      memset(&sa, 0, sizeof sa);

      sa.sa_handler = timeout_handler;
      sa.sa_flags   = SA_RESETHAND;
      sigaction(SIGALRM, &sa, &old_sa);

      setitimer(ITIMER_REAL, &timeout, &old_timer);
    }
  }

  while ( flock(fd, type|block) ) {
    switch( (err = errno) ) {
    case EWOULDBLOCK:		/* -n option set and failed to lock */
      exit(1);
    case EINTR:			/* Signal received */
      if ( timeout_expired )
	exit(1);		/* -w option set and failed to lock */
      continue;			/* otherwise try again */
    default:			/* Other errors */
      if ( filename )
	fprintf(stderr, "%s: %s: %s\n", program, filename, strerror(err));
      else
	fprintf(stderr, "%s: %d: %s\n", program, fd, strerror(err));
      exit((err == ENOLCK||err == ENOMEM) ? EX_OSERR : EX_DATAERR);
    }
  }

  if ( have_timeout ) {
    setitimer(ITIMER_REAL, &old_timer, NULL); /* Cancel itimer */
    sigaction(SIGALRM, &old_sa, NULL); /* Cancel signal handler */
  }

  status = 0;

  if ( cmd_argv ) {
    pid_t w, f;

    /* Clear any inherited settings */
    signal(SIGCHLD, SIG_DFL);
    f = fork();

    if ( f < 0 ) {
      err = errno;
      fprintf(stderr, _("%s: fork failed: %s\n"), program, strerror(err));
      exit(EX_OSERR);
    } else if ( f == 0 ) {
      if ( do_close )
	close(fd);
      err = errno;
      execvp(cmd_argv[0], cmd_argv);
      /* execvp() failed */
      fprintf(stderr, "%s: %s: %s\n", program, cmd_argv[0], strerror(err));
      _exit((err == ENOMEM) ? EX_OSERR: EX_UNAVAILABLE);
    } else {
      do {
	w = waitpid(f, &status, 0);
	if (w == -1 && errno != EINTR)
	  break;
      } while ( w != f );

      if (w == -1) {
	err = errno;
	status = EXIT_FAILURE;
	fprintf(stderr, "%s: waitpid failed: %s\n", program, strerror(err));
      } else if ( WIFEXITED(status) )
	status = WEXITSTATUS(status);
      else if ( WIFSIGNALED(status) )
	status = WTERMSIG(status) + 128;
      else
	status = EX_OSERR;	/* WTF? */
    }
  }

  return status;
}
Ejemplo n.º 9
0
int
readcmd(int argc, char **argv)
{
	char **ap;
	char c;
	int rflag;
	char *prompt;
	char *p;
	int startloc;
	int newloc;
	int status;
	int timeout;
	int i;
	fd_set set;
	struct timeval ts, t0, t1, to;

	ts.tv_sec = ts.tv_usec = 0;

	rflag = 0;
	timeout = 0;
	prompt = NULL;
	while ((i = nextopt("p:rt:")) != '\0') {
		switch(i) {
		case 'p':
			prompt = optionarg;
			break;
		case 't':
			p = strtotimeval(optionarg, &ts);
			if (*p || (!ts.tv_sec && !ts.tv_usec))
				sh_error("invalid timeout");
			timeout = 1;
			break;
		case 'r':
			rflag = 1;
			break;
		default:
			break;
		}
	}
	if (prompt && isatty(0)) {
		out2str(prompt);
#ifdef FLUSHERR
		flushall();
#endif
	}
	if (*(ap = argptr) == NULL)
		sh_error("arg count");

	status = 0;
	if (timeout) {
		gettimeofday(&t0, NULL);

		/* ts += t0; */
		ts.tv_usec += t0.tv_usec;
		while (ts.tv_usec >= 1000000) {
			ts.tv_sec++;
			ts.tv_usec -= 1000000;
		}
		ts.tv_sec += t0.tv_sec;
	}
	STARTSTACKSTR(p);

	goto start;

	for (;;) {
		if (timeout) {
			gettimeofday(&t1, NULL);
			if (t1.tv_sec > ts.tv_sec ||
			    (t1.tv_sec == ts.tv_sec &&
			     t1.tv_usec >= ts.tv_usec)) {
				status = 1;
				break;	/* Timeout! */
			}

			/* to = ts - t1; */
			if (ts.tv_usec >= t1.tv_usec) {
				to.tv_usec = ts.tv_usec - t1.tv_usec;
				to.tv_sec  = ts.tv_sec - t1.tv_sec;
			} else {
				to.tv_usec = ts.tv_usec - t1.tv_usec + 1000000;
				to.tv_sec  = ts.tv_sec - t1.tv_sec - 1;
			}

			FD_ZERO(&set);
			FD_SET(0, &set);
			if (select(1, &set, NULL, NULL, &to) != 1) {
				status = 1;
				break; /* Timeout! */
			}
		}
		switch (read(0, &c, 1)) {
		case 1:
			break;
		default:
			if (errno == EINTR && !pendingsigs)
				continue;
				/* fall through */
		case 0:
			status = 1;
			goto out;
		}
		if (c == '\0')
			continue;
		if (newloc >= startloc) {
			if (c == '\n')
				goto resetbs;
			goto put;
		}
		if (!rflag && c == '\\') {
			newloc = p - (char *)stackblock();
			continue;
		}
		if (c == '\n')
			break;
put:
		CHECKSTRSPACE(2, p);
		if (strchr(qchars, c))
			USTPUTC(CTLESC, p);
		USTPUTC(c, p);

		if (newloc >= startloc) {
resetbs:
			recordregion(startloc, newloc, 0);
start:
			startloc = p - (char *)stackblock();
			newloc = startloc - 1;
		}
	}
out:
	recordregion(startloc, p - (char *)stackblock(), 0);
	STACKSTRNUL(p);
	readcmd_handle_line(p + 1, ap);
	return status;
}
Ejemplo n.º 10
0
int date_main(int argc, char *argv[])
{
	int c;
    int res;
	struct tm tm;
	time_t t;
	struct timeval tv;
    struct timespec ts;
	char strbuf[260];
    int fd;

    int useutc = 0;

    tzset();

    do {
        c = getopt(argc, argv, "us:");
        if (c == EOF)
            break;
        switch (c) {
        case 'u':
            useutc = 1;
            break;
        case 's':
            settime(optarg);
            break;
        case '?':
            fprintf(stderr, "%s: invalid option -%c\n",
                argv[0], optopt);
            exit(1);
        }
    } while (1);
    if(optind + 2 < argc) {
        fprintf(stderr,"%s [-u] [date]\n", argv[0]);
        return 1;
    }

    int hasfmt = argc == optind + 1 && argv[optind][0] == '+';
    if(optind == argc || hasfmt) {
        char buf[2000];
        time(&t);
        if (useutc) {
            gmtime_r(&t, &tm);
            strftime(strbuf, sizeof(strbuf),
                     (hasfmt ? argv[optind] + 1 : "%a %b %e %H:%M:%S GMT %Y"),
                     &tm);
        } else {
            localtime_r(&t, &tm);
            strftime(strbuf, sizeof(strbuf),
                     (hasfmt ? argv[optind] + 1 : "%a %b %e %H:%M:%S %Z %Y"),
                     &tm);
        }
        printf("%s\n", strbuf);
    }
    else if(optind + 1 == argc) {
#if 0
        struct tm *tmptr;
        tmptr = getdate(argv[optind]);
        if(tmptr == NULL) {
            fprintf(stderr,"getdate_r failed\n");
            return 1;
        }
        tm = *tmptr;
#if 0
        if(getdate_r(argv[optind], &tm) < 0) {
            fprintf(stderr,"getdate_r failed %s\n", strerror(errno));
            return 1;
        }
#endif
#endif
        //strptime(argv[optind], NULL, &tm);
        //tv.tv_sec = mktime(&tm);
        //tv.tv_usec = 0;
        strtotimeval(argv[optind], &tv);
        printf("time %s -> %d.%d\n", argv[optind], tv.tv_sec, tv.tv_usec);
        fd = open("/dev/alarm", O_RDWR);
        ts.tv_sec = tv.tv_sec;
        ts.tv_nsec = tv.tv_usec * 1000;
        res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts);
        //res = settimeofday(&tv, NULL);
        if(res < 0) {
            fprintf(stderr,"settimeofday failed %s\n", strerror(errno));
            return 1;
        }
    }
    else {
        fprintf(stderr,"%s [-s 20070325.123456] [-u] [date]\n", argv[0]);
        return 1;
    }

    return 0;
}