LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   quicker way to start or disable service (http://www.linuxquestions.org/questions/slackware-14/quicker-way-to-start-or-disable-service-870398/)

Andy Alkaline 03-23-2011 01:09 AM

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;
}


Richard Cranium 03-23-2011 03:37 AM

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


Andy Alkaline 03-23-2011 04:47 AM

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);
        }
}


GazL 03-23-2011 11:25 AM

@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:

Andy Alkaline 03-23-2011 12:05 PM

Thanks, GazL.

len_tot = a + b + c + 1;

lumak 03-23-2011 05:57 PM

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?

GazL 03-23-2011 06:19 PM

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.

Andy Alkaline 03-23-2011 08:46 PM

Quote:

Originally Posted by lumak (Post 4301123)
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)

lumak 03-23-2011 11:38 PM

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'

GazL 03-24-2011 06:17 AM

Quote:

Originally Posted by Andy Alkaline (Post 4301230)
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.

Richard Cranium 03-24-2011 08:47 AM

Quote:

Originally Posted by GazL (Post 4300556)
@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. :D )

Andy Alkaline 03-25-2011 12:13 AM

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.


All times are GMT -5. The time now is 11:48 AM.