[comp.unix.questions] How do I unset TXTBSY on a daemon's binary?

A.Raman@massey.ac.nz (A.V.Raman) (05/10/91)

Hi all,

I've got two questions regarding a daemon I'm writing.

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.

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.

Thanks in advance.

- &

pfalstad@phoenix.princeton.edu (Paul Falstad) (05/10/91)

A.Raman@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@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.

les@chinet.chi.il.us (Leslie Mikesell) (05/12/91)

In article <1991May10.042300.5901@massey.ac.nz> A.Raman@massey.ac.nz (A.V.Raman) writes:

>1. What is the accepted procedure to unset the TXTBSY flag on the binary
>   after the daemon backgrounds itself?

Just mv it to a different name on the same filesystem so you can replace
it with the new version.  Later you can rm the old version.  If the
program is being frequently executed, arranging to mv the old copy
to another directory that is later in the PATH of the users will insure
that either the old or new copy will be executable at all times.

>2. How do I find out if a particular user is logged on from within a C
>   program.

>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.

If the notification is important, I'd mail it in all cases.  Just being
logged in does not insure that you will see a "write" message.  But
why not just execute write and let it fail if the user isn't logged
in?  You have to be prepared for failure anyway, since the user might
log out between your test and the execution of write - why do you
need any other test?.

Les Mikesell
  les@chinet.chi.il.us