AIX equivalent to /proc/self/cmdline to get process name from C++ program


 
Thread Tools Search this Thread
Operating Systems AIX AIX equivalent to /proc/self/cmdline to get process name from C++ program
# 1  
Old 10-26-2009
AIX equivalent to /proc/self/cmdline to get process name from C++ program

Hi,
I'm porting some old C++ code (that I didn't write) from Linux to AIX and have run into a problem in getting the process name from within the code when it is run on AIX. Basically the code is getting the process name so it can then return it to the rest of the code as argv[0]. This code is trying to name logfiles using the process name and is called by many other classes.
The OS is: powerpc-ibm-aix5.3.0.0
The gcc is: gcc version 4.2.0

The code is below:

Code:
static const std::string getArg0(void)
    {
#if defined(__hpux__) || defined(__hpux)
    extern char** __argv_value; // see man crt0 on HP-UX.
    return __argv_value[0];
#elif defined(__linux__)
    static std::string argv;
    static bool first = true;
    if (first)
        {
        first = false;
        std::ifstream is("/proc/self/cmdline");
        if (!is)
            argv = "unknown";
        else
            {
            std::ostringstream os;
            char buf;
            while (true)
                {
                is.get(buf);
                if (!is)
                    break;
                if (!isspace(buf))
                    os << buf;
                else
                    break;
                }
            os << std::ends;
            argv = os.str();
            is.close();
            }
        }
        return argv;
#elif defined(_AIX)
    extern char** argv;
    return argv[0];
#else
    return "unknown";
#endif
    }

// -- public -----------------------------------------------------------------
// getProcName()
//
// Determine the process name and return it.
//
// -- implementation ---------------------------------------------------------
// Since this is called before main(), we can't use argv.  But the C run time
// startup code sets argv from the global __argv_value, so just use that.
// ---------------------------------------------------------------------------
TextString LogStream::getProcName(bool useFullPath)
    {
      // LAB - GCC upgrade syntax change
    static TextString fullName(getArg0().c_str());
    static TextString leafOnly(basename((char *)fullName.stringPtr()));
    return useFullPath ? fullName : leafOnly;
    }

I intially added "|| _AIX" to the linux block above but since AIX doesn't have /proc/self it failed and all my logfiles are named unknown*. I have tried several different separate blocks for AIX, but after hours of searching for how to get the process name I've hit a wall. I am able to compile, but always get an undefined error on the link. How do I duplicate the above blocks used for linux and (ancient hp block) for AIX?

Any help is greatly appreciated! Thanks..
Tom
# 2  
Old 10-27-2009
There might be some clues from the "truss" output, although I admit I haven't found it myself.
Code:
truss -o /tmp/hosts-trace bash -c 'echo $0'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

How to compile and run the ProC (*.pc) program?

Hi Team, I am very new to this forum and hope someone will help me in resolving the issue. I am new to Pro C also. I made some changes to the existing Pro C program and want to run the program with the changes. But I am unable to neither compile nor run the program. Please do the... (2 Replies)
Discussion started by: prakashs1218
2 Replies

2. Solaris

Logging the memory consumed by a process with c/C++ without using proc

I need to log the size of physical/virtual memory consumed by any given given process using c/c++ code running on solaris and aix without using the proc filesystem. Please advise. (1 Reply)
Discussion started by: Manisha Paul
1 Replies

3. UNIX for Dummies Questions & Answers

Getting a process/program version from /proc folder

Hello I am writing a script that will first execute ps to get the list of processes running, and the go into the /proc folder for each PID listed and gather relevant information. I looked through the contents of a particular process in the /proc folder and I can't find where I can locate... (2 Replies)
Discussion started by: flagman5
2 Replies

4. Shell Programming and Scripting

does the pid of background process show in /proc?

Hi all, I'm reading <advanced bash scripting> and there is a example to kill a background process in a limited time,as shown below: #! /bin/bash #set -n TIMEOUT=$1 count=0 hanging_jobs & { while ((count < TIMEOUT));do eval ' && ((count = TIMEOUT))' ((count++)) sleep 1... (6 Replies)
Discussion started by: homeboy
6 Replies

5. UNIX for Dummies Questions & Answers

proc program compilation in unix

hi, i need to compile a proc program, say prog.pc can we compile this program in the unix environment? does this need a make file? can anyone help me on this since i am new to this area. Thanks in advance. (1 Reply)
Discussion started by: csprog
1 Replies

6. Programming

Turn java cmdline program into GUI

I have a java command line program from a vendor without source code, but it's too much work to run on a PC window, therefore I'm think to create a GUI wrapper or web interface calling that java cmdline program. But I'm a newbie in java world - How convert a java command line program into a web... (0 Replies)
Discussion started by: tqlam
0 Replies

7. Shell Programming and Scripting

Error in script to automate the daily monitoring process of UNIX server and it's proc

hi friends, I am trying to automate the daily monitoring process of UNIX server and it's processes. the script are below i executed the above script using ksh -x monitortest1.sh in root login . It shows error at some lines . 1. i logged in using root ,but it... (8 Replies)
Discussion started by: rdhaprakasam
8 Replies

8. AIX

ProC and other C file compilation problem on AIX

I am linking my compiled proC file with other C files and getting following error. ld: 0711-711 ERROR: Input file /opt/orabase/oracle/product/10.2.0/db_1/lib/libirc.a is empty. The file is being ignored. I used following command to compile my proC code. proc iname=dbConnect.pc code=ANSI_C... (0 Replies)
Discussion started by: amit.singhal
0 Replies

9. Linux

How to call a proc file from *.c program?

Hi, I am new to Linux programming. As part of learning, I need to create a *.c program where we call certain /proc files (i.e. such as meminfo, version, uptime, etc...) from our program. Can anyone point me to a simple program on how one would do this (i.e. can you directly call uptime() or... (4 Replies)
Discussion started by: pat_and_cami
4 Replies
Login or Register to Ask a Question