22 #include "modules/system/users/Group.h" 23 #include "modules/system/users/User.h" 24 #include "pedigree/kernel/BootstrapInfo.h" 25 #include "pedigree/kernel/LockGuard.h" 26 #include "pedigree/kernel/Version.h" 27 #include "pedigree/kernel/machine/Device.h" 28 #include "pedigree/kernel/processor/Processor.h" 29 #include "pedigree/kernel/time/Time.h" 31 #include "file-syscalls.h" 33 #include "PosixProcess.h" 37 extern size_t g_AllocedPages;
39 MeminfoFile::MeminfoFile(
size_t inode,
Filesystem *pParentFS,
File *pParent)
40 :
File(
String(
"meminfo"), 0, 0, 0, inode, pParentFS, 0, pParent),
41 m_pUpdateThread(0), m_bRunning(false), m_Contents(), m_Lock(false)
43 setPermissionsOnly(FILE_UR | FILE_UW | FILE_GR | FILE_GW | FILE_OR);
48 m_pUpdateThread =
new Thread(
52 MeminfoFile::~MeminfoFile()
55 m_pUpdateThread->join();
58 size_t MeminfoFile::getSize()
61 return m_Contents.length();
64 int MeminfoFile::run(
void *p)
67 pFile->updateThread();
71 void MeminfoFile::updateThread()
77 uint64_t allocKb = (g_AllocedPages * 4096) / 1024;
79 "MemTotal: %ld kB\nMemFree: %ld kB\nMemAvailable: %ld kB\n",
80 freeKb + allocKb, freeKb, freeKb);
83 Time::delay(1 * Time::Multiplier::Second);
88 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
92 if (location >= m_Contents.length())
96 else if ((location + size) > m_Contents.length())
98 size = m_Contents.length() - location;
101 char *destination =
reinterpret_cast<char *
>(buffer);
102 const char *source =
static_cast<const char *
>(m_Contents);
104 StringCopy(destination, source);
110 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
115 PciDevicesFile::PciDevicesFile(
117 :
File(
String(
"devices"), 0, 0, 0, inode, pParentFS, 0, pParent),
120 setPermissionsOnly(FILE_UR | FILE_UW | FILE_GR | FILE_GW | FILE_OR);
127 PciDevicesFile::~PciDevicesFile()
131 size_t PciDevicesFile::getSize()
133 return m_Contents.length();
137 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
141 if (location >= m_Contents.length())
145 else if ((location + size) > m_Contents.length())
147 size = m_Contents.length() - location;
150 char *destination =
reinterpret_cast<char *
>(buffer);
151 const char *source =
static_cast<const char *
>(m_Contents);
153 StringCopy(destination, source);
159 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
171 "%02x%02x\t%04x%04x\t%x", p->getPciBusPosition(),
172 (p->getPciDevicePosition() << 4) | p->getPciFunctionNumber(),
173 p->getPciVendorId(), p->getPciDeviceId(), p->getInterruptNumber());
177 for (
size_t i = 0; i < 7; ++i)
182 if (i < p->addresses().count())
184 size_t length = p->addresses()[i]->
m_Size;
186 uintptr_t address = p->addresses()[i]->m_Address;
188 if (p->addresses()[i]->m_IsIoSpace)
194 thisResStart.Format(
"\t%16lx", address | flags);
195 thisResLength.Format(
"\t%16lx", length ? length + 1 : 0);
199 thisResStart.Format(
"\t%16lx", 0);
200 thisResLength.Format(
"\t%16lx", 0);
203 resStart += thisResStart;
204 resLength += thisResLength;
207 m_Contents += initial;
208 m_Contents += resStart;
209 m_Contents += resLength;
216 auto callback = pedigree_std::make_callable(printer);
220 MountFile::MountFile(
size_t inode,
Filesystem *pParentFS,
File *pParent)
221 :
File(
String(
"mounts"), 0, 0, 0, inode, pParentFS, 0, pParent)
223 setPermissionsOnly(FILE_UR | FILE_GR | FILE_OR);
228 MountFile::~MountFile() =
default;
231 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
234 generate_mtab(mounts);
236 if (location >= mounts.length())
242 if ((location + size) >= mounts.length())
244 size = mounts.length() - location;
247 char *destination =
reinterpret_cast<char *
>(buffer);
249 destination, static_cast<const char *>(mounts) + location, size);
255 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
260 size_t MountFile::getSize()
263 generate_mtab(mounts);
264 return mounts.length();
267 UptimeFile::UptimeFile(
size_t inode,
Filesystem *pParentFS,
File *pParent)
268 :
File(
String(
"uptime"), 0, 0, 0, inode, pParentFS, 0, pParent)
270 setPermissionsOnly(FILE_UR | FILE_GR | FILE_OR);
275 UptimeFile::~UptimeFile() =
default;
278 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
280 String f = generateString();
282 if (location >= f.length())
288 if ((location + size) >= f.length())
290 size = f.length() - location;
293 char *destination =
reinterpret_cast<char *
>(buffer);
294 StringCopyN(destination, static_cast<const char *>(f) + location, size);
300 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
305 size_t UptimeFile::getSize()
307 String f = generateString();
311 String UptimeFile::generateString()
317 f.Format(
"%d.0 0.0", uptime);
322 ConstantFile::ConstantFile(
323 String name,
const char *value,
size_t size,
size_t inode,
325 :
File(name, 0, 0, 0, inode, pParentFS, 0, pParent), m_Contents()
327 setPermissionsOnly(FILE_UR | FILE_UW | FILE_GR | FILE_GW | FILE_OR);
331 m_Contents =
new char[size];
333 MemoryCopy(m_Contents, value, size);
336 ConstantFile::~ConstantFile()
342 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
344 if (location >= m_Size)
348 else if ((location + size) > m_Size)
350 size = m_Size - location;
353 char *destination =
reinterpret_cast<char *
>(buffer);
354 const char *source = m_Contents + location;
356 MemoryCopy(destination, source, size);
362 uint64_t location, uint64_t size, uintptr_t buffer,
bool bCanBlock)
367 size_t ConstantFile::getSize()
372 ProcFsDirectory::~ProcFsDirectory() =
default;
393 m_pRoot->setPermissions(
394 FILE_UR | FILE_UW | FILE_UX | FILE_GR | FILE_GW | FILE_GX | FILE_OR |
400 String(
"."), 0, 0, 0, m_pRoot->getInode(),
this, 0, 0);
401 dot->setPermissions(m_pRoot->getPermissions());
402 m_pRoot->addEntry(dot->
getName(), dot);
405 m_pRoot->addEntry(meminfo->
getName(), meminfo);
409 m_pRoot->addEntry(mounts->
getName(), mounts);
412 m_pRoot->addEntry(uptime->
getName(), uptime);
414 String fs(
"\text2\nnodev\tproc\nnodev\ttmpfs\n");
416 String(
"filesystems"), fs, fs.length(), getNextInode(),
this, m_pRoot);
417 m_pRoot->addEntry(pFilesystems->
getName(), pFilesystems);
421 cmdline =
"noswap quiet boot=live\n";
422 NOTICE(
"cmdline is '" << cmdline <<
"'");
424 String(
"cmdline"), cmdline, cmdline.length(), getNextInode(),
this,
426 m_pRoot->addEntry(pCmdline->
getName(), pCmdline);
431 "Pedigree version %s (%s@%s) %s", g_pBuildRevision, g_pBuildUser,
432 g_pBuildMachine, g_pBuildTime);
434 String(
"version"), version, version.length(), getNextInode(),
this,
436 m_pRoot->addEntry(pVersion->
getName(), pVersion);
439 String(
"bus"), 0, 0, 0, getNextInode(),
this, 0, m_pRoot);
441 String(
"pci"), 0, 0, 0, getNextInode(),
this, 0, pBusDir);
443 pBusDir->setPermissions(
444 FILE_UR | FILE_UX | FILE_GR | FILE_GX | FILE_OR | FILE_OX);
445 pPciDir->setPermissions(
446 FILE_UR | FILE_UX | FILE_GR | FILE_GX | FILE_OR | FILE_OX);
448 m_pRoot->addEntry(pBusDir->
getName(), pBusDir);
449 pBusDir->addEntry(pPciDir->
getName(), pPciDir);
454 bus.Format(
"%02x", p->getPciBusPosition());
461 bus, 0, 0, 0, getNextInode(),
this, 0, pPciDir);
462 pPciDir->addEntry(dir->
getName(), dir);
472 "%02x.%01x", p->getPciDevicePosition(), p->getPciFunctionNumber());
482 fn, reinterpret_cast<const char *>(&space),
sizeof(space),
483 getNextInode(),
this, dir);
484 dir->addEntry(cf->
getName(), cf);
489 "%02x%02x\t%04x%04x\t%x", p->getPciBusPosition(),
490 (p->getPciDevicePosition() << 4) | p->getPciFunctionNumber(),
491 p->getPciVendorId(), p->getPciDeviceId(), p->getInterruptNumber());
495 for (
size_t i = 0; i < 7; ++i)
500 if (i < p->addresses().count())
502 size_t length = p->addresses()[i]->
m_Size;
504 uintptr_t address = p->addresses()[i]->m_Address;
506 if (p->addresses()[i]->m_IsIoSpace)
512 thisResStart.Format(
"\t%16lx", address | flags);
513 thisResLength.Format(
"\t%16lx", length ? length + 1 : 0);
517 thisResStart.Format(
"\t%16lx", 0);
518 thisResLength.Format(
"\t%16lx", 0);
521 resStart += thisResStart;
522 resLength += thisResLength;
525 m_PciDevices += initial;
526 m_PciDevices += resStart;
527 m_PciDevices += resLength;
528 m_PciDevices +=
"\t";
529 m_PciDevices +=
"\n";
534 auto callback = pedigree_std::make_callable(printer);
538 String(
"devices"), m_PciDevices, m_PciDevices.length(), getNextInode(),
540 pPciDir->addEntry(pPciDevices->
getName(), pPciDevices);
545 size_t ProcFs::getNextInode()
547 return m_NextInode++;
550 void ProcFs::revertInode()
557 size_t pid = proc->
getId();
562 auto procDir =
new ProcFsDirectory(s, 0, 0, 0, getNextInode(),
this, 0, 0);
563 procDir->setPermissions(
564 FILE_UR | FILE_UX | FILE_GR | FILE_GX | FILE_OR | FILE_OX);
576 m_pProcessDirectories.insert(pid, procDir);
577 m_pRoot->addEntry(procDir->getName(), procDir);
584 size_t pid = proc->
getId();
593 m_pProcessDirectories.remove(pid);
File * find(const String &path, File *pStartNode=0)
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
virtual bool initialise(Disk *pDisk)
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
void removeProcess(PosixProcess *proc)
virtual Timer * getTimer()=0
static Directory * fromFile(File *pF)
static ProcessorInformation & information()
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
virtual uint64_t getTickCount()=0
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
static void foreach(Callback callback, Device *root=0)
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
void addProcess(PosixProcess *proc)
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)