473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How to check for already running program?

Hi all,
I'm new to C.

I have successfully written a small C program that acts as a "wrapper"
around cgi scripts. These scripts perform admin tasks such as backing up
etc. Obviously, The need for the "wrapper" was to get the scripts to run
with "root" privileges. So far so good. I simply create a "URL shortcut"
that some staff can double-click on to initiate the process.

However, because I am creating these automated tasks for management staff
of a company, I want to make sure that they don't go laucnching these tasks
twice or similar (2 or more similtaneous backup processes to the same drive
at the same time is not nice).

I thought the bast way to address this was just to grab a "process listing"
and use the available "regcomp and regexec" functions to check that there
is not already and instance of the designated script running. If so, baulk
and exit with a message.

The thing I don't understand; is that when testing this, any attempts to
run additional instances of the script using the c program don't work,
because the "new instance" will wait until the old process finishes, and
then start (which is not what I want).

BTW: I call it like this
http://some.internal.address/cgi-bin...?fw_backup.cgi

Could someone please point me in the right direction?

TIA
/* This program was created to get around the problem */
/* of having to do tasks that require root privileges */
/* but aren't possible when run as a simple cgi script. */
/* Set this binary to run suid and make sure it's owned */
/* by root, and you're good to go. */
/* Nick Sinclair 2005 GPL it's all yours. */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <regex.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[], int ac, char *av[]){

/* Declare some variables */

FILE *cgi_file;
FILE *process_listing;

char line[130];
int counter = 0;

pid_t pid;

char *pattern = argv[1];

int regex_result;
regex_t re;

/* Let's do some sanity checks on the cgi script name */

/* Argument supplied at all? */

if (argc==1)
{ printf("\nNo CGI script name supplied - Freaking out and
exiting.\n\n");
exit(172);
}

/* Too many arguments? */

if (argc>2)
{ printf("\nToo many arguments supplied - Freaking out and
exiting.\n\n");
exit(172);
}

/* Does the file exist at all? */

cgi_file = fopen(argv[1], "r");
if (cgi_file==NULL)
{ printf("\nSupplied file cannot be read from or doesn't exist\n");
printf(" - Freaking out and exiting -\n\n");
exit(127);
}

/* Is the script already running - doh! */

regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB);
process_listing=popen("ps ax", "r");

while ( fgets( line, sizeof line, process_listing))
{
regex_result=regexec(&re, line, (size_t) 0, NULL, 0);
/* printf("Result = %d\n", regex_result); */

if (regex_result==0)
{
/* printf("%s", line); */
counter++;
}

}

pclose(process_listing);
regfree(&re);

if (counter>1)
{
printf("Script already running! - Freaking out and exiting.\n\n");
exit(172);
}

/* Set UID to 0 (root) */

setuid(0);

/* Now run the actual ".cgi" file */

/* pid=getpid(); */
execl("/usr/bin/bash", "bash", argv[1], (char *)0);

}

Nov 15 '05 #1
4 5975
One quite simple way would be to open a socket at a specified port when
the application starts. When the second instance starts and tries to
open the same port it will fail. One has to take care however that no
other application open the same port while the application is not
running.

A better way might be to create a appname.pid-file in /var/run/ when
starting the application and check it one exists when starting. Problem
with this is that if the application crashes before removing the the
file can prevent it from starting again. To prevent this check the PID
in the file and see if any running process has this PID.

--
Erik Wikström

Nov 15 '05 #2
"Nick Sinclair" <nu*********@NOFRILLS.ssl-mail.com> wrote in message
news:jo********************************@4ax.com...
[snip]
However, because I am creating these automated tasks for management staff
of a company, I want to make sure that they don't go laucnching these
tasks twice or similar (2 or more similtaneous backup processes to the
same drive at the same time is not nice).


I don't know of a reliable method within the scope of ANSI C, the topic of
this newsgroup. On Unix systems, file locking can be used for mutual
exclusion - you should ask about this in a platform-specific newsgroup,
perhaps news:comp.unix.programmer.

Also, you need to be particularly wary of security issues when you expose a
means to invoke processes as root. Again, off-topic for this newsgroup.

Alex
Nov 15 '05 #3
# "Nick Sinclair" <nu*********@NOFRILLS.ssl-mail.com> wrote in message
# news:jo********************************@4ax.com...

# > However, because I am creating these automated tasks for management staff
# > of a company, I want to make sure that they don't go laucnching these
# > tasks twice or similar (2 or more similtaneous backup processes to the
# > same drive at the same time is not nice).

Decide on some specific file path in the system.
int fd = open(path,O_WRONLY|O_CREAT,0600);
int rc = flock(fd,LOCK_EX|LOCK_NB);

If rc==-1, another process is already in the critical section.

If rc==0, this process has acquired the lock. If the process exits
for any reason, the lock is released by the kernel. The process can
also exit the critical section with
flock(fd,LOCK_UN);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no respect for people with no shopping agenda.
Nov 15 '05 #4
Let's see what SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org>
has up their dress.
# "Nick Sinclair" <nu*********@NOFRILLS.ssl-mail.com> wrote in message
# news:jo********************************@4ax.com...

# > However, because I am creating these automated tasks for management staff
# > of a company, I want to make sure that they don't go laucnching these
# > tasks twice or similar (2 or more similtaneous backup processes to the
# > same drive at the same time is not nice).

Decide on some specific file path in the system.
int fd = open(path,O_WRONLY|O_CREAT,0600);
int rc = flock(fd,LOCK_EX|LOCK_NB);

If rc==-1, another process is already in the critical section.

If rc==0, this process has acquired the lock. If the process exits
for any reason, the lock is released by the kernel. The process can
also exit the critical section with
flock(fd,LOCK_UN);


Sweet.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Amrik Singh | last post by:
HI folks can anyone help a newbie here. I would like to know how I can check if my software is already running. So that if the user click s on the shortcut to start the progam again it maximises...
3
by: Fabio Pliger | last post by:
Hi, is it possibile, in python, to check for an already running instance of an application? My problem is that, if my program i running and the user relaunch it, i don't want to open a new...
6
by: Pierre-Yves | last post by:
Hello, I would like to prevent my perl program to be executed several times simultaneously (if the program is already running, I would like to display a message like "another instance of this...
4
by: TM | last post by:
Is there any way to check if another instance of my program is running if a user executes it ? I would like to warn the user if the program is already running to prevent multiple instances. ...
3
by: Henry | last post by:
oHi, I want to check for a Windows form program if an instance of the prgram is already running: if yes the program's window should be activated and no second program instance should be started....
12
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
14
by: Alex K. | last post by:
Hi all I'd like to avoid starting the same c# application twice on the same computer. How do I check if it is already running? Thank you
1
by: keri | last post by:
Hi, I use the below for the user to view their outlook calendar Sub DisplayInbox() Dim myolApp As Outlook.Application Dim myNameSpace As Outlook.NameSpace Dim myFolder As Outlook.MAPIFolder...
1
by: =?Utf-8?B?WWFlbA==?= | last post by:
Hi, How can I check from C# code if outlook program if the process is already running? if not, I run outlook with: System.Diagnostics.Process.Start("OUTLOOK.EXE"); Thank you!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.