pstat_getproc returns an error for pid 1 in hp-ux itanium

672 views Asked by At

I'm trying to run a test program that will display the process of a requested pid. If the pid is not inserted, it's suppose to get the process for pid 1 which is init. Somehow when I run it on hp-ux itanium, it cannot display the process. This only happens on hp-ux itanium for pid 1. For other pid and platform, it works just fine. Here are the code that I think is related:

This is the test file

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "procname.h"

int main(int argc, char **argv)
{
    pid_t pid;
    char proc[PROCNAME_SZ];

    if (argc==2) {
            pid = atoi(argv[1]);
            if (pid<1) {
                    printf("Invalid pid\n");
                    exit(1);
            }
    } else {
            pid = 1;
    }
    if (get_procname(pid, proc, sizeof(proc))!=0) {
            printf("Error retrieving process name: %s\n", strerror(errno));
            printf("proc = %s\n",proc);
            printf("pid is = %d\n",pid);
            exit(1);
    }

    if (argc==2) {
            printf("Process name = %s\n", proc);
    } else {
            printf("Checking if procname(1) have init...");
            if (strstr(proc, "init")) {
                    printf("OK (%s)\n", proc);
                    exit(0);
            } else {
                    printf("Failed (%s)\n", proc);
                    exit(1);
            }
    }

    return 0;
}

This is the .c file

#include <errno.h>

#include "procname.h"
#include "log.h"

#ifdef HPUX
#  include <sys/param.h>
#  include <sys/pstat.h>
#  include <sys/unistd.h>
#endif

int get_procname(pid_t pid, char *buf, size_t n)
{
#ifdef HPUX
    struct pst_status pst;
    if (pstat_getproc(&pst, sizeof(pst), (size_t)0, pid) != -1) {
            memcpy(buf, pst.pst_cmd, n - 1);
            buf[n-1] = 0;
    } else {
            return -1;
    }
#endif
    return 0;
}

This is the output that I got on hp-ux itanium:

./test_procname 1
Error retrieving process name: Value too large to be stored in data type
proc = 
pid is = 1

This is the result that I got on hp-uxmp

./test_procname 1
Process name = init

The pid are just the same, but somehow it cannot recognized the init on hp-ux itanium. The size for printing the string are more than enough to just print init.

0

There are 0 answers