linux-assembly.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* chdir on linux does not work
@ 2006-05-24  2:49 Noprianto
  2006-05-24  4:26 ` Timo Lindfors
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Noprianto @ 2006-05-24  2:49 UTC (permalink / raw)
  To: linux-assembly

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 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24  2:49 chdir on linux does not work Noprianto
@ 2006-05-24  4:26 ` Timo Lindfors
  2006-05-24  6:49   ` Frank Kotler
  2006-05-24  6:18 ` Brian Raiter
  2006-05-24 14:06 ` jeff
  2 siblings, 1 reply; 11+ messages in thread
From: Timo Lindfors @ 2006-05-24  4:26 UTC (permalink / raw)
  To: Noprianto; +Cc: linux-assembly

[-- Attachment #1: Type: text/plain, Size: 623 bytes --]

Hi,

On Tue, May 23, 2006 at 07:49:29PM -0700, Noprianto wrote:
> -> 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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24  2:49 chdir on linux does not work Noprianto
  2006-05-24  4:26 ` Timo Lindfors
@ 2006-05-24  6:18 ` Brian Raiter
  2006-05-24 14:06 ` jeff
  2 siblings, 0 replies; 11+ messages in thread
From: Brian Raiter @ 2006-05-24  6:18 UTC (permalink / raw)
  To: linux-assembly

> 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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24  4:26 ` Timo Lindfors
@ 2006-05-24  6:49   ` Frank Kotler
  2006-05-24  7:15     ` Brian Raiter
  2006-05-25  3:55     ` Adrian Ho
  0 siblings, 2 replies; 11+ messages in thread
From: Frank Kotler @ 2006-05-24  6:49 UTC (permalink / raw)
  To: linux-assembly

Timo Lindfors wrote:

...
> 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...

> 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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24  6:49   ` Frank Kotler
@ 2006-05-24  7:15     ` Brian Raiter
  2006-05-24  7:21       ` Frank Kotler
  2006-05-24 12:17       ` Timo Lindfors
  2006-05-25  3:55     ` Adrian Ho
  1 sibling, 2 replies; 11+ messages in thread
From: Brian Raiter @ 2006-05-24  7:15 UTC (permalink / raw)
  To: Frank Kotler; +Cc: linux-assembly

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

Good question, actually. I don't know.

>> 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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24  7:15     ` Brian Raiter
@ 2006-05-24  7:21       ` Frank Kotler
  2006-05-24 12:17       ` Timo Lindfors
  1 sibling, 0 replies; 11+ messages in thread
From: Frank Kotler @ 2006-05-24  7:21 UTC (permalink / raw)
  To: Brian Raiter; +Cc: linux-assembly

Brian Raiter wrote:

>>>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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24  7:15     ` Brian Raiter
  2006-05-24  7:21       ` Frank Kotler
@ 2006-05-24 12:17       ` Timo Lindfors
  1 sibling, 0 replies; 11+ messages in thread
From: Timo Lindfors @ 2006-05-24 12:17 UTC (permalink / raw)
  To: Brian Raiter; +Cc: linux-assembly

Hi,

On Wed, May 24, 2006 at 12:15:19AM -0700, Brian Raiter wrote:
> 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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24  2:49 chdir on linux does not work Noprianto
  2006-05-24  4:26 ` Timo Lindfors
  2006-05-24  6:18 ` Brian Raiter
@ 2006-05-24 14:06 ` jeff
  2006-05-25  3:20   ` Noprianto
  2 siblings, 1 reply; 11+ messages in thread
From: jeff @ 2006-05-24 14:06 UTC (permalink / raw)
  To: Noprianto; +Cc: linux-assembly

On Wednesday 24 May 2006 02:49 am, Noprianto wrote:
> 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?

>
> -> 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 majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24 14:06 ` jeff
@ 2006-05-25  3:20   ` Noprianto
  0 siblings, 0 replies; 11+ messages in thread
From: Noprianto @ 2006-05-25  3:20 UTC (permalink / raw)
  To: jeff; +Cc: linux-assembly

Hi Jeff,

> 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. 



> >
> > -> 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'
> >
> >
> >


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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-24  6:49   ` Frank Kotler
  2006-05-24  7:15     ` Brian Raiter
@ 2006-05-25  3:55     ` Adrian Ho
  2006-05-25  4:18       ` Frank Kotler
  1 sibling, 1 reply; 11+ messages in thread
From: Adrian Ho @ 2006-05-25  3:55 UTC (permalink / raw)
  To: linux-assembly

On Wed, May 24, 2006 at 02:49:24AM -0400, Frank Kotler wrote:
> 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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: chdir on linux does not work
  2006-05-25  3:55     ` Adrian Ho
@ 2006-05-25  4:18       ` Frank Kotler
  0 siblings, 0 replies; 11+ messages in thread
From: Frank Kotler @ 2006-05-25  4:18 UTC (permalink / raw)
  To: Adrian Ho; +Cc: linux-assembly

Adrian Ho wrote:
> On Wed, May 24, 2006 at 02:49:24AM -0400, Frank Kotler wrote:
> 
>>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.

> 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



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2006-05-25  4:18 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-24  2:49 chdir on linux does not work Noprianto
2006-05-24  4:26 ` Timo Lindfors
2006-05-24  6:49   ` Frank Kotler
2006-05-24  7:15     ` Brian Raiter
2006-05-24  7:21       ` Frank Kotler
2006-05-24 12:17       ` Timo Lindfors
2006-05-25  3:55     ` Adrian Ho
2006-05-25  4:18       ` Frank Kotler
2006-05-24  6:18 ` Brian Raiter
2006-05-24 14:06 ` jeff
2006-05-25  3:20   ` Noprianto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).