#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Perl Flock Problems


    Hello all,
    I am making a web site that revolves around a single file that stores all the passwords, usernames, and emails of all the users. Of course, whenever I want to edit this file, I flock it, so that it does not have two programs writing to it at the same time. Now I am finding that sometimes, when my processes that have flocked the file have stopped running, other processes still have to wait for a flock. Now, I was under the impression that when a process terminated, all it's open file descriptors were closed and with the file descriptors, all it's flocks. I am running this on Debian Linux with the Apache web server. Any suggestions?
  2. #2
  3. /usr/bin/drinking
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Posts
    718
    Rep Power
    1890
    Do you test to make sure everything is okay before you exit the program?
  4. #3
  5. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    83
    Rep Power
    142
    flock() has issues. I recommend you avoid it. I suggest you use a database. They are secure, stable, and take care of problems like this. If not, then I suggest you create a daemon to handle password lookups and changes. Since it is the only one accessing the file, it doesn't get confused. See `perldoc perlipc` to see how your CGI can communicate with the daemon.
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,878
    Rep Power
    3891
    Originally Posted by shawnhcorey
    flock() has issues.
    What kind of issues?

    @solpic, can you post the code you're using?
  8. #5
  9. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    83
    Rep Power
    142
    Originally Posted by ishnid
    What kind of issues?
    1. flock() is completely voluntary. Any program can ignore it and change the file at any time.
    2. The lock on the file does not necessarily go away if the process holding it dies.
    3. The lock on the file does not always go away when the system is rebooted. This depends on the OS; some do, some don't.
    4. On some OSes, the lock is released if the process dies but not if it's an zombie.


    I recommend using a database because:
    1. Synchronization of the data is handled by the database.
    2. Encryption is handled by the database.
    3. Backup is handled by the database.
    4. Uniqueness of user ID is handled by the database.
    5. All the issues of using flock() are handled by the database.


    In other words, someone has already resolved the issues and you should take advantage of this. Don't re-invent the wheel.

    Comments on this post

    • keath agrees : Sorry no points to give. I would strongly recommend the database as well.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0
    I was going to use a database, and I was writing it in C++, but I found it easier to do it this way. All the Perl programs that use the user database use the same module, so that makes it safer. I realize that programs can ignore flocks, and that is why I use the same module for all the programs that is compliant with other flocks. The code I am using for the flock, even though the things in between vary, is this:

    Code:
    open(FH, "+<".$users_db) or die("Can't open user database");
    flock(FH, LOCK_EX);
    //do my things here
    close(FH);
    Between the flock I open up other file handles on the same file, I only put the flock on to make sure no other process writes to it while I am reading and writing.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0
    Sorry everyone, it seems that flock was working correctly. I found an errant flock in my code within another flock, so in other words I tried to flock a file twice without closing the original one.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0
    Hello all,
    I am making a web site that revolves around a single file that stores all the passwords, usernames, and emails of all the users. Of course, whenever I want to edit this file, I flock it, so that it does not have two programs writing to it at the same time. Now I am finding that sometimes, when my processes that have flocked the file have stopped running, other processes still have to wait for a flock. Now, I was under the impression that when a process terminated, all it's open file descriptors were closed and with the file descriptors, all it's flocks. I am running this on Debian Linux with the Apache web server. Any suggestions

    Thanks
  16. #9
  17. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    83
    Rep Power
    142
    When a process terminates gracefully, all its file descriptors, flocks, and other filesystem paraphernalia are cleaned up properly. When a process just dies, like with a kill -9, almost anything could be left behind. To deal with this problem, you have to get your program to restart gracefully, that is, to clean up anything that is not right before it gets to work. And it should periodically die just so it will restart gracefully.

    Just like modern databases do. I did mention something about not re-inventing the wheel.
    Last edited by shawnhcorey; July 13th, 2009 at 08:30 AM. Reason: Changed to a better example
  18. #10
  19. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,274
    Rep Power
    0
    I think this thread is stuck in an infinite loop. How is post #8 exactly the same as #1?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0
    How exciting!

IMN logo majestic logo threadwatch logo seochat tools logo