How do I unset TXTBSY on a daemon's binary?

Paul Falstad pfalstad at phoenix.princeton.edu
Fri May 10 19:17:29 AEST 1991


A.Raman at massey.ac.nz (A.V.Raman) wrote:
>1. What is the accepted procedure to unset the TXTBSY flag on the binary
>   after the daemon backgrounds itself?
>2. How do I find out if a particular user is logged on from within a C
>   program.
>
>I need to do (1) because I want to be able to make changes to the code
>and compile and reinstall it,  but the changes are not so important that
>the currently running daemon needs to be killed and restarted.  Currently
>when I try to install a new version when the old daemon is running, I
>get the error message: Text file busy.

Instead of trying to copy the new file on top of the old one, remove the old
file first, and then put the new file there.   You don't want to unset
the TXTBSY flag; if you do this, the running daemon will probably dump core
the next time it page faults in the text segment.

>I need to do (2) because I want to find out whether I must write a 
>user or mail him regarding some status change he required.  I tried doing
>a man -k on "user", "wtmp", "logg" etc., but none gives me any info about
>a C library function / System call that tells me whether a particular user
>is logged on.

Easy way:

int ison(char *s) {
char buf[BUFSIZ];

   sprintf(buf,"who|grep %s >/dev/null",s);  /* or grep -s */
   return !system(buf);
}

Hard way:

#include <stdio.h>
#include <utmp.h>

int ison(char *s) { 
struct utmp u;
FILE *in;
int ret = 0;
   
   in = fopen("/etc/utmp","r");
   while (fread(&u,sizeof u,1,in))
#ifdef USER_PROCESS
      if (u.ut_type == USER_PROCESS)
#else
      if (u.ut_name[0])
#endif
         if (ret = !strncmp(u.ut_name,s,8))
            break;
   fclose(in);
   return ret;
}

This code is probably wrong; it's taken from zsh.  :-)

--
              Paul Falstad  pfalstad at phoenix.princeton.edu
         And on the roads, too, vicious gangs of KEEP LEFT signs!
     If Princeton knew my opinions, they'd have expelled me long ago.



More information about the Comp.unix.questions mailing list