executing newgrp from python in current shell possible?

  • Thread starter Svenn Are Bjerkem
  • Start date
S

Svenn Are Bjerkem

Hi,
as a user on a linux system I am member of the groups "users" and
"design" with users as my default group. To controll the accessibility
of some parts of the file system, creation of files and directories in
those parts must be done with group "design". This is currently done
manually with "newgrp design" on the command line before doing
anything else. I have been looking for a way to execute this command
as a part of a script, but it seems that the changes are only valid in
the context of the script and when the script exits, the current shell
still have the original "users" group setting. It looks like the
command spawns a new command line in the context of the current xterm
as I get back to my old command line when exiting (instead of the
xterm dissapearing)

A possible alternative could be to have the python script launch a new
shell, but I still have the problem to set the group id for that new
shell (lack of python knowledge). I am happy for any advice. Maybe it
is simply not possible to do this kind of operation from a script in
python on linux.
 
Z

Zentrader

I have been looking for a way to execute this command
as a part of a script, but it seems that the changes are only valid in
the context of the script and when the script exits, the current shell
still have the original "users" group setting.

I don't think you would want it any other way. Would you want a user
to be able to change the group and have it remain permanently? Who's
going to remember whether they were last in "A" or "B", and it opens
up oportunities for the practical joker when you go to the restroom
and leave the terminal on. Put the "change the group" code into a
separate function in a separate file (with only you as the owner) and
call it whenever you want to change groups.
 
S

Svenn Are Bjerkem

I don't think you would want it any other way. Would you want a user
to be able to change the group and have it remain permanently? Who's
going to remember whether they were last in "A" or "B", and it opens
up oportunities for the practical joker when you go to the restroom
and leave the terminal on. Put the "change the group" code into a
separate function in a separate file (with only you as the owner) and
call it whenever you want to change groups.

I am trying to create a script that make it easy for users in a design
team to create files that belong to the same group, but retain the
users uid. In order to make it visible that the user is creating files
with a different gid, the script will change the prompt to indicate
so. In a tcl solution I have now, the users home is changed to the
design area as some tools are reading and writing setup files into
$HOME. I have not found a way to change the gid in tcl so I turned to
python in hope that this scripting language could do so.

The tcl solution spawns a new tcsh after setting several environment
variables and works quite well except for not being able to change
gid. And it is also a wish from my side to port this script to python.

Is your suggestion to put "newgrp design" into a new file and then
exec this file in my python script? What happens to the group id of
the shell that called the python script in this case? I would try to
avoid spawning a new tcsh as this make execution of tools on a remote
computer difficult as the handover of command line arguments does not
seem to be handed over to the newly spawned shell. I may be
understanding something wrongly here.
 
D

Dennis Lee Bieber

Is your suggestion to put "newgrp design" into a new file and then
exec this file in my python script? What happens to the group id of
the shell that called the python script in this case? I would try to
avoid spawning a new tcsh as this make execution of tools on a remote
computer difficult as the handover of command line arguments does not
seem to be handed over to the newly spawned shell. I may be
understanding something wrongly here.

I suspect anything that is using an os.spawn, popen, or os.system
(or subprocess) call will have a problem for you... These may be
creating a new shell that inherits from your calling shell -- but
changes made in that shell will not propagate back up to the parent.

Look at (in Python 2.4) section 6.1.1 of the library reference (the
os and related modules)...

"""
setegid( egid)
Set the current process's effective group id. Availability: Unix.

seteuid( euid)
Set the current process's effective user id. Availability: Unix.

setgid( gid)
Set the current process' group id. Availability: Unix.

setgroups( groups)
Set the list of supplemental group ids associated with the current
process to groups. groups must be a sequence, and each element must be
an integer identifying a group. This operation is typical available only
to the superuser. Availability: Unix. New in version 2.2.
"""

IOWs, don't try to change it by spawning a shell command to set the
group -- directly invoke the low-level API from within your program
itself.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
K

Karthik Gurusamy

I am trying to create a script that make it easy for users in a design
team to create files that belong to the same group, but retain the
users uid. In order to make it visible that the user is creating files
with a different gid, the script will change the prompt to indicate
so. In a tcl solution I have now, the users home is changed to the
design area as some tools are reading and writing setup files into
$HOME. I have not found a way to change the gid in tcl so I turned to
python in hope that this scripting language could do so.

The tcl solution spawns a new tcsh after setting several environment
variables and works quite well except for not being able to change
gid. And it is also a wish from my side to port this script to python.

Is your suggestion to put "newgrp design" into a new file and then
exec this file in my python script? What happens to the group id of
the shell that called the python script in this case? I would try to
avoid spawning a new tcsh as this make execution of tools on a remote
computer difficult as the handover of command line arguments does not
seem to be handed over to the newly spawned shell. I may be
understanding something wrongly here.

When you fork a process in unix/linux, the child inherits all of
parents settings; *but* any future changes that is made in child
process will not get reflected in the parent.

So there is no way you can fire a process and some how get its setting
back to the invoking shell (which could be anything btw, say bash/tcsh/
csh).

Thus what you really want is start a wrapper python script, say
setup_design.py
In setup_design.py, call necessary os routines like setegid(egid);
this will update the process settings of the process running
setup_design.py.

At the end of setup_design.py, do an exec call[1] to fire the user's
shell (note that even if you use popen or other ways of forking a new
process, things will work just fine) Whatever changes you made to the
process environment will get propagated to the new shell. User must
explicitly finish the new shell (say typing 'exit' on shell prompt)

Karthik

[1]. e.g.
import os
os.execvp(cmd_run[0], cmd_run) # cmd_run is probably ['/bin/bash']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top