LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need Suggestions for Tutorials - Where to start? (http://www.linuxquestions.org/questions/programming-9/need-suggestions-for-tutorials-where-to-start-532154/)

RavenLX 02-25-2007 12:26 AM

Need Suggestions for Tutorials - Where to start?
 
I have Mandriva 2006 LE (Free Edition) and choose KDE for my desktop environment and using KDev.

I want to create a program which will run as a service (ie. in the background).

This program will also store and retrieve data from a MySQL database.

Anyone know any books/tutorial on these topics:

Writing services in Linux (C++)
How to use MySQL Database in C++

Suggestions are appreciated.

I know I'm creating this in KDev but the system will be runnable without any XWindows at all or even if XWindows is running, it won't matter. And I'll want to start this app after booting and after MySQL is running, of course. :)

I guess I also will eventually make a GUI-based config system but I'll want to keep it text for now.

slzckboy 02-25-2007 04:41 AM

I have integrated mysql into a C program b4
The actual mysql site has C api documentation on it as below.

http://dev.mysql.com/doc/refman/5.1/en/c.html

Not sure if there is an C++ port?....

some examples using the C api

http://www.linuxfocus.org/English/Se...ticle304.shtml.

RavenLX 02-25-2007 06:46 AM

Thank you. That looks quite informative for the MySQL part of it. I did figure out enough to create a table and column.

Now to learn how to make the program run as a service (which can be on by default when booted, and started/stopped like other services).

slzckboy 02-25-2007 01:13 PM

simple example daemon code.

Code:

#include <stdio.h>
#include <sys/times.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syslog.h>
int main()
{
        pid_t pid,sid;
        int fd,len;
        if((pid=fork())<0){
        perror("fork");
        exit(EXIT_FAILURE);
        }

        if(pid>0){
        puts("parent Exiting.");
        exit(EXIT_SUCCESS);
        }

/*====================DAEMON================================*/


        puts("child process");
        openlog("printime",LOG_PID,LOG_DAEMON);
        if((sid=setsid()) <0){
        syslog(LOG_ERR,"%s\n","Fork error");
        exit(EXIT_FAILURE);
        }

        if((chdir("/"))<0){
        syslog(LOG_ERR,"%s\n","chdir error");
        exit(EXIT_FAILURE);
        }
        umask(0);
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
        char *pnt;
        time_t timebuf;
        while(1){
        time(&timebuf);
        pnt=ctime(&timebuf);
        len=(strlen(pnt)+1);
        if((fd=open("/var/log/robsdate.log",O_CREAT|O_WRONLY|O_APPEND,0600)) <0){
        syslog(LOG_ERR,"%s\n","open error");
        exit(EXIT_FAILURE);
        }
        write(fd,pnt,len);
        close(fd);
        sleep(10);
        }
        closelog();
        exit(EXIT_SUCCESS);

}

creating a daemon is simple.

The steps for creating a daemon are to fork a child process and then make the parent exit.
We need to disassociate the process from the current terminal session.Daemons are not interactive and so don't need a controlling terminal.
We call setsid to create a new session making the child process autonomous from for terminal session that it is invoked from,be that from a startup script or directly from the command line.

We then make the root directory the current working directory for the daemon.

(This is to ensure that a daemon dosn't prevent a mounted filesystem from being unmounted for whatever reason;i.e going to signle user mode etc.)

We then set its umask to 0 as the process's inherited mask may stop it from being able to make folders or files etc.

We then close down its inherited file descriptors that it will not need;i.e STDIN/OUT/ERROR,as the daemon will not echo characters to the terminal or take input from the command line.

when we enter an infinite loop to do whatever it is the daemon is going to do.

This should be enough for you to do your own research.
Also look into the openlog system call and how it is used to handle errors etc.

This is all paraphrased from Linux Programming by example by Kurt Wall.

A beginners book to C coding in LInux.
I still use it for reference from time to time(Obviously :0) )

slzckboy 02-25-2007 01:15 PM

..ohyeah

Once you have compiled your daemon and have your binary file you start it at startup like any other service i.e via a startup script.

RavenLX 02-25-2007 02:04 PM

Thank you. Looks like a good starting point.

Though the program I will be creating is for a system-wide artificial intelligence system that will interact with the user via speech recognition and speech output. It will also need to write files, create folders and act as a common user (once the admin sets up an account for this AI system to use). Naturally, the AI system will NOT have root access. ;) It will also have limited access to it's own systems only as necessary.

Some of the things you mentioned about not giving it permission to write to files, I may have to have a way to give it permission. Seems like it's adjustable though.

About the C++ port of the MySQL C api; there was one, but it looks like it's no longer being maintained. It was called MySQL++. I am using the libmysql library and it seems to be working well enough for what I'm intending to need to do.

slzckboy 02-25-2007 02:20 PM

Quote:

We then set its umask to 0 as the process's inherited mask may stop it from being able to make folders or files etc.
sorry if this was unclear.


the point of umask(0); is to give the process the ability to write to root owned folders and files.i.e like /var/log

Your project sounds very interesting.
Good luck.

introuble 02-25-2007 02:36 PM

By the way, you might want to ask in a mandriva-specific forum (or a subform here on LQ if it exists) about writing the actual init script which will manage your application. Or perhaps the folks at mandriva already have some official documentation in this sense.

You might also want to check out:

Code:

man 3 daemon
Code:

man 2 setuid

RavenLX 02-26-2007 08:17 PM

slzckboy - Thank you for clearning that up. I guess I'll keep it set at 0. Yeah, it's an interesting project. :) I've always wanted to dabble in AI programming. Maybe if I get something accomplished I may sourceforge it and put out a call for developers to help out. I think it'd be nice to open source an AI project like this.

introuble - Thank you for the info. and starting places. :) I probably will be needing extra info on creating the startup script.

I already a lot of the MySQL class I was making done. I have now the more difficult code to do on that.


All times are GMT -5. The time now is 01:38 PM.