Exemple #1
0
/* Lock or unlock a terminal line. */
static int
tty_lock(char *path, int mode)
{
  static char saved_path[PATH_MAX];
  static int saved_lock = 0;
  struct passwd *pw;
  int fd;
  char apid[16];

  /* We do not lock standard input. */
  if ((opt_l == 0) || ((path == NULL) && (saved_lock == 0))) return(0);

  if (mode == 1) {	/* lock */
	sprintf(saved_path, "%s/LCK..%s", _PATH_LOCKD, path);
	if (tty_already_locked(saved_path)) {
		fprintf(stderr, _("slattach: /dev/%s already locked!\n"), path);
		return(-1);
	}
	if ((fd = creat(saved_path, 0644)) < 0) {
		if (errno != EEXIST)
			if (opt_q == 0) fprintf(stderr,
				_("slattach: tty_lock: (%s): %s\n"),
					saved_path, strerror(errno));
		return(-1);
	}
	sprintf(apid, "%10d\n", getpid());
	if (write(fd, apid, strlen(apid)) != strlen(apid)) {
		fprintf(stderr, _("slattach: cannot write PID file\n"));
		close(fd);
		unlink(saved_path);
		return(-1);
	}

	/* Make sure UUCP owns the lockfile.  Required by some packages. */
	if ((pw = getpwnam(_UID_UUCP)) == NULL) {
		if (opt_q == 0) fprintf(stderr, _("slattach: tty_lock: UUCP user %s unknown!\n"),
					_UID_UUCP);
		(void) close(fd);
		return(0);	/* keep the lock anyway */
	}
	if (fchown(fd, pw->pw_uid, pw->pw_gid))
		/* keep the lock anyway */;

	(void) close(fd);

	saved_lock = 1;
  } else {	/* unlock */
	if (saved_lock != 1) return(0);
	if (unlink(saved_path) < 0) {
		if (opt_q == 0) fprintf(stderr,
			"slattach: tty_unlock: (%s): %s\n", saved_path,
							strerror(errno));
		return(-1);
	}
	saved_lock = 0;
  }

  return(0);
}
/* Lock or unlock a terminal line. */
static int tty_lock(char *path, int mode)
{
	int fd;
	int ret;
	char apid[16];
	struct passwd *pw;
	static int saved_lock = 0;
	static char saved_path[PATH_MAX];

	/* We do not lock standard input. */
	if (mode == 1) {	/* lock */
		sprintf(saved_path, "%s/LCK..%s", _PATH_LOCKD, path);
		if (tty_already_locked(saved_path)) {
			fprintf(stderr, "/dev/%s already locked\n", path);
			return -1;
		}
		if ((fd = creat(saved_path, 0644)) < 0) {
			if (errno != EEXIST) {
				fprintf(stderr, "tty_lock: (%s): %s\n",
						saved_path, strerror(errno));
			}
			return -1;
		}
		sprintf(apid, "%10d\n", getpid());
		if ((ret = write(fd, apid, strlen(apid))) != strlen(apid)) {
			fprintf(stderr, "write to PID file incomplete, ret = %d\n", ret);
			close(fd);
			unlink(saved_path);
			return -1;
		}
		(void) close(fd);

		/* Make sure UUCP owns the lockfile.  Required by some packages. */
		if ((pw = getpwnam(_UID_UUCP)) == NULL) {
			fprintf(stderr, "tty_lock: UUCP user %s unknown\n", _UID_UUCP);
			return 0;
		}
		(void) chown(saved_path, pw->pw_uid, pw->pw_gid);
		saved_lock = 1;
	} else {	/* unlock */
		if (saved_lock != 1)
			return 0;
		if (unlink(saved_path) < 0) {
			fprintf(stderr, "tty_unlock: (%s): %s\n",
				saved_path, strerror(errno));
			return -1;
		}
		saved_lock = 0;
	}
	
	return 0;
}