Question

I have application that has a several process, including the authentication process. I need to prevent launching of authentication, if connection was established and authentication successfull. How can I implement this behavior? Platform - linux.

Was it helpful?

Solution

You could take advantage of a shared semaphores and a small shared memory. Let's call the semaphore mutex and the shared memory bool is_authenticated. mutex is initialized with a value of 1 and is_authenticated with a value of false.

Then your authentication process becomes:

wait(mutex);

if (!is_authenticated)
    authenticate();
is_authenticated = true;

signal(mutex);

You would then have to take care of authentication expiration. So when the session is over:

wait(mutex);

assert(is_authenticated == true);  /* if not, you have been compromised */
deauthenticate();
is_authenticated = false;

signal(mutex);

OTHER TIPS

You can simply check in your process in there is already the same application running. There are powerful libraries that allow you to do advanced checking, locking resources and more ... depending on the language your using.

The easiest solution consists in creating a lock file (in /tmp for example) that would indicate that the program is running, and checking if a such file exists at the beginning of your program. The cons of this method is that you have to be sure the lock file will be deleted even if the app would crash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top