Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Bizarre su -c behavior
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Carlos
Guru
Guru


Joined: 07 Aug 2002
Posts: 458
Location: Providence, RI

PostPosted: Fri Dec 06, 2002 1:30 pm    Post subject: Bizarre su -c behavior Reply with quote

When I try
Code:
su -c "emerge foo"
Portage retrieves the package, but doesn't actually install it. Also, when I do something like
Code:
su -c "vim blah"
I can open and edit the file fine, but I get the following error when I exit:
Code:
E138: Can't write viminfo file $HOME/.viminfo!
None of these problems happen if I su to root then execute commands instead of using su -c, but I find it perplexing. Alas, I'm not sure where to RTFM and can't think of how to search for similar problems, so I'm posting here. I suspect that this happened after I ran etc-update at some point, but I'm not entirely sure.
_________________
Man must shape his tools lest they shape him.
Back to top
View user's profile Send private message
zhenlin
Veteran
Veteran


Joined: 09 Nov 2002
Posts: 1361

PostPosted: Fri Dec 06, 2002 2:15 pm    Post subject: Reply with quote

There are a few types of su, a binary one and one embedded in the shell. su - -c 'ls ~' should list the home dir of root, not the current user. su -c 'ls ~' should list the home dir of the current user.
Back to top
View user's profile Send private message
Carlos
Guru
Guru


Joined: 07 Aug 2002
Posts: 458
Location: Providence, RI

PostPosted: Sat Dec 07, 2002 12:08 am    Post subject: Reply with quote

zhenlin wrote:
There are a few types of su, a binary one and one embedded in the shell. su - -c 'ls ~' should list the home dir of root, not the current user. su -c 'ls ~' should list the home dir of the current user.
I've tried those, but they both give the listing of /root. Also, I tried using "su - -c" instead of "su -c" with emerge and vim, but I'm still getting the same problems.
_________________
Man must shape his tools lest they shape him.
Back to top
View user's profile Send private message
ebrostig
Bodhisattva
Bodhisattva


Joined: 20 Jul 2002
Posts: 3152
Location: Orlando, Fl

PostPosted: Sat Dec 07, 2002 12:28 am    Post subject: Reply with quote

But none of the command lines you listed includes a user name.

I take it that you wanted to su to root, then run the command as root and exit back to your normal user?

Take a look at sudo, this command is normally used for this type of jobs.

Erik
Back to top
View user's profile Send private message
Carlos
Guru
Guru


Joined: 07 Aug 2002
Posts: 458
Location: Providence, RI

PostPosted: Sat Dec 07, 2002 12:47 am    Post subject: Reply with quote

Well, su su's to root by default, and I'm sure I've seen what I'm doing recommended somewhere - otherwise I never would have started using it, considering that I can't find documentation for the -c option. In any event, it was working perfectly until recently; if things stay this way I might install sudo.
_________________
Man must shape his tools lest they shape him.
Back to top
View user's profile Send private message
ebrostig
Bodhisattva
Bodhisattva


Joined: 20 Jul 2002
Posts: 3152
Location: Orlando, Fl

PostPosted: Sat Dec 07, 2002 1:49 am    Post subject: Reply with quote

Carlos wrote:
Well, su su's to root by default, and I'm sure I've seen what I'm doing recommended somewhere - otherwise I never would have started using it, considering that I can't find documentation for the -c option. In any event, it was working perfectly until recently; if things stay this way I might install sudo.


You're right about su ofcourse, my bad :)

Here is a way around it:

1. Create a shell script with the following entry:
#! /bin/bash
export HOME=/root
vi $*

2. Make it executable:
chmod +x filename

3. Use su like this:
su -c "./filename parameterlist"

This will avoid the message you see, why I don't know :)

Erik
Back to top
View user's profile Send private message
Carlos
Guru
Guru


Joined: 07 Aug 2002
Posts: 458
Location: Providence, RI

PostPosted: Sat Dec 07, 2002 1:53 am    Post subject: Reply with quote

ebrostig wrote:

You're right about su ofcourse, my bad :)
No problem. :) Thanks for the solution for the vi part; I'm still quite curious about what the trouble with emerge is, and what the underlying cause of all the trouble is, but it's only a minor inconvenience.

The search continues...
_________________
Man must shape his tools lest they shape him.
Back to top
View user's profile Send private message
lx
Veteran
Veteran


Joined: 28 May 2002
Posts: 1012
Location: Netherlands

PostPosted: Sat Dec 07, 2002 1:53 am    Post subject: Reply with quote

ebrostig wrote:

1. Create a shell script with the following entry:
#! /bin/bash
export HOME=/root
vi $*

Erik


Well I wanted to suggest su - -c "set" a while ago, but when I do a test on my machine it just works, HOME=/root etc well I don't have vim but anyway, still I had some problems executing files using other user besides root in startup script, I fixed this using su - -c "/bin/bash --login -c ....", but I can't remember what the problem was then, but it also got to do with some environment variable not being set.

Cya lX.
_________________
"Remember there's a big difference between kneeling down and bending over.", Frank Zappa
Back to top
View user's profile Send private message
ebrostig
Bodhisattva
Bodhisattva


Joined: 20 Jul 2002
Posts: 3152
Location: Orlando, Fl

PostPosted: Sat Dec 07, 2002 2:12 am    Post subject: Reply with quote

Ok, I took this to another level :)

I ran strace as my su command:
su -c "strace -ff -o /root/su.out vi /root/aaa"

Exiting resulted in the now infamous "Cannot write to $HOME/.viminfo"

When I looked at the strace output, this is what i found:
Code:

stat64("/root/aaa", {st_mode=S_IFREG|0644, st_size=13, ...}) = 0
write(1, "\33[43;13H5L, 13C written", 23) = 23
_llseek(4, 0, [0], SEEK_SET)            = 0
write(4, "b0VIM 6.1\0\0\0\0\20\0\0\271R\361=\344k)\0S@\0\0root"..., 4096) = 4096
unlink("/root/aaa~")                    = 0
open("$HOME/.viminfo", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
stat64("$HOME/.viminfo", 0xbffff290)    = -1 ENOENT (No such file or directory)
umask(077)                              = 022
open("$HOME/.viminfo", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = -1 ENOENT (No such file or directory)
umask(022)                              = 077
write(1, "\r\r\n\33[1m\33[37m\33[41mE138: Can\'t wri"..., 63) = 63
write(1, "\33[m\r\r\n\33[32mHit ENTER or type com"..., 48) = 48


As you can see, it can not expand the $HOME variable, henc can not really find the correct directory. That si why the little script I gave you works since it sets the HOME variable prior to executing vi. Oh, btw, I do have a /root/viminfo

Erik
Back to top
View user's profile Send private message
kyptin
Tux's lil' helper
Tux's lil' helper


Joined: 29 Jun 2002
Posts: 95
Location: UNC (Chapel Hill, NC, USA)

PostPosted: Sat Dec 07, 2002 5:51 am    Post subject: environments Reply with quote

I'm pretty sure this behavior has something to do with which environment you're in when the command executes. For instance, the su manpage says
su manpage wrote:
The optional argument - may be used to provide an environment similar to what the user would expect had the user logged in directly.

So, my understanding is that - will invoke the shell specified in /etc/passwd for the user you're su-ing to, and that shell might look at .bashrc or whatever startup files it needed. I'm thinking a simple su -c without the - option simply changes the UID of the current user for that process, which could interfere with the environment somehow.

Looking into this further, at the login manpage (the su manpage directed me here)....

Seems like su with the - option basically does the same thing as login does. And according to its manpage, login is responsible for setting UID/GID, $HOME, $SHELL, $PATH, etc.

I haven't quite nailed anything down, but maybe I've managed to set somebody to sniffing in the right direction. I'd look more, but my eyelids are beginning to interfere with my vision, meaning I need to go to bed. :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum