Changing the main program name at runtime

1,643 views
Skip to first unread message

simran

unread,
Oct 4, 2015, 11:23:39 PM10/4/15
to golang-nuts
Hi All, 

I was going through "Go by Example" and on https://gobyexample.com/command-line-arguments - it talks about os.Args. 

I've found that i can change os.Args[0] in my program (which makes sense, it's just a slice of strings i guess), but that does not change the program name being executed. 

That is, doing a "ps auxww | grep <progname>" shows me the original program name i executed, even though i changed os.Args[0]. 

Is it possible to change the name of the program being executed at runtime? 

kind regards, 

simran.

ps: Coming from perl where changing $0 actually changes the running program name (*for the most part - see http://perldoc.perl.org/perlvar.html and search for $0). 

Dave Cheney

unread,
Oct 4, 2015, 11:50:34 PM10/4/15
to golang-nuts
os,Args is a copy of the program environment which is made very early on in the life of the process. I don't know how perl works, but I suspect they have additional magic to make those changes visible to other programs.

Micky

unread,
Oct 4, 2015, 11:57:55 PM10/4/15
to simran, golang-nuts
Looking at the perlvar docs, I can see it's modified by prctl() syscall. So, you can directly use its PR_SET_NAME argument.
All you need to know is how to use to the "unsafe" package.

If that's too complicated, a workaround is spawning a new process. See os.StartProcess.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Staven

unread,
Oct 5, 2015, 3:00:45 AM10/5/15
to golang-nuts
On Mon, Oct 05, 2015 at 02:22:42PM +1100, simran wrote:
> Is it possible to change the name of the program being executed at runtime?

Out of curiosity, why would you want to do something like that?

simran

unread,
Oct 5, 2015, 3:41:46 AM10/5/15
to golang-nuts
Hi Staven, 

I'm sure there's heaps of reasons, but i was coming from the perspective of looking at rootkits, etc... and they hide their process names - make it look like it's an innocent window manager process or something like that is running. I know it doesn't ultimate disguise the process but it can throw people off on a visual inspection via ps... it's just another layer. 

The reason perl mentioned in some perl documentation is that you can send other processes outside it an update on which stage your process is at (just via the system process table).

cheers, 

simran.

ps: Dave, Micky, thanks for your responses... good info in both of them and i'm digging further as a result :) 
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nathan Fisher

unread,
Oct 5, 2015, 6:41:43 AM10/5/15
to golang-nuts
I'm not saying it's a good use case but if you use dynamic port allocation you could append the allocated port to the process name to make listening port, process id, and start time all visible from a ps listing on the command line.

Manlio Perillo

unread,
Oct 5, 2015, 7:59:03 AM10/5/15
to golang-nuts, sta...@staven.pl
Both Nginx and PostgreSQL use this feature, to make it easy to search for a specific process.
As an example:
root       456  0.0  0.0  25812   760 ?        Ss   Sep29   0:00 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr;
http       457  0.0  0.0  26264  1624 ?        S    Sep29   0:00 nginx: worker process


Regards  Manlio

Dave Cheney

unread,
Oct 5, 2015, 7:13:40 PM10/5/15
to golang-nuts
Add prctl support to x/SYS would be a useful project to get your feet wet in Go development.

Erik Dubbelboer

unread,
Oct 6, 2015, 1:32:32 PM10/6/15
to golang-nuts

simran

unread,
Oct 7, 2015, 2:57:46 AM10/7/15
to Erik Dubbelboer, golang-nuts
Thanks Erik, 

Works a charm :) 

cheers, 

simran.


On Wed, Oct 7, 2015 at 2:03 AM, Erik Dubbelboer <er...@dubbelboer.com> wrote:
Try https://github.com/ErikDubbelboer/gspt


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave Cheney

unread,
Oct 7, 2015, 4:09:12 AM10/7/15
to golang-nuts
See https://go-review.googlesource.com/#/c/15520/

On Wednesday, 7 October 2015 04:32:32 UTC+11, Erik Dubbelboer wrote:
Try https://github.com/ErikDubbelboer/gspt

Reply all
Reply to author
Forward
0 new messages