So let's go ahead and get started here
Good morning
Good morning. Everyone is Google. So I, My screen share this morning is a bit different, because I realize I wanted to browser.
I got a question that Google Chrome is bad. Yes, Google Chrome is bad use.
Firefox, although firefox I will say there is no specific. So there's a question I I did get the tailors who have tickets.
I had to do it later on, connect from other device. But I did.
That particular window never refreshed. There is no specific ta for grade.
Your assignment to, because it was graded by question.
But firefox is skin chromium.
No, no. Just okay, yeah, edges. So 5 note .
There are
Yeah.
You know
So
So just just a little quick thing about web browsers there's only 3 rendering edges that can handle modern web link web kit and get go firefox and and note that webkit blink is a fork a web
kit. So Getco is the only the only other real independent implementation of our engine that can render the web.
People are now running so much tailor to chrome.
There are some sites that break in firefox that work in chrome, but that's not because firefox is doesn't have enough features.
It's because there are slight implementation. differences in their taking advantage of little bugs or things that are in grown, and in fact, very irritating.
Yeah, and Firefox is so much better privacy protections it's not even funny.
No earlier.
Side note, Although web browsers are really an operating team, question No.
Anyway up is Ios based. I you know Ios is Mac Os.
They have the same kernel. they have the same user land mostly. There's just some configuration differences to make Ios lockdown.
Yes, I the difference between android and ios are actually largely overstated in many ways, although for the re re rendering engine stuff, it's very real.
But I can't even completely object to it because honestly if Apple started letting other browsing, Yes, Firefox would be there.
But Chrome would also be there, and it's very possible that ever would be on a chrome.
So I do appreciate that apple is enforcing diversity for the web browser ecosystem, even if it does irritate me in many ways.
Moving on let's talk about let's go back to operating systems. here.
I'd love to keep talking about web stuff that that's a whole other class.
So where are we now? We are now on November seventeenth.
You know. So we have. So where is so? Assignment 3 is due on the 20 first, which is before Monday.
It was a Monday night, Tuesday. I will. I was unless.
Yeah, I was playing on going on the solutions on the 20 s note that there is no class on next Thursday, because it's us Thanksgiving.
So I am taking that day off
So there, there's a question here is there a operating systems on arm versus X.
86 I will say from an operating system perspective.
It really doesn't matter except for standardization. x 86 platforms are much more standardized than arm, and that's part of the reason why it's actually much harder to get new kernels running on android
devices or any any you know. arm device, because oftentimes. they have to customize the linux kernel to run on that specific platform, and those customizations would then have to report it to you know run a newer
kernel, and it sometimes millions of lines of code that have changed It's something in It's insane the level of customization happens on arm platforms to make each thing work.
So X 86 has the advantage of being standardized and that's very nice for an operating system, perspective, but otherwise not really yeah assigned over.
Do I mean standardization? i'll buy all x-axis share of some modules. it's not just modules.
Modules are heat. I was where the variation is.
Is that booting up on an X 86 system is standardized.
How do you find out what devices are there How Do you initialize everything that's all standardized?
There is kind of a stamp there there's some standard mechanisms for for arm platforms.
But it's much more each one's much more of a one off thing there isn't the only reason why Pcs are so standardized is because of the work.
Originally by Ibm and then Microsoft to make it a uniform platform.
Basically Microsoft wouldn't say if you don't make it work this way.
It Won't Run Windows if it doesn't run windows. You can't sell it so the hardware makers had to follow Microsoft's lead in terms of making you know making a standard platform with Arm because
they typically just can get Linux running. and Android also allows for so much customization device.
Makers who control the hardware can basically hack up the software to make it work on their device.
And thus not worry about standardization. So this is a weird way that software freedom leads to less freedom, such as the weird thing of our world.
Okay, So these are all these are all weird things.
Let's go let's talk about so anyway.
But this is where we are right now. Assignment 3 is here.
Assignment. 4 will be out before assignment.
3 is due i'm hoping to have it done basically today or tomorrow.
So you go and start working on that And then, of course, you know, assignments tutorials, 5 and 6 are also due by the end of you know.
Basically you have Monday to finish off the the tutorials, 5 and 6, hoping most you've already gotten checked off, because you know, we're now going on a tutorial 8 and then 9.
So. but but yeah, So we'll continue talking about 2 but we'll continue with you. trial right now, so but 5 and 6 are due on Monday, ,
Monday, 20 first.
7,
Okay, So just Wanna: get that there written down. Yeah. So
So
Yeah as I say it's I I it's it's it.
Yeah, I'll be accepting it till just for class on Tuesday, but you should really finish it up Monday night. I used to Bill. There's a little bit of buffer time there.
That's all the logistics I wanna talk about I do now want to let us now go on to tutorial 8, and so we've in 2 tutorial is a tutorial a we've already gone
through the simple module. and we've talked some about the ones module, although I will want to go I'll be start talking with the ones module.
I'm going to bring that up here hold on
We're gonna have all these windows up here, which is one reason why I try to stick with the emacs.
I find it just gets less water. for
Now we've got the code here. let's start going through this because we talked about the Oh.
Okay, before doing that, I see? i've got some questions in the chat. Now, how does one find the 9 name and Yeah.
So just i'll just show this right here see find out
If you do. Ls Dash, if you had the dash I you got dash I to a Ls command.
You can see the I know number if I do
So now, since I made this new text, new dot text has the same.
I node number as this so they're both referring to the same.
I know 2 names. Now you can ask I created new texts later after lecture 18, but they're both have the same timestamp, because the timestamp modification refers to the I know first to the I node not the
name. How do I? to a first approximation lecture?
18 dot text and new dot text are completely equivalent names for the same file.
There's no fundamental difference between them. I can delete one and the other one will stay.
I, in order to delete the I node I have to delete both names for it, and that's also.
Why the link count Now, now, you might ask so, which is the original name, which is the new name?
Well, this is the new name, because I created a second but that's the only real difference is I created second so in the assignment you have to go figuring out.
I nodes. And while they names you need to go through the script and figure out when they got created that's all i'll say, Yeah, yeah, yeah, it basically I made a hard link, That's why they have the same name
Yes, anyway, back to modules. So we've got this Yeah, if there's any other one.
This is my last class before assignment. 3 so if you have more questions about assignment.
3 I can talk. i'm happy to talk about that inside of talking about this, but this is the new material, so I think I've come.
I think i've covered things right now up to that but if let me know. I can take breaks.
So here's here's the licensing information author subscription.
So this is metadata that's associated with the module
Then we have declaring what's the initialization function with the exit function.
The exit function is it's? it's easy to see it tells you what does a little info logging What's that info thing at the top?
We actually define some macros info does pr info, you know you know the class name this so
It's another. it's it it just makes it a little bit easier.
It's not strictly necessary, but so for logging info level.
I'm going this but then i've got these 3 things here.
So I destroy the device I enriched to the class and then register the character device.
So the idea is that so? what are we talking about with classes and things?
What a what is that? Well, if we go into
If you go into slash, Proc.
Why don't I have a syntax highlight Why not have that enabled here, anyway?
So if we go into actually No, I think it's probably zoom and slash this, the files are much different.
So in here. It gives a nice view of everything on the thing.
If you look under devices, you know it's giving you how the devices are connected.
But there's another view of it you go into and and if you look at Slash. Dev.
It tells you Well, the character devices block devices. Yeah.
Char so here's all the character devices
These are all the character devices. But the this, all this is idol, symbolic link to the Devices Directory.
This is where it's connected by how it's connected to the system?
What what's interesting Here is there's this class directory. It turns out that in the kernel to organize this vast collection of devices and other things, they divide them up into classes and inside the classes they
have other things so like. For example, there are you know there's this block blue to some of them make sense.
Oh, Bluetooth devices graphics you know but there's this.
Is not this these categories evolved over time and so they are not completely consistent, and I would not look at them for complete consistency.
They're just groupings of devices but under those things like we go under
Now let's go let's go under under Bluetooth, you'll see I have a bluetooth device by go under and Vm.
E that's my you know that's you know I have one, and you know one of those devices so Nvme.
That's a you know fast sol state disc so what happens here when we play with the ones module.
So let's go into so
I don't have it loaded good
Okay.
Loaded. good. we've loaded the right module now the Let's look in slash sess let's go into class.
You'll notice in here conf 3,000 appears they cop 3,000. There's the ones and the ones actually points to dot dot dot dot devices actually.
So so we got devices that's right actually virtual here's all the virtual devices, and it just called 3,000 of us and the ones.
And there's all this stuff about the ones
No power subsystem so info on it for testing the largest file that can be created in the continue.
Okay. So
Which part I i'm right there talking about the ones module, which is those questions where am I talking about things?
So the ones module i'm talking about so the simple kernel module is the first one.
This is a character's device from a colonel module so it's it's long.
Oh, you're for testing the largest following and creative containers is is creating and figuring out Max file Sizing create only way to find it out there in a different, better way.
I suggest for creating a large file. Just use Dd.
And go from like Dev Dev 0, and just make a giant file and listen.
Let it go until it fills up a disc so that's what i'll say about okay back to this.
So this is this is what i'm talking about here now, going back to you know.
Let's let's now so when we talk about classes it's literally just time about these classes that you can see when you go into a slash system.
It's just a way of organizing these things and So when you create a you need to choose the class. it's going to be in or make a new class, which is what we do here.
So So one's major, and so first thing we have to do is create the actual character device.
So the ones and member devices have all devices. Have a major number and a minor number
So what you're a major number which designates the driver and a minor specify the device.
So if we go in just like so we look up tty, you know, like all these, all these tty's, you notice all like the serial.
Ttis all have the same major device. The capital less means is the serial teach y.
So the major device is, for so the serial driver is the one responsible for those.
But then and that same drivers possible these. But then you have things like tty print.
K. That's a completely different driver. but you'll notice amongst all these teach-wise there's numbers here.
That second number refers to which one. it is the so you know there's a lot of built-in Tgis, and they all, you know 0, you know this.
But oh, that's interesting t toy 0 is one device driver.
But just dev Tty is something completely different. I think that probably has to do with
Yeah, pseudo-tis have this thing, you know 5.
But then this one has 1, 36. So, but this is 0 that corresponds to the 0
Anyway. So this is what's happening with so Major minor, not the devices
So if I had multiple ones devices I could give them each a separate number.
So here.
I'm brushing a character device this device name dev ones, and then the register of the character by so I'm.
Specifying the minor number here of 0 and the major number is what it will return.
It's giving you a dynamic one and if it says it's less than 0.
We go like we couldn't register the device and so we exit out?
Is there a range for both major and minor numbers? or are they just assigned randomly so?
Minor numbers the driver. controls i've specifying the minor number right here, so it can be whatever it want, because again it's for the driver.
It uses the minor number to distinguish between its own devices.
The major number is dynamically assigned by the kernel.
On older unix systems. The major numbers were statically defined, and work and like saying, Oh, this one here!
But now there's so many devices and I think there can only be 255 major numbers on any given system.
So they now go to dynamic allocation I don't I don't.
I'm sure there's they must have lifted that 2 55 number have a chart.
And so then we need to create a class for the device so like what? what?
What capability is this going to fit in? And so we say, this module class name.
So what is this module? I think that may be automatically defined?
If it's not what whoops
Yeah, that's automatically defined so So for this module we're creating the name class name in class name.
We defined up here the device name is ones class name is defined as comp 3,000.
And so that's what we're specified here is that the clock, you know we create this module class name.
And again Here we just check to see if there's an error and fail to register you know this, and then we return, you know, sit in one class, and then we go to failed. class.
Rich. So this is
I'm not quite sure why have to go pull your error this came from Yeah, i'm i'm not sure.
By the way.
So this module is defined in the kernel. Does it refer to the assigned name?
The Colonel delegated. Yes, yeah exactly I don't know exactly what this module resolves to It's obviously defined in one of these headers.
But yeah, so this module comes predefined there's a number of things you'll find that are that are pre, you know, predefined sometimes in in the kernel source.
And it's good to note those so we so here we got a class, so we got One's made you know our device got a class, and then you notice we set the device node our new class to be the one we got here you know
the ones dev node where's one step node
Oh, right that's a function so one's class dev note.
So when we say, dev note here, this is actually a office function that it wants.
That tells the mode, you know, basically gives information on the on the device.
No, no I'll, i'll Explain this stuff in a second, and then we create the actual device here where we say I know what you know.
Device create one's class you know make a device and one's Major 0, a macro for doing things and then give it the device name.
If we got an error for it. We have to do this now.
Why do we have all these Go-to's one thing weird in coding in the Kernel?
Is it's very common to see go-to-s why do you think
Hey? thoughts.
So the the reason why there's all these go to
Without the goaches would be too many if statements it's not search for it's not the go-to's are not used to make loops.
Is because this the nested estates exactly it's used for better error handling.
So you'll notice all these go-toes are going to the end.
You can't do a go to between functions and see go to our limit to within a function.
And so what this got does is it basically lets you say, Okay, we've got an error I want to. I could just try returning or doing the error handling here.
But what if I have a bunch of standard things that have to be done?
And here you'll notice that in order to exit cleanly I have to do a bunch of things to undo the things I did.
And so here I just list all the things to undo the things I did, and where I jump in is the different places.
So if you fail in the device registration well, we have a successful class on register registration.
So we better unregister that if we had a field class registration what we don't need to register the class because it's already been a register.
But we do have to underestimate the character device, and if you failed the character device registration, well, nothing happens.
So you just return minus one. So, but we fall through here.
So that means we do, Debray. We do the class and register on paradigm and minus one.
You know, you know you have like object destructors things that you want to run the end and stuff.
This is kind of doing a poor man's version of it just doing it.
Very manual, but this is a very standard pattern in Colonel.
Come because you have to do error handling very carefully, and error handling means Typically, you did something, you allocate some stuff.
Oh, but you can't use it so you better deallocate it, because if you don't deallocate it, it's just gonna stay allocated forever.
Remember the kernel doesn't depend on being rebooted. It's supposed to be able to stay up or days, months, years, without a reboot, unless you need to do a software patch, and so it has to live
cleanly, which means it has to clean up after itself so much of the code.
The Colonel is error handling and you notice most of the What's this doing?
It's doing like 3 or 4 lines of code register character device, class create.
We do an assignment device create that's it that's all the work we actually did.
Everything else. Is there a handler
So go to breaks function blocks and behave. the case.
Damage to the I. Just yeah, Exactly. So remember a go-to just is that is a nonconditional jump.
It literally is go to here just means that that's it it's simple.
Remember if you translate this to assembling this is just a branch instruction, and that's so the semantics of C make it so.
These branches have to be within the scope of a function.
If your program is Assembly can go anywhere, See is a portable assembly in many ways.
And so you wants to be able to do things that are efficient assembly.
One of the things that wants to do is go do these sort of branches, but it doesn't want you to do it between functions, because that would be the spaghetti code.
It would be very hard to understand, and so, you know, you can only do it here
So just notice these, you know patterns here and then now let's try to understand.
There's a couple of key they just that she and and and things need to understand.
So remember what's what does it mean to implement a device?
A device is a is basically a weird file in your device.
File. And so when we say weird, what does that mean it means it does non standard things. When you're interacting with it, how do you interact with file?
You do file operations on it. You do things like open.
You do read, you do right. you do seek you do close.
So what it means to be a device is to a device, you know.
File is to have have custom, behavior, for those and that's how we define it literally.
We define a struct, a struct which contains pointers to functions with predefined, you know, signatures.
And we wanted to specify all the functions notice we don't specify close, and seek, and right there's all these other operations.
We aren't specifying and in fact if we go into the kernel. we find out the struct file operations.
It's a pretty big struct we should go look in the kernel for that.
But this We just specify the functions that we want.
So only things that are defined here are open, read, and release release gets called on.
Basically when it's not needed. it can be created by a close but not necessarily
Is this one of the things where you can't go to is cannot go go to labels.
Cannot exist outside the function that's compilation error and if you think about the semantics of C you'll realize why jumping inside of jumping into the middle of the function, actually doesn't make much sense how do you get to
the Colonel Logs
Also d message we'll do it oh.
D message is the command. Do that? used to be i'm not sure permissions on that, But it used to be.
We could read that more easily. but so there the logs are here too.
That's actually more of the raw thing but Now we want to look at it after It's gone through.
Now I should point out Okay, there's all kinds of Oh, that's this long term law. There we go that's where it is.
So it's a file so it has its own file operations so we define those here, and i'll walk through those.
But it's also in order to have a file you have to have an I node so how do we get info on?
The I know this is how we get info on the I know
So
We don't actually fill in the device. thing someone else will do that for us, but we will fill in the mode.
So, if we look at
You'll notice that its permissions is 0 for 4 4, which means It's world readable if we change the mode here, we would actually change what permissions it would have here.
And that's what changed. whether it was readable by a regular user or not.
So these so you could fill in more of the device.
I know here. but you don't have to what again that notice the functionality here.
We're just implementing what we want and other things we're just ignoring.
And so when you access it, you try to an operation that is a support like if I tried seeking on dev ones not support, it, will say permission. night operation not supported or Some samara like that you don't have
to build everything into the code. Now let's look at this struct file operations.
So let's bring up let's go here this is why I wanted a browser here
I don't think free electrons has been anymore we have Alexa is some good one.
So here we are and let's look for
File up operations defined here as a struct, and here is the struct struct file operations.
And you see in here this is all the operations that are supported on a file.
It's supported llc read right there's also this other right?
Enter Io, poll, iterate open flush, you know.
Release. Okay. So so in principle, you could implement any of these things.
But we don't have to if we're implementing a full file system.
We'd potentially had to implement I nodes and things and stuff.
But it's just that device you know file so really it's just one thing.
So we just and all the other stuff is basically taken care of by the Linux.
You know the legs kernel, so who knows we spec we create the file operation struct we pass that in. So now that's what's going to be called and then you know the rest of the stuff is just passing it around
So Now, once we're running is the module continuously running as the kernel is running No, it's just sitting there in the background.
In fact, one's in it will terminate it will just do these calls, and then terminate, and then the module won't be doing anything.
The only time the module code now will be run is if someone accesses what you know what the Kobe activated.
If you unload the module download the module, then once exit will get called.
But if we access the file slash, dev slash ones, then the code here will get accessible.
First of all, if you do, an you look at the Directory slash debt and want to get information on the I node, you know, on the dev file.
This functional get called, but otherwise ones open will get called.
When we do an open on the file, one's read will get called when we read on it from it and release will get called when we're you know done with it. so we're not we won't have any state to manage there's nothing
really to do much. So the release and the open basically do nothing.
The thing that does anything is one's read and this is the function that you need to understand precisely you don't understand this function.
Don't know what the heck is going on so really this is the code you should focus on, and it's worth going through it.
By bit. so. First thing it's a function he's got this declarate it's it's quite static.
That's just you know that's not really important for our purpose.
This is but the it returns a sized t Now you might want to see what's this sized T.
Oh, I really should call this sized team not size the elbow.
What this is saying is, this is a 64 bit
This is always a 64 bit size thing, and traditional sizes of files are thirty-two-bit.
Although I think size Ts size team might both be 64 bit in this kernel. So that's why this doesn't matter?
Then we have a function, and then it takes 1, 2, 3, 4 parameters.
So there is a struct file there is a buffer there's a length, and there is an offset.
Now what I want to compare this to
There we go let's compare this don't know I want read, not hoping opens board right Now we don't.
We're not doing anything with it interesting. but read what is the spack of read the read system call specifies a file descriptor, a buffer, and account. So, oops.
So we have some clear correspondence between these things. We have a buffer here.
That buffer seems basically the same as this, although we put it to character pointer, but avoid, so we're treating as a care about.
But that's a pointer we have a size t for length here, for account that the those would match up.
But then, instead of a file descriptor, we have struct file here, and we have an offset here
Why, why is there that difference
So if you look at a per process each process. in the kernel, we're gonna talk about the how processes are represented in the kernel in a bit.
But if you look at a power process is represented in the kernel, what a file descriptor is is an index into an array!
What's in an array of file structure. So here this index then gets translated to Oh, I want to be interacting with this file struct.
And then why do I also need an offset we also need the offset?
Because this file that we're accessing remember implicit in any read the current position in the file.
But here we don't know what the current position is in that file, so we have to pass it in with the current position is, and that's that.
Now it's actually a little confusing in the sense of there's actually a offset here, and there is an offset inside the file.
It actually maintains its own offset. but part of the internal kernel Api. Is that offset specified here?
I think, because that way you're not modified I don't think you're supposed to modify the file structure inside of these things.
Other parts of the system was supposed to do it so the only change we'd be making to it is this and so that's handled by other layers on the kernel.
Have the offset that's the offset inside the foul reading.
This is the structure of the file. we're actually accessing there's a buffer on the one.
So what is the struct file? What about? No, I was it?
Wow, here, file that is here's the struct file so what is it?
You might know some spin locks, you Texas, to allow for mutual exclusion.
On accessing this Oh, God, you tax and a spin lock something few texting help. Guess if you're sleeping versus not sleeping
Here's the path here's the I node room. Reference Use the file operation supported by this file struck me out.
No you'll under I I shouldn't expect you I don't understand everything here, but that just shows you it's just a strong
But then, but let's look at the logic of this?
What is this log? it's really simple We have an index, and we do 4.
I equals 0. I is less than length I plus plus
But user.
So remember this plus plus means. After this pointer is referenced.
Incremented, so put users getting off and then we increment off.
It's a is the post increment operator but why didn't I just do
This way.
Why don't we do it this way
Think about for a second
We have to you have to put a little quiz in there for this? So what's what's happening is the the user space?
So put user is serving a very important purpose. here it's we're being passed from a process a specific buffer that pointer potentially only makes sense in the context
Every process has its own address, Space and the kernel itself manages its own address.
Space. and that address space is right here you're seeing it here. because remember, this offset is a pointer to kernel data.
This file pointer is a pointer to a kernel data structure. But Buff is a pointer to a user space data structure will just you know a buffer.
So, in order to, you know, get data from it or put things into it, we have to use special routines to make sure we do the address space translation correctly where that buffer is interpreted in Its address. space.
Not the kernels address space and so that's why you use put users now on older versions of Linux and on some architectures you actually can get away without using put user.
But because of quirks, and how things are implemented.
But you should never do that. Whenever you're accessing user space, you wanna do this, because this also makes sure that errors are generating stuff.
Lots of stability and security issues can come up if you aren't using user in the full right to it.
But
So. So if we didn't so for potentially if this buffer, if we get so Jason, if this buffer, this pointer, if we didn't treat as a use space pointer, no this is coming from user space
so we could overwrite arbitrary parts of kernel data.
Yes, we could overwrite you know this could be a pointer to just something, and then we would override part of the kernel. if we didn't you know if we weren't careful this put user.
Checks it make sure that we're only putting so into user space, and we'll actually do some validation to make sure it's valid things.
But this way we aren't accidentally writing over data in the kernel.
Now if we clobber parts of the address space for a process well, that's the processes problem.
It gave us the pointer grover riding it sorry but we certainly don't want to overwrite any other process, and we don't overwrite kernel data.
So pointers coming from user space are dangerous business. You have to use them correctly.
Oh!
Hello, hold on a second. I'm here. alright Yes, Come here.
Okay, you want to say hi to everyone. Okay, you can say hi!
Everyone. we'll say hi, She's coming here?
Going like, Yes, this is shift. Are you on? Okay here, chef?
She. She was sitting at the door, just kind of crying at me and going like yelling.
So if i'll put her down here she would sorry
To get my attention. She'll start chewing cables of my computer and just look at me and try to get me to to move.
So yeah, everyone's paid their chat. How do you know what a cat wants to take a dump.
They just do it. They go and do their own thing yeah they don't tell you they just they're very private about such things.
Anyway.
Independent. But sometimes this cat wants attention on her terms. Anyway, what What was this thing about?
So remember every process, so oh, so oh.
So reading from dev ones basically gives you an unbounded number of ones.
But ones read only writes a finite number like you give it as a link.
That's all it's writing so where does the infinite come from
Is it looped There's clearly a loop somewhere where is the loop
The user space never gets an end to file. Notice this. Oh, every time it gives it a buffer the buffer is full like gets completely filled with ones.
And so you just keep calling and gets one more and more and more
Yeah, a a Jason. You got it exactly right so it's not so th the loop there is no loop in the kernel for this.
It's just saying we need from it the read always gives you more data, and it never reports into file.
And so you just keep reading. So this is important. So no.
When we look at the third module from from the tutorial.
So let's now
Let's get a new gift. Okay.
Hey? and also
I'm going to my local machine and the vm so I can access it
Okay, So Here's new get pid new get pid is based on ones very similar.
Let's first run it, so you can see What it does and then we can talk about how it works.
So we'll do a make again
I'm not sure if they can conflict but it's better to just remove one from the other.
So
Okay.
You get pid device registered, using Major 236 great
So what this does is when you read from this file. it tells you the pid of the process that accessed.
It. So, as I keep running it, I keep creating a new process, and I keep going.
Oh, your new pid is 40, you know 4,700.
So let's say some other processes being created as part of this i'm kinda curious.
Why Oh, oh, I don't know
Along me out. But no about the show Huh! yeah I don't know why that's doing that.
Okay, so that's what it's doing it's just outputing the process.
The across society of the current process. So if you look at this code here, basically you get a exit.
This. I know you can. You know this thing. This is all exactly the same.
Basically up. some names changed. Otherwise the function is that same.
This is the same close that's the same opens the same.
The only difference is read that's the difference so what's redoing.
So what's redoing so this is a little more common you know.
Let's go to. So this is the one dot scene so this is this is what it does.
This is that so? signature is the same. Yeah, start file, buffer length, offset
He's a little different customer. get pid is longer than once.
And now here we have an I. We have a message link, and then we have the pid.
So we create a static message, local, variable notice, making local variables and functions in the kernel.
You have to be very careful, because the stack in the kernel is very small. I'm.
A 100 Byte that's fine I wouldn't want to do a whole lot more. You want to do kilobytes.
You should have to allocate memory and that's a whole other thing.
Because you can't call malcolm because That's user space we'll see it so.
But first we just checked if offset's greater than 0.
We will return 0. Okay, and we'll talk about why that's there.
In a second, and then we we get the pid from task.
Tgid and V Nr. Current Cool
It was
It so current refers to the currently running task
So tasks is the abstraction that's used and you want to get what the current task is.
That's always current
So current is referring to the task that made the system call made the read system call that called us to make you know, cause this function to be run.
So you go into the kernel. You get this. No, you got here.
You go like what? what process you got me to run? find that, you know.
Use access current, and then we go. How would I know to do this?
How they figure that out. You have to look at the kernel source.
Where do you look in the kernel source? cause that seems kind of specific.
How would I know what the Api is? Well, what I do know is
All we do is we look at the source for the get pid system.
Call. How do we find that? So you have to go and search those different ways?
I'll see if I can just I don't think i'll actually here, because it's actually declared as
No, that's not what I want Oh, kelsey now, since I see.
Okay.
It is there Good job. But so here it is. So Cisco defined 0.
So this is defining system. Call let's go here yep Pid.
It's a process that is does it take any arguments Richard returns a process.
Id get V returns to process. Id of the calling process, sure.
So here, here it is. What does get Pi do? Do it?
Call task tgid vnr so I copied that that's all I did.
Does the kernel source contain comments to make it easier to find the functions we need.
It definitely contains comments.
They're only 2 yeah it's there are comments but it's not.
I mean it's really not intended to be just you know you have to explore the kernel.
It doesn't actually make it like super easy, but I will say this particular file, this kernel, Cis, c.
Very interesting, because if you look through here I could just look up all the Sis calls that are defined.
It defines a lot of basic set priority. get priority, get you know.
Get Gid get r a uid set uid set residue.
Id is there like a lot of you know, things involving permissions and process ids yet tough.
Id. These are all defined here times. it group the Id So there's a whole lot of system calls you name hold you name set host name, get host names that domain name.
No get resource limits. Whole bunch of system calls are defined in this file.
So I will just say, remember this thing it's actually referred to in the
You might wonder. Aren't there different versions of this file. Yes, there are, and that's what's cool about this is.
You can choose the kernel version that you want to look at this I'm: actually looking at 609.
This is technically not W What this is technically the kernel We're running 5, 15 this dash 53 is because of bunch of in the debian made some patches to it but it's basically linux
fit 5, 15. So what do you really want to do? is go over to 5 15, dot.
Oh, which is basically
So this is technically the version that you're running or at least I'm close to this, because it might have been patched.
But this is what's in the kernel source
The documentation. again the there are books written on the latest kernel.
People have written books. Some of them were quite good but the problem is, they all get out of date, because the kernel source keeps changing, keeps evolving, and frankly keeps getting easier to read and honestly it's
worth looking at different resources when you start trying to understand what the abstractions are.
There is documentation in the Linux kernel source tree which has some use.
There is a document so where is it documentation so there's a documentation directory, and it does have a number of files here, you know, hosting patches atomic fit operations.
Like It's like them talk about a bit nothing atomically There's all kinds of little things on the X 86.
There's you know what is this rst stuff microcode loaders, you know.
There's like all kinds of documentation in here on all kinds of things. You probably don't wanna go browsing through here most of the time.
So. Quite so, Jason. So yeah no it it so Jason. You're exactly right.
It's giving you a different pid because cat is external, and will fork to a sub shell process.
Then does the executive account, Yes, what i'm confused about is, I would expect the process Id to be incremented by one when I create a new process, and this is being incremented by 2 sorry almost one if it's because
of the cores or so like it's a 2 core system, and so it's those increments by 2, because I remember always it before like it ran by one I don't know there's no reason why it has to include
by I Don't think the background is creating one process on this Vm: Let's do not much anything else, so I don't know.
Interesting.
But just notice if you want to do something
Yeah, you're you're really want to go to the kernel, if I mean the the code is like at least it 15 million lines of code there's like all kinds of things going on there find something That's close to what you want
to do, and then and then follow its pattern. So or was it
So here we were with it. So we get the task struct.
Yeah, we, you know, get what it is, and then we do this sn print up.
Now sn print off
That's in the c library right didn't I tell you the C.
Library is not there. So how am I using sn printf?
Well turns out that the list has its own string, And you wonder what what file am I actually including here?
This is actually session user include Linux turns out workers
So
That's annoying
We don't want strings that age I don't know where that is. okay.
Why am I doing this? let's look here
Essential it's in colonel so here here's s printf is defined as external printf, and that's defined, and as a macro
I wonder which version it is Oh, there's the
Wow! it's defined all over the place really
Okay, that's not what I wanted I don't want that I want this prototype has put this code in there.
We go That's right one here's printf s printf what s s n per t vs printf Anyway, I'm getting lost there.
It's defined in the correct it has its own version so we're calling essence print off we have a buffer.
We say it's in the maximum size 100 we give it a string.
Your pid is present. D we go with the pid?
We get the message length as a string length of the message we check to make sure the length is here is looking out.
Make sure the length that's given up here is if it's less than that, we we truncate it to.
Basically, if you haven't been with a buffer that's large enough to hold all this, then we have to do that, then we copy.
Message. So we personally put the message into a kernel buffer, and then we copy it to the user space buffer.
We don't just write to it directly and then we update our offset to be Hi basically that's how many bytes were, you know, written.
And so notice what's how does the system know that there's no more bytes to read typically when you read from a file.
If it doesn't fill up the buffer then you know there's no more bytes
So, and then, and but that 0 is actually important also. because this way you might say that you know the buffers there maybe try reading again.
But if the offset is greater than 0 that means they're trying to read further into the file, we just say 0 there's nothing there.
And so then it knows that we've definitely reached the end of the file.
So we let's so we we can do our new cat
So this and then we can do yeah, the Id one.
No boxes equals 100
Okay, that works. we can give an outfit so it goes to you know the screen.
So that's fine now let's say
That didn't work so well
Nope. So that's interesting. that actually is probably a bug in my thing that if you if it tries seeking, I see.
Yeah, I have a question, you know why it does that let's
So what did it do? seek illegal sick seek?
Okay, and we'll seek and then it just does a read
And the read Doesn't succeed
Anyway, we're out of time here but i'm just sort of show.
I just wanted to show you this like so you can watch exactly what's happening here.
So here. what we do here is we know. I tell this, you know, and then it opens open for the yeah.
So does it open that it duplicates this to 0, and then it reads from that, and then it seeks it.
You can't seek on it 0 and then it reads from 0, and it says that and I get to know this.
That's what comes the buffer it gets 18 Byte, and then it amutely causes it
It's an illegal seek because the colonel says so
It works there. anyway. I will stop here, we're out of time play with this stuff.
There's lots of this is what you're supposed to Do and I walk through all the code and tutorial 8 at this point.
But now you should go and play with it on your own and see if you can try.
Get an understanding, or you don't know what's going on with it.
But of course you get an assignment 3 that's kind of important, anyway.
Have a great weekend. Good luck with the assignments for those you assigned for interviews.
I will see you tomorrow. I will open up more slots.
But otherwise I will see you all next week. Have a great weekend