LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 03-23-2011, 02:09 AM   #1
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 355

Rep: Reputation: 33
quicker way to start or disable service


I've been practicing C programming the past month and had the idea to do this. I don't know if there's already something similar, or if anyone would find this program useful.

Slervice simply calls Pat's scripts and the chmod command. I believe it's self explanatory. It can be compiled with
cc slervice.c -o slervice

Code:
//      slervice.c
//      
//      Copyright 2011 Andy Alt <andyqwerty removeit users.sourceforge.net>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VER "0.01.1a"
#define DATED "March 23, 2011"

int main(int argc, char **argv)
{	
	printf("\nslervice %s %s\n\n", VER, DATED);
	
	if (argc != 3) {
		printf("Usage: slervice <service> <start,restart,disable,enable>\n");
		printf("Ex:    slervice httpd restart\n");
		exit(1);
	}
	char cmd[256];
	if (strcmp(argv[2],"enable") == 0) {
		strcpy(cmd,"chmod -v +x /etc/rc.d/rc.");
		strcat(cmd,argv[1]);
		
	}
	else if (strcmp(argv[2],"disable") == 0) {
		strcpy(cmd,"chmod -v -x /etc/rc.d/rc.");
		strcat(cmd,argv[1]);
	}
	else {
		strcpy(cmd, "/etc/rc.d/rc.");
		strcat(cmd, argv[1]);
		strcat(cmd, " ");
		strcat(cmd, argv[2]);
	}
    printf("Executing %s\n", cmd);
	system(cmd);
	
	return 0;
}
 
Old 03-23-2011, 04:37 AM   #2
Richard Cranium
Senior Member
 
Registered: Apr 2009
Location: Carrollton, Texas
Distribution: Slackware64 14.1
Posts: 1,568

Rep: Reputation: 462Reputation: 462Reputation: 462Reputation: 462Reputation: 462
I should live up to my username....

Code:
#! /usr/bin/bash
if [ $# != 2 ]; then
    echo "Usage:   $0 <service> <start,restart,disable,enable>"
    echo "Example: $0 httpd restart"
    exit 1
fi

case $1 in
    start|restart)
        exec /etc/rc.d/rc.$1 $2
        ;;
    disable)
        chmod -v -x /etc/rc.d/rc.$1
        ;;
    enable)
        chmod -v +x /etc/rc.d/rc.$1
        ;;
    *)
        exec /etc/rc.d/rc.$1 $2
esac
 
Old 03-23-2011, 05:47 AM   #3
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 355

Original Poster
Rep: Reputation: 33
Richard, thanks for the Bash conversion.

Here's a C update:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VER "0.01.2a"
#define DATED "March 23, 2011"

#define MAX_LEN 256

void CheckSize(char *argv[]);

char cmd[MAX_LEN];

int main(int argc, char **argv)
{	
	printf("\nslervice %s %s\n\n", VER, DATED);
	
	if (argc != 3) {
		printf("Usage: slervice <service> <start,stop,restart,disable,enable>\n");
		printf("Ex:    slervice httpd restart\n");
		exit(1);
	}
	
	
	if (strcmp(argv[2],"enable") == 0) {
		strcpy(cmd,"chmod -v +x /etc/rc.d/rc.");
		CheckSize(argv);
		strcat(cmd,argv[1]);
		
	}
	else if (strcmp(argv[2],"disable") == 0) {
		strcpy(cmd,"chmod -v -x /etc/rc.d/rc.");
		CheckSize(argv);
		strcat(cmd,argv[1]);
	}
	else {
		strcpy(cmd, "/etc/rc.d/rc.");
		CheckSize(argv);	
		strcat(cmd, argv[1]);
		strcat(cmd, " ");
		strcat(cmd, argv[2]);
	}
    printf("Executing %s\n", cmd);
	int t = system(cmd);
	
	if (t) {
		printf("An unknown error ocurred.\n");
	}
		
	return t;
}

void CheckSize(char *argv[]) {
	short a, b, c, len_tot;
	a = strlen(cmd);
	b = strlen(argv[1]);
	c = strlen(argv[2]);
	len_tot = a + b + c + 1;
	
	if ( len_tot > (MAX_LEN - 1) ) {
		printf("Command line too long. Exiting...%d %d %d\n", a, b, c);
		exit(1);
	}
}

Last edited by Andy Alkaline; 03-23-2011 at 01:02 PM. Reason: more updating
 
Old 03-23-2011, 12:25 PM   #4
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
@richard,
I think you meant case $2 in

@Andy, glad to see you spotted the potential buffer overflow in your original, but I think your calculation is still one out....
Code:
else {
		strcpy(cmd, "/etc/rc.d/rc.");
		CheckSize(argv);	
		strcat(cmd, argv[1]);
		strcat(cmd, " ");
		strcat(cmd, argv[2]);
You need to add one to the len_tot calculation to cater for that additional space character:

Last edited by GazL; 03-23-2011 at 01:19 PM. Reason: spelling
 
1 members found this post helpful.
Old 03-23-2011, 01:05 PM   #5
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 355

Original Poster
Rep: Reputation: 33
Thanks, GazL.

len_tot = a + b + c + 1;
 
Old 03-23-2011, 06:57 PM   #6
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 109Reputation: 109
The simplicity of the task seems to negate the need in efficiency from using a compiled program. Not to say that the program isn't nice or useful. It is.

But the execution of services are already using bash scripts anyway. What's one more execution of another short lived shell to execute a wrapper script to execute the service script?
 
Old 03-23-2011, 07:19 PM   #7
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
Shell script is definitely the right tool for this sort of job, I was under the impression that Andy was just doing this as a C coding learning exercise though.
 
Old 03-23-2011, 09:46 PM   #8
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 355

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by lumak View Post
The simplicity of the task seems to negate the need in efficiency from using a compiled program. Not to say that the program isn't nice or useful. It is.
Thank you.

Quote:
But the execution of services are already using bash scripts anyway. What's one more execution of another short lived shell to execute a wrapper script to execute the service script?
As GazL mentioned, it was a learning exercise.

I had another idea regarding the service startup scripts. Allow me to illustrate in a diff manner:

Code:
# diff rc.sshd rc.sshd.old 
22,25d21
< sshd_edit() {
<   $EDITOR /etc/ssh/sshd_config
< }
< 
54,56d49
< 'edit')
<   sshd_edit
<   ;;
58c51
<   echo "usage $0 start|stop|restart|edit"
---
>   echo "usage $0 start|stop|restart"
(I export EDITOR into the environment from a script in /etc/profile.d)
 
Old 03-24-2011, 12:38 AM   #9
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 109Reputation: 109
err... sorry... I miss things in posts sometimes. Either way, looked like a great opportunity in messing with c-strings... I hate those things. But then... it helps to have a better understanding of pointers as well.

I just hate the whole
const char "string" vs. pointer to 's''t''r''i''n''g''\0'

Last edited by lumak; 03-24-2011 at 12:41 AM.
 
Old 03-24-2011, 07:17 AM   #10
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
Quote:
Originally Posted by Andy Alkaline View Post
I had another idea regarding the service startup scripts. Allow me to illustrate in a diff manner:
I can see where you're going with that, and it's kind of an interesting concept. it sort of gives the service scripts a sort of object orientated feel, and you could add all sorts of features for various services. An install/setup call for rc.mysql for example.

However, the way to UNIX enlightenment is to "Do one thing and do it well". Admin assistants/wizards and front ends really don't belong in the service startup/shutdown scripts which really ought to be kept as simple as possible. if one must have them at all, then they should be invoked from front end programs such as your 'service' program, or Richard's bash script, or even bigger interfaces such as SUSE's YAST.

Debian used to have this sort of functionality built into their packages(they probably still do, but it's been years since I tried debian), You used to do something like "dpkg --reconfigure" and it'd do setup type stuff related to that package. I never really liked that sort of thing though.
 
1 members found this post helpful.
Old 03-24-2011, 09:47 AM   #11
Richard Cranium
Senior Member
 
Registered: Apr 2009
Location: Carrollton, Texas
Distribution: Slackware64 14.1
Posts: 1,568

Rep: Reputation: 462Reputation: 462Reputation: 462Reputation: 462Reputation: 462
Quote:
Originally Posted by GazL View Post
@richard,
I think you meant case $2 in
Yep, you're right. So much for posting in the wee hours of the morning.

(Then again, my getting it wrong makes my reply an even better match to my nick. )
 
Old 03-25-2011, 01:13 AM   #12
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 355

Original Poster
Rep: Reputation: 33
lumak, no apologies necessary. Not only do I also miss stuff sometimes, but if I added up all the things I did wrong in my life, it might wind up being a long int (unsigned).

GazL, your explanation makes sense to me. Thanks.
 
  


Reply

Tags
services, startup


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable Service port Paris Heng Solaris / OpenSolaris 3 02-08-2009 11:18 PM
disable vsftpd (or any) service on start up krazyglue Linux - Software 2 12-06-2007 03:15 PM
when i try to start samba pdc's smb service nmbd failed to start . sandeepchau123 Linux - Newbie 2 10-20-2007 02:59 PM
disable cold start and warm start snmp traps chkisco Linux - Newbie 0 10-28-2005 03:58 AM
How to disable service linuxturtle Linux - Security 1 09-23-2003 11:30 PM


All times are GMT -5. The time now is 09:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration