0

I made my daemon use fanotify API to control access to files. Here is the working thread:

void * threadProc( void * data )
{
    if( data == NULL ) return 0;

    RealTimeDrvrImp & _this = *( ( RealTimeDrvrImp * )data );

    const unsigned int fi_flags = FAN_CLOEXEC | FAN_CLASS_CONTENT | FAN_NONBLOCK;
    const unsigned int fi_event_f_flags = O_RDONLY | O_LARGEFILE;

    _this.m_fa_fd = fanotify_init( fi_flags, fi_event_f_flags );
    if (-1 == _this.m_fa_fd )
        return NULL;

    const unsigned int fm_flags = FAN_MARK_ADD | FAN_MARK_MOUNT;
    const uint64_t fm_event_f_flags = FAN_OPEN_PERM /*| FAN_ACCESS_PERM*/ /*| FAN_CLOSE_WRITE*/;

    if (-1 == fanotify_mark( _this.m_fa_fd, fm_flags, fm_event_f_flags, 0, "/" ) )
    {
        close( _this.m_fa_fd );
        return NULL;
    }

    char buf[4096];
    int len = 0;
    struct timespec tmsp = { 0, 1000000 };//500 miliseconds
    pid_t self_pid = getpid();

    while( _this.m_DoAvRealtimeScanThread )
    {
        if(-1 == ( len = read(_this.m_fa_fd, (void *) &buf, sizeof (buf))) )
        {
            if( EAGAIN == errno )
            {
                nanosleep( & tmsp, NULL );
                continue;
            }
            else
                break;
        }
        const struct fanotify_event_metadata *metadata
                = (struct fanotify_event_metadata *) buf;    

        while (FAN_EVENT_OK(metadata, len)) {
            if (metadata->fd != FAN_NOFD ) {
                if (metadata->fd >= 0)
                {
                    bool bCloseFdNow = true;

                    if(  metadata->mask & FAN_OPEN_PERM ||
                        metadata->mask & FAN_ACCESS_PERM )
                    {
                        bool bWriteNow = true;
                        struct fanotify_response response = {0,0};
                        response.fd = metadata->fd;
                        response.response = FAN_ALLOW;

                        if( metadata->pid == self_pid )
                        {//this process event, always allow
                        }
                        else if( _this.IsReplyReadyNow( response ) )
                        {//response.response is set in IsReplyReadyNow();
                        }
                        else //else event is added to a queue, 
                             //will be handled and closed later in another thread
                        {
                            bCloseFdNow = false;
                            bWriteNow = false;
                        }

                        if( bWriteNow )
                        {
                            pthread_mutex_lock( & _this.m_faWriteMtx );
                            write( _this.m_fa_fd, &response, sizeof (struct fanotify_response ) );
                            pthread_mutex_unlock( & _this.m_faWriteMtx );
                        }

                    }
                    if( bCloseFdNow )
                        close( metadata->fd );
                }
            }
            metadata = FAN_EVENT_NEXT(metadata, len);
        }
     }

    close( _this.m_fa_fd );
    _this.m_fa_fd = -1;    
    return NULL;
}

It works correctly. If I stop the daemon before rebooting or shutting down everything is okay. But if I'm trying to reboot the system or shutdown whith the daemon running, the system freezes.

I thougth that maybe the system sends SIGSTOP to it's daemons on reboot/shutdown, is that correct? If so the daemon cannot allow any access to any file and that locks the system?

Please help.

I'm using Ubuntu 12.04 64-bit with kernel 3.11.0.

kopalvich
  • 434
  • 5
  • 14
  • I believe I see SIGKILL being sent to daemons. I maybe wrong also. – DumbCoder Jan 28 '14 at 18:00
  • Yes, I think so too, but are the daemons somehow suspened in the meanwhile? – kopalvich Jan 28 '14 at 18:04
  • How are other user processes stopped during reboot ? Maybe you could follow that. Probabaly open a browser and start the reboot. – DumbCoder Jan 28 '14 at 18:07
  • Depending on your init script, your daemon will normally see a SIGTERM (15) and and then a SIGKILL (9). With no init script, you will see only a SIGKILL when `upstart` decides it's had enough of any non-terminating processes. – abligh Jan 28 '14 at 20:43

1 Answers1

0

I found out why it blocked.

Apparently on reboot linux intensively accesses files, which are controlled by my daemon. Each access must be allowed in _this.IsReplyReadyNow() call, which in it's turn uses several syslog() calls to log filesystem events. On reboot in syslog after some of my entries came this message:

'imuxsock begins to drop messages from due to rate-limiting'

and after that my daemon blocked, and stopped to allow or deny file access permissions, and so blocked the system.

When I commented out syslog() calls, the system finally rebooted.

kopalvich
  • 434
  • 5
  • 14