As far as I can tell, that should work.
(it even works if you want to change uid, using the modified version of grantpt below)
/* Sets the permissions on the slave pty suitably for use by USER. * This function is derived from the grantpt function in * sysdeps/unix/grantpt.c in glibc-2.1. */
static int pty_check_permissions(const char *name, uid_t user) { struct stat st; struct group *grp; gid_t tty_gid;
if (stat(name, &st) < 0) return 0;
/* Make sure that the user owns the device. */ if ( (st.st_uid != user) && (chown(name, user, st.st_gid) < 0) ) return 0;
/* Points to static area */ grp = getgrnam(TTY_GROUP);
if (grp) tty_gid = grp->gr_gid; else { /* If no tty group is found, use the server's gid */ werror("lshd: server_pty.c: No tty group found.\n"); tty_gid = getgid(); }
if ( (st.st_gid != tty_gid) && (chown(name, user, tty_gid) < 0)) return 0;
/* Make sure the permission mode is set to readable and writable * by the owner, and writable by the group. */
if ( ((st.st_mode & ACCESSPERMS) != (S_IRUSR | S_IWUSR | S_IWGRP)) && (chmod(name, S_IRUSR | S_IWUSR | S_IWGRP) < 0) ) return 0;
/* Everything is fine */ return 1; }
/* Returns the name of the slave tty, or NULL on failure. */
const char * pty_grantpt_uid(int master, uid_t user) { uid_t me = getuid(); if (me == user) { /* Use standard grantpt call */ if (grantpt(master) < 0) return NULL;
return ptsname(master); } else { /* Set up permissions for user */
/* Pointer to static area */ char *name = ptsname(master); return (pty_check_permissions(name, user) ? name : NULL); } }
/ Niels Möller ()
Previous text:
2003-02-15 21:52: Subject: setsid
Is there some compelling reason not to do
master = open("/dev/ptmx") grantpt(master)
before fork(), if you aren't going to change uid? Because that is what I do. Doing the grantpt() in the child requires modification* of pike.
- I have on my todo to make a Process.create_ptypair() or something,
but not today.
/ Peter Bortas