[prev in list] [next in list] [prev in thread] [next in thread] 

List:       hpux-devtools
Subject:    Re: HPUX-DEVTOOLS: Porting NT Application to HP-UX
From:       Mike Stroyan <stroyan () hpstryn ! fc ! hp ! com>
Date:       2001-02-28 23:32:19
[Download RAW message or body]

| 1.  Is there an HP-UX equivalent to the Win32 call GetTickCount ()?  I
| need a monotonically increasing number in increments of milliseconds so
| that I can measure relative time.  The time () function almost provides
| this functionality.  However, I don't believe that it is impervious to
| changes in system time.  In other words, if a superuser changes the
| system time, relative time calculations based on time () may be
| incorrect.

  I don't know if any of the "wall time" clocks are immune to abrupt setting
of the time.  A clock won't very useful as a clock if it doesn't match the
system's current idea of the time.  The usual way to avoid troubles with
setting the time is to use the adjtime() interface to gradually correct
the time without actually jumping backward.

  You might like the behavior of clock_gettime() with CLOCK_VIRTUAL or
CLOCK_PROFILE.  They are not wall clock time, but they are more likely
to be montonic in all circumstances.

| 2.  Is there a way for a process to determine the full pathname of its
| image (e.g., /home/terry/test/a.out)?  In Win32, there is
| GetModuleFileName ().  Is there an equivalent for HP-UX?  I could use
| argv [0], except I need to know the full pathname during static
| construction time (before the first line of main () executes).  If there
| isn't a GetModuleFileName () equivalent, is there a way to get the
| command-line information at static construction time through some API
| call or some global variable?  Win32 has GetCommandLine ().

  It is quite difficult to map a process to a file in all cases.
You can usually get what you want with the PATH and the command line.
The first 64 characters of the command line are available using
pstat_getproc() and the pst_cmd field of the struct it returns.
I have attached an example that is using argc and argv instead of
pst_cmd.

| 3.  Is there a way for a process to determine if it is detached (i.e.,
| running with no stdout)?  I'd like to optimize my application to avoid
| executing its diagnostic console-print logic if it is running as a
| detached process (in the background or as a daemon).

  There are a lot of different attributes related to being a full daemon.
You could compare the result of getpgrp() to the result of tcgetpgrp(1).
That would determine foreground vs. background.  You could see if you can
open /dev/tty.  You may just want to see if stdout is a tty-

#include <unistd.h>
if (isatty(1)) { /* this is writing to a tty */ } else { /* something else */ }

| 4.  The Windows NT version of our server application uses a separate
| Windows MFC client that provides the GUI to program/configure the
| server.  The Windows MFC application communicates with the server using
| (Microsoft) RPC.  Is Microsoft RPC compatible with HP-UX RPC?  Can I
| port the server portion of my server-side RPC logic over to HP-UX and
| have it work with my MFC client?

  I haven't tried it myself.  I think there is a good chance that they
could interoperate that way.

| 5.  Windows NT has an extensible SNMP agent.  I wrote an NT SNMP
| extension DLL to provide SNMP support under NT.  How do I go about
| adding SNMP support under HP-UX?  Is there an extensible agent under
| HP-UX as well?

  That's way out of my turf.

Mike Stroyan, mike_stroyan@fc.hp.com
["homing.c" (text/plain)]

#include <sys/param.h>
#include <sys/pstat.h>
#include <mntent.h>
#include <sys/vfs.h>
#include <ftw.h>
#include <limits.h>
#include <malloc.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *fs_name(long fsid)
{
    FILE *mnttab;
    struct mntent *ent;
    struct statfs buf;
    int result;

    mnttab = fopen("/etc/mnttab", "r");
    ent = getmntent(mnttab);
    while (ent) {
	result = statfs(ent->mnt_dir, &buf);
	if (result == 0) {
	    if (fsid == buf.f_fsid[0]) {
		printf("mount point: %s\n", ent->mnt_dir);
		fclose(mnttab);
		return(strdup(ent->mnt_dir));
	    }
	} else {
	    perror("statfs failed");
	}
	ent = getmntent(mnttab);
    }
    fclose(mnttab);
    return(NULL);
}

long pstat_executable(long *inode)
{
    struct pst_status pst;
    int target = getpid();

    if (pstat_getproc(&pst, sizeof(pst), (size_t)0, target) == -1) {
	perror("pstat_getproc failed");
	return (0L);
    } else {
	printf("The text for this process is inode %d of file system %u.\n",
	    pst.pst_text.psf_fileid,
	    pst.pst_text.psf_fsid.psfs_id);
	*inode = pst.pst_text.psf_fileid;
	return (pst.pst_text.psf_fsid.psfs_id);
    }
}

long inode;
char *executable;

int match_inode(
    const char *obj_path,
    const struct stat *obj_stat,
    int obj_flags,
    struct FTW obj_FTW)
{
    if (obj_stat->st_ino == inode) {
	executable = strdup(obj_path);
	return(1);
    }
    return(0);
}

int main(int argc, char *argv[])
{
    char cwd[PATH_MAX+1];
    char candidate[PATH_MAX+1];
    char *base_dir, *path, *dir;

    /* First handle the easiest case */
    if (argv[0][0] == '/') {
	printf("executable is '%s' (from /).\n", argv[0]);
	return 0;
    }

    /* See if argv[0] was path from cwd. */
    if (strchr(argv[0], '/') != NULL) {
	if (getcwd(cwd, PATH_MAX+1) == NULL) {
	    perror("getcwd failed");
	} else {
	    printf("executable is '%s/%s' (from cwd).\n", cwd, argv[0]);
	    return 0;
	}
    }

    /* Try the PATH. */
    path = getenv("PATH");
    if (path != NULL) {
	path = strdup(path);
	for (dir = strtok(path, ":"); dir != NULL; dir = strtok(NULL, ":")) {
	    strcpy(candidate, dir);
	    strcat(candidate, "/");
	    strcat(candidate, argv[0]);
	    if (access(candidate, X_OK) == 0) {
		printf("executable is '%s' (from PATH).\n", candidate);
		return 0;
	    }
	}
	free(path);
    }

    /* Hunt it down */
    executable = NULL;
    base_dir = fs_name(pstat_executable(&inode));
    if (base_dir) {
	nftw(base_dir, match_inode, 40, FTW_PHYS|FTW_MOUNT|FTW_SERR);
	free(base_dir);
    }
    if (executable) {
	printf("executable is '%s' (from mount point).\n", executable);
	return 0;
    }
    printf("executable is lost.\n");
    return 1;
}
 _________________________________________________________________
 To leave this mailing list, send mail to majordomo@cxx.cup.hp.com
    with the message UNSUBSCRIBE hpux-devtools
 _________________________________________________________________


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic