We all know that when an Oracle instance starts, the background processes are all (or perhaps mostly?) copies of the same executable – $ORACLE_HOME/bin/oracle.
We can see this using the “args” and “comm” format options of the Unix “ps” command. Below we can see the name of the SMON process (“args”) and the name of the source executable (“comm”):
$ ps -p 6971 -o args,comm COMMAND COMMAND ora_smon_orcl1 oracle
This is common knowledge and when in the past I’ve wondered how this works I’ve always just accepted this Oracle magic and moved on to more important thoughts such as “what’s for lunch?”. Well today my belly is full and I have time to let such thoughts develop!
It turns out this is a quite simple trick. If you have a basic knowledge of the C programming language you’ll be familiar with the argc and argv parameters. e.g.
int main (int argc, char *argv[])
argc – the number of command line arguments
argv – an array of pointers to the command line arguments with argv[0] being the process name.
Surprisingly the values of this array are modifiable. So we can manipulate the name of a running process with a simple sprintf() command (other options are available). e.g.
sprintf(argv[0], "renamed");
My description of this is high level and I’m sure there are risks in doing this, such as overflowing argv[0] into argv[1], but by way of a simple example here is a demo C program:
#include <stdio.h> void main (int argc, char *argv[]) { int pid; pid = (int) getpid (); printf("My name is '%s'\n",argv[0]); printf("Sleeping for 10 seconds (quick - 'ps -p %d -o args,comm')...\n",pid); sleep(10); sprintf(argv[0], "renamed"); printf("My name is now '%s'\n",argv[0]); printf("Sleeping for 10 seconds (quick - 'ps -p %d -o args,comm')...\n",pid); sleep(10); }
This program sleeps so I can capture process details from another session using “ps”, renames the running process and then sleeps again so I can capture more details. So let’s compile the C program and see what happens when I run it from one session and capture ps details from another session:
Session 1
$ cc newname.c -o newname $ ./newname My name is './newname' Sleeping for 10 seconds (quick - 'ps -p 7930 -o args,comm')... My name is now 'renamed' Sleeping for 10 seconds (quick - 'ps -p 7930 -o args,comm')...
Session 2
$ ps -p 7930 -o args,comm COMMAND COMMAND ./newname newname $ ps -p 7930 -o args,comm COMMAND COMMAND renamed e newname
Pretty cool. Notice how my renamed process is now called “renamed e”. The trailing “e” is left over from the original name of “./newname” thus proving it’s not quite as simple as I suggest and that my C skills are basic. None-the-less I think this is pretty cool.