9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] equivalent of fstat | grep $portnumber ?
@ 2007-06-19 15:27 Matthias Bauer
  2007-06-19 15:39 ` Skip Tavakkolian
  0 siblings, 1 reply; 18+ messages in thread
From: Matthias Bauer @ 2007-06-19 15:27 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Hi all,
what would be the Plan9 way of finding the process that
listens on a given port number? Grepping through /proc
does not help afaics because only /net/tcp/clone is
opened while listening (?).

Thanks,

Matthias


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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 15:27 [9fans] equivalent of fstat | grep $portnumber ? Matthias Bauer
@ 2007-06-19 15:39 ` Skip Tavakkolian
  2007-06-19 16:09   ` ron minnich
  2007-06-19 16:13   ` Kris Maglione
  0 siblings, 2 replies; 18+ messages in thread
From: Skip Tavakkolian @ 2007-06-19 15:39 UTC (permalink / raw)
  To: 9fans

> Hi all,
> what would be the Plan9 way of finding the process that
> listens on a given port number? Grepping through /proc
> does not help afaics because only /net/tcp/clone is
> opened while listening (?).
>
> Thanks,
>
> Matthias

something like this?

cpu% netstat | grep https
tcp  25   none       Listen       https      0          ::
cpu% grep '/net/tcp/25' /proc/*/fd
/proc/8429/fd: 13 rw I    0 (000000000002040c     0 00)     0        2 /net/tcp/25/listen
cpu% ps -eaf|grep 8429
none           8429    0:00   0:00     1404K Pread    httpd ?
fst           17202    0:00   0:00      156K Pread    grep 8429
cpu%



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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 15:39 ` Skip Tavakkolian
@ 2007-06-19 16:09   ` ron minnich
  2007-06-19 16:13   ` Kris Maglione
  1 sibling, 0 replies; 18+ messages in thread
From: ron minnich @ 2007-06-19 16:09 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 6/19/07, Skip Tavakkolian <9nut@9netics.com> wrote:

> cpu% netstat | grep https
> tcp  25   none       Listen       https      0          ::
> cpu% grep '/net/tcp/25' /proc/*/fd
> /proc/8429/fd: 13 rw I    0 (000000000002040c     0 00)     0        2 /net/tcp/25/listen
> cpu% ps -eaf|grep 8429
> none           8429    0:00   0:00     1404K Pread    httpd ?
> fst           17202    0:00   0:00      156K Pread    grep 8429
> cpu%

needs to be on wiki :-)

it's so cool, and so damn hard on other systems.

ron


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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 15:39 ` Skip Tavakkolian
  2007-06-19 16:09   ` ron minnich
@ 2007-06-19 16:13   ` Kris Maglione
  2007-06-19 16:17     ` andrey mirtchovski
  1 sibling, 1 reply; 18+ messages in thread
From: Kris Maglione @ 2007-06-19 16:13 UTC (permalink / raw)
  To: 9fans

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

On Tue, Jun 19, 2007 at 08:39:24AM -0700, Skip Tavakkolian wrote:
>cpu% netstat | grep https
>tcp  25   none       Listen       https      0          ::
>cpu% grep '/net/tcp/25' /proc/*/fd
>/proc/8429/fd: 13 rw I    0 (000000000002040c     0 00)     0        2 /net/tcp/25/listen
>cpu% ps -eaf|grep 8429
>none           8429    0:00   0:00     1404K Pread    httpd ?
>fst           17202    0:00   0:00      156K Pread    grep 8429
>cpu% 

I don't think that helps. As he said, you usually just see the open 
clone file for apps which are listening. The open on the listen file 
doesn't return until there's an incoming connection, so it's only ever 
visible for programs with active incoming connections.  There might be a 
simpler (and faster) way, but something like this will probably get you 
what you want:

#!/bin/rc

port = $1

cd /net/tcp
c = `{grep -l !$port'$' */local | sed 's,/.*,,'}
l = `{grep -l '^Listen' $c^/status | sed 's,/.*,,'}
q = `{du -q $l^/ctl | awk '{print $1}'}

grep -l -e^$q /proc/*/fd | awk -F/ '{print $3}'

-- 
Kris Maglione

People to whom you are attracted invariably thing you
remind them of someone else.

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

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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 16:13   ` Kris Maglione
@ 2007-06-19 16:17     ` andrey mirtchovski
  2007-06-19 16:49       ` Kris Maglione
  0 siblings, 1 reply; 18+ messages in thread
From: andrey mirtchovski @ 2007-06-19 16:17 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

i just found a small program i had forgotten about that mimics unix'
lsof. it's in /contrib/andrey/cmd/lsof.c

you can do lsof | grep 'tcp/25' to get the pid of the program.


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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 16:17     ` andrey mirtchovski
@ 2007-06-19 16:49       ` Kris Maglione
  2007-06-19 16:53         ` andrey mirtchovski
  0 siblings, 1 reply; 18+ messages in thread
From: Kris Maglione @ 2007-06-19 16:49 UTC (permalink / raw)
  To: 9fans

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

On Tue, Jun 19, 2007 at 10:17:17AM -0600, andrey mirtchovski wrote:
> i just found a small program i had forgotten about that mimics unix'
> lsof. it's in /contrib/andrey/cmd/lsof.c

I don't really see the point. It seems only marginally easier, if at 
all, than grepping /proc/*/fd. Most of the time, I think you'd want to 
grap the output, anyway, in which case, it's easier to just grep /proc. 
But, assuming it's useful, why write it in C when you may as well write 
it in rc?

#!/bin/rc

if(~ $#* 0)
	* = `{cd /proc; echo *}

grep '.?' /proc/^$*^/fd | sed 's,^/proc/([0-9]+)/fd:,\1	,'

-- 
Kris Maglione

You can't fix it if it ain't broke.

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

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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 16:49       ` Kris Maglione
@ 2007-06-19 16:53         ` andrey mirtchovski
  2007-06-19 16:57           ` Kris Maglione
  2007-06-19 18:13           ` jmk
  0 siblings, 2 replies; 18+ messages in thread
From: andrey mirtchovski @ 2007-06-19 16:53 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> But, assuming it's useful, why write it in C when you may as well write
> it in rc?

why is ps written in C when it may as well be written in rc?


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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 16:53         ` andrey mirtchovski
@ 2007-06-19 16:57           ` Kris Maglione
  2007-06-19 17:16             ` andrey mirtchovski
  2007-06-19 18:13           ` jmk
  1 sibling, 1 reply; 18+ messages in thread
From: Kris Maglione @ 2007-06-19 16:57 UTC (permalink / raw)
  To: 9fans

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

On Tue, Jun 19, 2007 at 10:53:16AM -0600, andrey mirtchovski wrote:
> why is ps written in C when it may as well be written in rc?

Because ps is a much more complex program and used much more 
extensively. Aside from having quite a few options to process, the work 
that it does would be much more expensive to do in rc, although a fairly 
speedy implementation could probably be written without much trouble.

But your lsof is equivalent to about 3 lines of rc script, done almost 
entirely by programs written in C, and is much clearer. I doubt that 
there's any noteworthy difference in speed, or that it would matter much 
if there were, so I just can't imagine why you'd want to write it in C.

-- 
Kris Maglione

The worse your line is tangled, the better is the
fishing around you.

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

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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 16:57           ` Kris Maglione
@ 2007-06-19 17:16             ` andrey mirtchovski
  2007-06-19 17:38               ` Kris Maglione
  0 siblings, 1 reply; 18+ messages in thread
From: andrey mirtchovski @ 2007-06-19 17:16 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> But your lsof is equivalent to about 3 lines of rc script, done almost
> entirely by programs written in C, and is much clearer. I doubt that
> there's any noteworthy difference in speed, or that it would matter much
> if there were, so I just can't imagine why you'd want to write it in C.

either i missed a smiley or you missed my point. lsof is not intended
for the Plan 9 distribution.

if you want to argue, however, here are a few points:

your script as you've supplied it is incorrect. adding the required
functionality will make it more complex:

- results are sorted alphabetically, not numerically by process id (in
other incarnations they'll need to be sorted by node, then by process
id)
- /proc/trace is not a directory
- race: at least one process has exited by the time the script tries
to cat its fd file, resulting in an error

as far as speed is concerned, the rc script is two to three times
slower on a single user machine, four to six times slower on a busy
machine with multiple users. if one is doing this on several machines
at the same time it becomes too much (think 'ps' on a 1000 node
cluster).


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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 17:16             ` andrey mirtchovski
@ 2007-06-19 17:38               ` Kris Maglione
  2007-06-19 18:09                 ` Roman Shaposhnick
  0 siblings, 1 reply; 18+ messages in thread
From: Kris Maglione @ 2007-06-19 17:38 UTC (permalink / raw)
  To: 9fans

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

On Tue, Jun 19, 2007 at 11:16:44AM -0600, andrey mirtchovski wrote:
> either i missed a smiley or you missed my point. lsof is not intended
> for the Plan 9 distribution.

Well, the original point was that I don't really see the point of the 
program at all. Even if it were part of the main distrobution, it's 
still such a trivial program that it seems fitting that it be provided 
as a script, as are many other such programs.

As for the speed, I just can't imagine it being an issue in practice, 
but I also can't really imagine using the program at all, rather than 
just grepping /proc. All in all, it doesn't matter much to me, it just 
seems much simpler to just write it in rc.

The other problems you mentioned seem trivial. Redirecting greps stderr 
to /dev/null solves the second 2. There are a number of simple ways to 
solve the first.

#!/bin/rc

if(~ $#* 0)
	f = /proc/*/fd
if not
	f = /proc/^$*^/fd
f = `{ls $f | sort -t/ -n +2}

grep '.?' $f >[2]/dev/null | sed 's,^/proc/([0-9]+)/fd:,\1	,'

-- 
Kris Maglione

Serving coffee on aircraft causes turbulence.

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

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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 18:09                 ` Roman Shaposhnick
@ 2007-06-19 18:08                   ` Kris Maglione
  2007-06-19 18:53                     ` Kris Maglione
  0 siblings, 1 reply; 18+ messages in thread
From: Kris Maglione @ 2007-06-19 18:08 UTC (permalink / raw)
  To: 9fans

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

On Tue, Jun 19, 2007 at 11:09:30AM -0700, Roman Shaposhnick wrote:
>On Tue, 2007-06-19 at 13:38 -0400, Kris Maglione wrote:
>> The other problems you mentioned seem trivial. Redirecting greps stderr 
>> to /dev/null solves the second 2. There are a number of simple ways to 
>> solve the first.
>
>  What about the race? That's always been one of my personal pet peeves 
>as far as any shell programming goes.

Redirecting to /dev/null. The only real issue with the race is that 
there's an error message printed. Hiding the error message from grep is 
basically the same as not printing it in lsof.c.

-- 
Kris Maglione

Things equal to nothing else are equal to each other.

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

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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 17:38               ` Kris Maglione
@ 2007-06-19 18:09                 ` Roman Shaposhnick
  2007-06-19 18:08                   ` Kris Maglione
  0 siblings, 1 reply; 18+ messages in thread
From: Roman Shaposhnick @ 2007-06-19 18:09 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Tue, 2007-06-19 at 13:38 -0400, Kris Maglione wrote:
> The other problems you mentioned seem trivial. Redirecting greps stderr
> to /dev/null solves the second 2. There are a number of simple ways to
> solve the first.

  What about the race? That's always been one of my personal pet peeves
as far as any shell programming goes.

Thanks,
Roman.



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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 16:53         ` andrey mirtchovski
  2007-06-19 16:57           ` Kris Maglione
@ 2007-06-19 18:13           ` jmk
  2007-06-19 18:21             ` jmk
  2007-06-20  0:46             ` Russ Cox
  1 sibling, 2 replies; 18+ messages in thread
From: jmk @ 2007-06-19 18:13 UTC (permalink / raw)
  To: 9fans

actually, seriously, i'd like a verison of ps in rc.
for the minimal boot fs on the compute nodes i have things like
fn cat {
	sed '' $*
}
where's fn ps?

--jim

p.s. oh yeah, the only programmes you can use are rc, sed and echo.


On Tue Jun 19 12:58:25 EDT 2007, mirtchovski@gmail.com wrote:
> > But, assuming it's useful, why write it in C when you may as well write
> > it in rc?
>
> why is ps written in C when it may as well be written in rc?


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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 18:13           ` jmk
@ 2007-06-19 18:21             ` jmk
  2007-06-19 18:28               ` Roman Shaposhnick
  2007-06-20  0:46             ` Russ Cox
  1 sibling, 1 reply; 18+ messages in thread
From: jmk @ 2007-06-19 18:21 UTC (permalink / raw)
  To: 9fans

sorry, please ignore that last post.
it was intended for an individual, not 9fans.
posting to 9fans is an abberation i've
tried to train myself not to do.


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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 18:21             ` jmk
@ 2007-06-19 18:28               ` Roman Shaposhnick
  0 siblings, 0 replies; 18+ messages in thread
From: Roman Shaposhnick @ 2007-06-19 18:28 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Tue, 2007-06-19 at 14:21 -0400, jmk@plan9.bell-labs.com wrote:
> sorry, please ignore that last post.
> it was intended for an individual, not 9fans.
> posting to 9fans is an abberation i've
> tried to train myself not to do.

  I've enjoyed it. Could you, please, may be
consider abberating more often?

Thanks,
Roman.



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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 18:08                   ` Kris Maglione
@ 2007-06-19 18:53                     ` Kris Maglione
  0 siblings, 0 replies; 18+ messages in thread
From: Kris Maglione @ 2007-06-19 18:53 UTC (permalink / raw)
  To: 9fans

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

On Tue, Jun 19, 2007 at 02:08:14PM -0400, Kris Maglione wrote:
> Redirecting to /dev/null. The only real issue with the race is that there's 
> an error message printed. Hiding the error message from grep is basically 
> the same as not printing it in lsof.c.

Admittedly, I introduced another such race when I lazily decided to use 
ls instead of tr to split the files into lines.

f = `{echo $f | tr ' ' '\012' | sort ...}

-- 
Kris Maglione

No experiment is ever a complete failure.
It can always be used as a bad example.

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

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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-19 18:13           ` jmk
  2007-06-19 18:21             ` jmk
@ 2007-06-20  0:46             ` Russ Cox
  2007-06-20  5:48               ` Bruce Ellis
  1 sibling, 1 reply; 18+ messages in thread
From: Russ Cox @ 2007-06-20  0:46 UTC (permalink / raw)
  To: 9fans

> actually, seriously, i'd like a verison of ps in rc.
> for the minimal boot fs on the compute nodes i have things like
> fn cat {
> 	sed '' $*
> }
> where's fn ps?

apologies for continuing this awful discussion.

i used to do this on the single-floppy boot image
but i tired of making up clever things like that.

the last time the single-floppy boot image filled
i wrote a few programs to combine many
binaries into a single one.  this saves space
because they all end up using the same instance
of libc, libbio, etc instead of having multiple ones.
this sort of thing is very common on unix for
embedded systems.  the binary looks at argv0
to figure out which main function to run.

/sys/lib/dist/cmd/multi/mkfile takes care of
combining a rather large number of binaries
that the boot floppy needed.  the final binary
is 1300k compared with 4200k for the collection
of the others.

you can't mix in programs that use libthread.

russ



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

* Re: [9fans] equivalent of fstat | grep $portnumber ?
  2007-06-20  0:46             ` Russ Cox
@ 2007-06-20  5:48               ` Bruce Ellis
  0 siblings, 0 replies; 18+ messages in thread
From: Bruce Ellis @ 2007-06-20  5:48 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

i have dark memories of updating a sunos machine many years ago.
the disk got kinda full and we were in "WTF do i do mode".
my blender broke down so i had to send brian out to get a new one
'cause we needed more cocktails.

we could only use static linked progs because the libraries weren't their yet.

many shell scripts were involved.  "rm" was dynamically linked, very
handy when you want to clean things up.

ed worked and shell script "rm" which did an "echo > $i" and a few
others got us thru the rum & fruit based twilight zone.

(yes - it's not the same but it frees stuff.)

brucee

On 6/20/07, Russ Cox <rsc@swtch.com> wrote:
> > actually, seriously, i'd like a verison of ps in rc.
> > for the minimal boot fs on the compute nodes i have things like
> > fn cat {
> >       sed '' $*
> > }
> > where's fn ps?
>
> apologies for continuing this awful discussion.
>
> i used to do this on the single-floppy boot image
> but i tired of making up clever things like that.
>
> the last time the single-floppy boot image filled
> i wrote a few programs to combine many
> binaries into a single one.  this saves space
> because they all end up using the same instance
> of libc, libbio, etc instead of having multiple ones.
> this sort of thing is very common on unix for
> embedded systems.  the binary looks at argv0
> to figure out which main function to run.
>
> /sys/lib/dist/cmd/multi/mkfile takes care of
> combining a rather large number of binaries
> that the boot floppy needed.  the final binary
> is 1300k compared with 4200k for the collection
> of the others.
>
> you can't mix in programs that use libthread.
>
> russ
>
>


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

end of thread, other threads:[~2007-06-20  5:48 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-19 15:27 [9fans] equivalent of fstat | grep $portnumber ? Matthias Bauer
2007-06-19 15:39 ` Skip Tavakkolian
2007-06-19 16:09   ` ron minnich
2007-06-19 16:13   ` Kris Maglione
2007-06-19 16:17     ` andrey mirtchovski
2007-06-19 16:49       ` Kris Maglione
2007-06-19 16:53         ` andrey mirtchovski
2007-06-19 16:57           ` Kris Maglione
2007-06-19 17:16             ` andrey mirtchovski
2007-06-19 17:38               ` Kris Maglione
2007-06-19 18:09                 ` Roman Shaposhnick
2007-06-19 18:08                   ` Kris Maglione
2007-06-19 18:53                     ` Kris Maglione
2007-06-19 18:13           ` jmk
2007-06-19 18:21             ` jmk
2007-06-19 18:28               ` Roman Shaposhnick
2007-06-20  0:46             ` Russ Cox
2007-06-20  5:48               ` Bruce Ellis

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