Index: [thread] [date] [subject] [author]
  From: Marcus Sundberg <mackan@stacken.kth.se>
  To  : ggi-develop@eskimo.com
  Date: Thu, 04 Feb 1999 18:27:53 +0000

Re: Problems with Linux_kbd & 2.2.0-pre9 (fwd)

Andrew Apted wrote:
>
> One important case is when the program has forked another process
> before that piece of code is executed.  Please try that, IIRC that was
> the thing that spoilt my day :->.  What could possibly do that, you may
> ask ?  Ponder the fact, that the emu targets use mansync, and SYNC mode
> is the default...

Hmm, you are right, it doesn't work then, which isn't surprising either.
:(
Well, this code should do it in that case too:

	if (neednewvt && geteuid() && setsid() < 0) {
		int i;
		int pid = getpid();
		int ppgid = getpgid(getppid());

		/* We're not running as root and setsid() failed, so we try
		   to set our process group ID that of our parrent. */
		setpgid(pid, ppgid);
		if (setsid() < 0) {
			/* We propably have a child process... */
			for (i = 1; i < 5; i++) {
				if (getpgid(pid + i) == pid) {
					setpgid(pid + i, ppgid);
				}
			}
			if (setsid() < 0) {
				/* That failed too. Now we scan the entire
				   process space after possible childs.
				   (This takes 32ms on a P225, so we can live
				   with doing this once at startup time...) */
				for (i = 2; i < PID_MAX; i++) {
					if (getpgid(i) == pid) {
						setpgid(i, ppgid);
					}
				}
				/* Last chance, if this fails we probably
				   can't switch VTs below. */
				setsid();
			}
		}
	}

Note that the last loop is only used if we have a child process which's
pid does not match 'ourpid < childpid <= ourpid + 4'. This occurs when
the machine have been running for a while and pids are no longer
allocated
sequential. And as the comment says I think we can live with that.

Btw, there are some other issues here, which are not only related
to LibGGI:

The console(4) manpage suggests that /dev/tty<n> should be owned
by root.tty and have permissions 0622. If that is followed you
can allocate a new VT just fine (because all the vtswitching stuff
can still be done if you open with O_WRONLY), but the opening the
keyboard will fail as we obviously need read permission for that.

Also, having permissions 0622 means that any user with an account
on the machine can switch consoles even if he's logged in remotely.

All in all my suggestion is that on a multiuser machine you use XDM
to start XGGI and let the users run other programs on the console
they are started from, and set permissions on /dev/tty<n> to 
root.tty 0660.

On a singleuser machine or on a machine with really trusted users you
can put trusted users in a group "trusted" and set permissions on
/dev/tty<n> to root.trusted 0660.

//Marcus
-- 
-------------------------------+------------------------------------
        Marcus Sundberg        | http://www.stacken.kth.se/~mackan/
 Royal Institute of Technology |       Phone: +46 707 295404
       Stockholm, Sweden       |   E-Mail: mackan@stacken.kth.se

Index: [thread] [date] [subject] [author]