Converting caffeine to code, shooting pics, and cruising the roads of life.

Archive for the ‘C/C++’ Category

pthreads – the love hate relationship

Sunday, March 12th, 2006

Since Friday afternoon, I have been working on JayantHTTPD trying to get it to serve up pages threaded style, using a worker thread model. It took research on Friday night and all day Saturday to get it going, but I have succeeded.

The first main issue I had was figuring out how to dynamically allocate an array for the number of threads set in the configuration file. Since I’m used to C++, we never malloc’d our arrays, we used things like std::vector. The solution to this issue was amazingly simplistic. Just a threadinfo_t* workers = (threadinfo_t*)malloc((sizeof(thread_info_t)*server->maxthreads)); :)

The next bug I found coming from the old crappy threading system was that it closed stdin, stdout, and stderr… I have no clue how I didn’t see this, nor why I’m even mentioning this embarrassing, n00by mistake, but I am.

Then once I had all of this handled, I had the file descriptors getting completely owned. I’m still not 100% sure what was up with them, but for instance, changing the following:

	if( (fd = accept(mast_fd, (struct sockaddr *)&their_addr, &sin_size)) != -1)
	{
		dprintf("fd %d accepted from fd %d\n",fd,mast_fd);
		add_request(fd, &request_mutex, &got_request);
	}

to

	fd = accept(mast_fd, (struct sockaddr *)&their_addr, &sin_size);
	if(fd != -1)
	{
		dprintf("fd %d accepted from fd %d\n",fd,mast_fd);
		add_request(fd, &request_mutex, &got_request);
	}

Fixed one issue. The first code snippet will always return fd 1. No idea why, which caused some interesting actions when the thread went to send the page. ;) After this was fixed, I had to go through and convert some library calls to be thread safe, but this sadly still didn’t fix the handling of the requests.

As of this very moment, I’m still working on the actual handling of the requests. Right now it sends a 200 OK then dies before another if statement. Example:

	if(send(fd, "HTTP/1.1 200 OK\n", 16, 0) == -1)
	{
		dprintf("Failed to send Status 200 OK\n");
		fclose(in);
		goto cleanup;
	}
	dprintf("Send 200 OK\n");
	/* Dies here for some reason... */
	sprintf(tempstring,"Server: %s\n",server->signature);

So this is how I spent my weekend. You can see my active social life gleeming through, eh?

Jayant lives again!

Monday, March 6th, 2006

I must say I’m proud of the progress with Jayant. It can now serve basic html pages and handle 404’s. Each connection is processed via a pthread as I said the in the previous post. I fixed two buffer overflows in it today as well.

Tomorrow I’m going to try to get a mime type system up. I want to the /etc/mime.types file if possible, this means I’m going to need a custom parser for it. I’m debating whether to write one, or go the easy route and find a public domain one. I guess it just depends on how much time school gives me.

As of today, Jayant also runs on two platforms with no modifications, FreeBSD and linux. So I can only assume it will run on any other unix. I’ll be trying BeOS within the next few days, as well as a native SkyOS compile.

cody@devnulled:/home/cody/jayant>./jhttpd
Server Port: 8181
Server Log Dir: /home/cody/jayant
Server Signature: JayantHTTP (FreeBSD) 1.0.0
Adding callback for fd 3
Realloc’ing fd_tasks
No callback for 1
No callback for 2
Listening on port 8181 fd 3

cody@laptop:~/jayant$ ./jhttpd
Server Port: 8181
Server Log Dir: /home/cody/jayant
Server Signature: JayantHTTP (Linux) 1.0.0
Adding callback for fd 3
Realloc’ing fd_tasks
No callback for 1
No callback for 2
Listening on port 8182 fd 3

Here is a little bite to chew on, the first page served by it:
jhttpd
Click for larger image

printf(”ERROR: Invalid mm on OS %s\n”, __ILLUSIONOS__);

Sunday, March 5th, 2006

Today I found out that I’m still going to have to do work to IllusionOS’s memory manager to get multitasking up because the current one won’t take into account of each process’ address space. I’m not sure what to do here, completely rewrite the thing or adapted it. I may try to adapt it to the current system, but either way it won’t be easy for me. If there is one think I suck at, it’s writing a memory manager. This made me very depressed for some reason.

A few round of Counter-Strike:Source later and I was fairly undepressed, but felt like I needed to accomplish something. So I started working on JayantHTTPD again. I finally got the rewrite listening for connections now via my custom libserver.so. I finally decided how I’m going to handle the connections after reading up on pthreads. They really aren’t too complicated, and as long as I don’t need Windows support, work on basically every OS. Right now libserver creates a thread with a callback specified to that file descriptor, but you may also have a custom connection handler for you application. So basically the server will hand out connections via a thread instead of forking.

Support me! Buy this. I’ll be using the money from these to rebuild my SGI 1100. :)
P.S. If you were wondering what pthreads page I was reading, check this out.

Interesting day

Monday, February 20th, 2006

Today I ventured over to the University of Cincinnati to meet with Dr. Hal Carter, the computer science department head. My mom set up an appointment for me so that he could tell me more about the programs that UC runs for computer science and computer engineering.

He was rather amazed at what I already knew for being only 15. After talking a little about what he has taught, he was amazed to see that I’ve done work on Sourceforge, know C/C++, and have my own OS. He seemed kind of baffled how I taught myself all of these things. This makes me wonder about all the other programmers that are my age and have more skill than me. Maybe we could all benefit from a meeting like this?

After talking about the classes for a while, he then showed me around the campus. They had some pretty interesting things around. In one of the extra rooms (an old utility closet?) the graduate students have a room dedicated to themselves. In it were a few SGI octanes, couple parallel computer setups, and a custom designed gaming console they have been working on for a few years. If that isn’t cool enough for you, just having a keycard identification system won’t work. You need a custom one! These students wired the door up to a computer and hung a keyboard on the outside so they could just enter the keycode…

Next I went around to a few labs where kids were building circuits and designing them in CAD for their computer engineering classes. They sure have some super, heavy-duty breadboards in those labs… I found it quite interesting that they had so many SGI and SUN machines. They have several labs that are 100% SUN workstations…

Then we walked into a room with something that really grabbed my attention. Their server room was amazing. There were three, count’em, three beowulf clusters. One had 32 SMP machine nodes. The other two were 64 SMP nodes. One of the 64nodes was *brand* spanking new. To top off this multi-hundred thousand dollar setup. There was probably 5 Sun servers (don’t remember what type) laying around the room.

Didn’t see too much that drew my attention after this. Got to see the level 10 clean room and a few other pieces of equipment used in creating microchips.

After that I went back to my mom’s office and he said that he was amazed at what I knew once again. This is what seems kind of cool, he said that he could learn a lot from what I already know. Good or bad?

On the way home my mom decided I was going to go through the Chemical Materials department as well. Waste of time. I am ABSOLUTELY not interested in this stuff. w00t! I can see crystalline structures in a microscope and do things to them! They are needed no doubt, but that is not the field for me…

Today I also got some coding time in on osdever.net. The forums have come a long way over the past few months. It’s pathetic it’s take this long, but I have more important things to do I guess. Editing of posts should be done in maybe 30 minutes. After that it’s just moderator controls and the forums are done. I’ll probably do a code audit for security holes and optimizations after that is complete.

From there on it’s just finishing up the tutorials system, adding all the existing tutorials into the database, and tweaking the templates. :) Admin control modules can come later I guess, they won’t take that long to write.

In other news, multitasking is a PITA.

I’m also going to try something new here, weekly PHP tips. I’m not sure if I’ll focus on a designated area or just anything handy, but here is Week #1’s.
PHP Tip #1 – Week 1

This week I’m going to start with an essential security basic. Do not trust any data the user submitted to your site or that the user can edit. This includes query strings and forms usually. Make sure you do the appropiate cleaning of $_POST data.

I usually cast any number values to integers when I clean my $_POST vars. This means that any string that isn’t a number is set to 0. When I’m cleaning user data that needs to be entered into a database, I relyon mysql_real_escape_string to do the dirty work for me. I hope this little tip helps a few of you. =D

woah!

Tuesday, January 31st, 2006

Well this site has gotten quite a few visit since my discovery of the music protection hole on purevolume.com. (~800 visits).

Well, I’ve been doing a lot of work on the new web site for osdever.net and I must say it’s coming alone quite well. Check out the demo and leave me a message on the forums while you’re still there.

I still have a few things left to do on the forums. I’m having problems integrating GeSHi syntax highlighter and the bbcode system because when you go to nl2br() the post’s message, you’ll get double spaced code from GeSHI. This is because GeSHI specifically adds the \n’s to the code it generates so the source looks alright. So I have a hack in there to make it work right now, but that keeps us from adding \n into the code, as it becomes just “n.”

I have also begun working on JayantHTTP as of yesterday. It seems pretty portable right now, so hopefully I can get it on *nix and windows. Granted i’ts no all that far, but hopefully we’ll be sending out pages within the next few days.

I am also currently moving to FreeBSD as my main OS on my laptop, so this may not get updated for a while because if you’ve seen some of my other posts, it takes quite a few hours to get a desktop enviroment setup on it.

Bye for now.