LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-24-2007, 11:26 PM   #1
RavenLX
Member
 
Registered: Oct 2004
Posts: 88

Rep: Reputation: 15
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.

Last edited by RavenLX; 02-24-2007 at 11:28 PM.
 
Old 02-25-2007, 03:41 AM   #2
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
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.
 
Old 02-25-2007, 05:46 AM   #3
RavenLX
Member
 
Registered: Oct 2004
Posts: 88

Original Poster
Rep: Reputation: 15
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).
 
Old 02-25-2007, 12:13 PM   #4
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
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) )
 
Old 02-25-2007, 12:15 PM   #5
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
..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.
 
Old 02-25-2007, 01:04 PM   #6
RavenLX
Member
 
Registered: Oct 2004
Posts: 88

Original Poster
Rep: Reputation: 15
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.

Last edited by RavenLX; 02-25-2007 at 01:06 PM.
 
Old 02-25-2007, 01:20 PM   #7
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
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.
 
Old 02-25-2007, 01:36 PM   #8
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
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
 
Old 02-26-2007, 07:17 PM   #9
RavenLX
Member
 
Registered: Oct 2004
Posts: 88

Original Poster
Rep: Reputation: 15
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.
 
  


Reply

Tags
c++, kdevelop, mysql, programming, services


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
Linux Video Tutorials-Suggestions please! fleeced Linux - General 2 02-18-2008 01:56 PM
any quick start tutorials for mandrake? furtivefelon Mandriva 4 12-25-2004 03:38 PM
suggestions for slack dns tutorials fazz Slackware - Installation 0 06-27-2004 05:10 AM


All times are GMT -5. The time now is 02:43 AM.

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