Under Ubuntu 12.04, I've written the following C program to help me shut down my server's apache2 and samba services while I run automated backups. Notice that in the Makefile I'm setting the SUID bit so that the program will have root privileges when run by the lowly user tmv
.
services.c:
#include <stdio.h>
#include <stdlib.h>
void usage(char * arg0) {
printf("Usage: %s start|stop\n", arg0);
exit(1);
}
int main(int argc, char ** argv) {
fprintf(stderr, "Running as: ");
system("whoami");
if (argc != 2) usage(argv[0]);
if (!strcmp(argv[1], "stop")) {
printf("Before running rsync, we need to shut down apache2 and smbd.\n");
system("service apache2 stop");
system("service smbd stop");
} else if (!strcmp(argv[1], "start")) {
printf("After running rsync, we need to start apache2 and smbd.\n");
system("service apache2 start");
system("service smbd start");
} else {
usage(argv[0]);
}
return 0;
}
Makefile:
all: services.c
gcc -o services services.c
chown root:tmv services
chmod u+s services # allow elevation to root
chmod o-rx services # only user tmv should execute
Here's what I get:
tmv@patience:~$ ./services start
Running as: root
After running rsync, we need to start apache2 and smbd.
* Starting web server apache2 [ OK ]
start: Unable to connect to system bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
Running as root works fine:
# ./services start
Running as: root
After running rsync, we need to start apache2 and smbd.
* Starting web server apache2 [ OK ]
smbd start/running, process 8515
Any ideas why my ./services
isn't working as expected when run as the user tmv
? Do I need to configure some environment variables too?