Skip to contentSkip to conversation

chdir on linux does not work

DISCUSSION Noprianto 6 participants 11 messages May 24, 2006 – May 25, 2006

lore
Share & tools
lore

Noprianto

author
#0 lore raw

Dear All,

Anyone can tell me why this chdir code doesnot work on linux ?

-> from what i read, chdir is system call number 12

section .text
global _start

_start:
mov eax, 12
mov ebx, dir
int 80h

mov eax, 1
mov ebx, 0
int 80h

section .data
dir: db '/bin'

Thanks,
Pri

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

#1 lore raw

Hi,

Quoted 1 line · Noprianto

-> from what i read, chdir is system call number 12

Yep, but better use something like

%define __NR_chdir 12

to make code more readable. Also, your example works just fine here:

$ nasm a.asm -f elf
$ gcc a.o -o a -nostdlib
$ strace ./a
execve("./a", ["./a"], [/* 30 vars */]) = 0
chdir("/bin")                           = 0
_exit(0)                                = ?

Remember that chdir affects only the process that calls chdir. You can't e.g. write a replacement for shell's builtin "cd" command that way.

best regards,
Timo Lindfors

#2 lore raw

Timo Lindfors wrote:

...

Quoted 1 line

Remember that chdir affects only the process that calls chdir.

I thought Noprianto's problem was the lack of a terminating zero after "/bin"... didn't help, but I still think it's a bug waiting to byte. Adding a call to sys_getcwd (__NR_ 183) (why is this man 3, not man 2 ???) proves that, sure enough, it *does* work... but we change back on exit... as you say...

Quoted 3 lines

You
can't e.g. write a replacement for shell's builtin "cd" command that way.

Okay, how *would* we do that? I know! I'll check "asmutils"! Hmmm... no chdir in asmutils... This is apparently harder than it looks!

Good Question!

Best,
Frank

#3 lore raw
Quoted 2 lines

Adding a call to sys_getcwd (__NR_ 183) (why is this man 3, not man 2 ???)

Good question, actually. I don't know.

Quoted 4 lines
Quoted 2 lines

You can't e.g. write a replacement for shell's builtin "cd" command that way.

Okay, how *would* we do that?

It is simply not possible. Like setenv, cd absolutely must be a shell builtin.

b

#4 lore raw
Quoted 8 lines · Brian Raiter
Quoted 4 lines
Quoted 2 lines

You can't e.g. write a replacement for shell's builtin "cd" command that way.

Okay, how *would* we do that?

It is simply not possible. Like setenv, cd absolutely must be a shell builtin.

Okay... Thanks for the quick response, Brian - probably saved me a lot of frustration!

Best,
Frank

#5 lore raw
In reply to Brian Raiter

Hi,

Quoted 2 lines · Brian Raiter

It is simply not possible. Like setenv, cd absolutely must be a shell builtin.

Indeed, unless you do some dirty tricks by using ptrace(2) to attach to the parent and force it to do the chdir syscall :)

best regards,
Timo Lindfors

#6 lore raw
In reply to Frank Kotler
Quoted 2 lines · Frank Kotler

Adding a call to sys_getcwd (__NR_ 183) (why is this man 3, not man 2 ???)

Are you talking about sys_getcwd(3) (which I've never seen or known to exist) or getcwd(3)? If the latter, there's a Linux-specific extension that uses malloc() -- perhaps that's the reason why:

As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. It is possi- ble (and, indeed, advisable) to free() the buffers if they have been obtained this way.

- Adrian

#7 lore raw
Quoted 7 lines · Adrian Ho
Quoted 1 line · Frank Kotler

Adding a call to sys_getcwd (__NR_ 183) (why is this man 3, not man 2 ???)

Are you talking about sys_getcwd(3) (which I've never seen or known to exist) or getcwd(3)?

getcwd(3). I refer to it as "sys_???" when I mean the int 80h interface, sorry.

Quoted 2 lines

If the latter, there's a Linux-specific extension that uses malloc() -- perhaps that's the reason why:

I'll bet you're right. If that isn't the reason, it's as good a reason as any. Good point!

Best,
Frank

#8 lore raw
In reply to Noprianto
Quoted 5 lines

Anyone can tell me why this chdir code doesnot work on linux ?

section .data
dir: db '/bin'

You forgot to NUL-terminate your string. A dangerous mistake, as it can become a Heisenbug. You were lucky it failed to work for you. Use this instead:

dir: db '/bin', 0

b

#9 lore raw
In reply to Noprianto
Quoted 4 lines · Noprianto

Dear All,

Anyone can tell me why this chdir code does not work on linux ?

If you run this program in a terminal it will do the chdir, then restore the original directory when the program exits. Possibly this is what you are seeing?

Quoted 35 lines

-> from what i read, chdir is system call number 12

section .text
global _start

_start:
mov eax, 12
mov ebx, dir
int 80h

mov eax, 1
mov ebx, 0
int 80h

section .data
dir: db '/bin'

Thanks,
Pri

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-assembly" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#10 lore raw

Hi Jeff,

Quoted 5 lines

If you run this program in a terminal it will do the chdir,
then restore the original directory when the program exits. Possibly this is what you are seeing?

Yes, thats what i am seeing.

Quoted 25 lines
Quoted 2 lines

-> from what i read, chdir is system call number

12

Quoted 22 lines

section .text
global _start

_start:
mov eax, 12
mov ebx, dir
int 80h

mov eax, 1
mov ebx, 0
int 80h

section .data
dir: db '/bin'

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com