ADVERTISEMENT
Responsive Ad Unit • Systems Architecture & Low-Level Engineering

System Databases

UNIX systems maintain several databases that store information about users, groups, passwords, login sessions, and other system resources. Instead of reading these files directly, applications should use the standard library interfaces provided by the operating system. These interfaces provide portability, improve reliability, and isolate applications from implementation-specific file formats.

Learning Objectives

  • Understand UNIX system databases.
  • Learn why applications use database APIs.
  • Study user and group databases.
  • Understand database lookup functions.
  • Prepare for practical programming examples.

1. What are System Databases?

System databases are collections of information maintained by the operating system to describe users, groups, login accounts, network configuration, and other system resources. Applications retrieve this information through standardized library functions instead of directly reading database files.

2. Why are System Databases Needed?

  • Store user account information.
  • Store group membership information.
  • Support authentication mechanisms.
  • Provide standardized access to system information.
  • Improve application portability.
  • Hide implementation-specific storage details.

3. Common UNIX System Databases

Database Typical File Purpose
User Database /etc/passwd User account information.
Group Database /etc/group Group definitions.
Shadow Password Database* /etc/shadow Password information (implementation-specific).
Login Accounting* Implementation dependent Login session records.

*Availability and format depend on the operating system.

4. Why Use Database APIs?

Directly parsing system database files ties an application to a particular file format. Using the standard APIs allows applications to work correctly even if the underlying storage mechanism changes.

5. User Database

The user database stores information about user accounts, including user names, user identifiers, home directories, login shells, and related account information. Applications normally access this information using password database functions.

6. Group Database

The group database stores information about groups, including group names, group identifiers, and group membership. Applications use group database functions to retrieve this information.

7. Database Access Workflow

Application ↓ Database API ↓ Operating System ↓ System Database ↓ Requested Information Returned

8. Advantages of Standard APIs

  • Portable applications.
  • Consistent programming interface.
  • Improved maintainability.
  • Reduced dependency on file formats.
  • Simplified application development.

9. Applications Using System Databases

Application Purpose
Login Programs Retrieve user account information.
Shells Determine the user's login shell.
File Managers Display user and group ownership.
System Utilities Lookup user and group details.
Administrative Tools Manage accounts and permissions.

10. Interview Questions

  1. What are system databases?
  2. Why should applications use database APIs instead of parsing files directly?
  3. Name two common UNIX system databases.
  4. What information is stored in the user database?
  5. What information is stored in the group database?
  6. Why do standard APIs improve portability?
  7. Which file commonly stores user account information?
  8. Which file commonly stores group information?
  9. Why can the underlying storage implementation change without affecting applications?
  10. What types of applications commonly use system databases?

11. Password Database

The password database contains information describing user accounts. Applications use password database functions to retrieve user information without directly parsing the database file.

12. struct passwd

The password database functions return information using the struct passwd structure.

#include <pwd.h> struct passwd { char *pw_name; char *pw_passwd; uid_t pw_uid; gid_t pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; };

13. Members of struct passwd

Member Description
pw_name User login name.
pw_passwd Password field (implementation dependent).
pw_uid User ID.
pw_gid Primary group ID.
pw_gecos User description or account information.
pw_dir Home directory.
pw_shell Login shell.

14. getpwuid()

The getpwuid() function searches the password database using a user identifier (UID).

#include <pwd.h> struct passwd *getpwuid(uid_t uid);

15. Example Using getpwuid()

#include <stdio.h> #include <pwd.h> #include <unistd.h> int main(void) { struct passwd *pw; pw = getpwuid(getuid()); if (pw != NULL) { printf("User : %s\n", pw->pw_name); printf("Home : %s\n", pw->pw_dir); } return 0; }

16. getpwnam()

The getpwnam() function searches the password database using a user name.

#include <pwd.h> struct passwd *getpwnam( const char *name );

17. getpwent()

The getpwent() function sequentially returns entries from the password database. It is commonly used when every user entry needs to be examined.

#include <pwd.h> struct passwd *getpwent(void);

18. Password Database Iteration

Open Password Database ↓ Read Entry ↓ Process Entry ↓ More Entries? ↓ Yes → Read Next Entry ↓ No ↓ Close Database

19. Related Password Database Functions

Function Purpose
setpwent() Rewind the password database.
endpwent() Close the password database.
getpwent() Read the next password entry.
getpwnam() Search by user name.
getpwuid() Search by user ID.

20. Interview Questions

  1. What information does the password database contain?
  2. Why should applications use getpwuid() instead of parsing /etc/passwd?
  3. What does pw_uid represent?
  4. What is stored in pw_shell?
  5. How does getpwnam() differ from getpwuid()?
  6. When is getpwent() useful?
  7. What is the purpose of setpwent()?
  8. Why should endpwent() be called after iteration?
  9. Which structure stores password database information?
  10. What header file declares password database functions?

21. Group Database

The group database stores information about system groups. Each group has a unique group identifier (GID), a group name, and a list of users who belong to that group. Applications access this information using the group database functions provided by the operating system.

22. struct group

Group database functions return information using the struct group structure.

#include <grp.h> struct group { char *gr_name; char *gr_passwd; gid_t gr_gid; char **gr_mem; };

23. Members of struct group

Member Description
gr_name Group name.
gr_passwd Group password field (implementation dependent).
gr_gid Group ID.
gr_mem NULL-terminated array of member names.

24. getgrgid()

The getgrgid() function searches the group database using a group identifier (GID).

#include <grp.h> struct group *getgrgid(gid_t gid);

25. Example Using getgrgid()

#include <stdio.h> #include <grp.h> #include <unistd.h> int main(void) { struct group *grp; grp = getgrgid(getgid()); if (grp != NULL) { printf("Group : %s\n", grp->gr_name); printf("GID : %u\n", (unsigned)grp->gr_gid); } return 0; }

26. getgrnam()

The getgrnam() function searches the group database using a group name.

#include <grp.h> struct group *getgrnam( const char *name );

27. getgrent()

The getgrent() function returns successive entries from the group database. It is commonly used when every group entry must be examined.

#include <grp.h> struct group *getgrent(void);

28. Group Database Iteration

Open Group Database ↓ Read Group Entry ↓ Process Entry ↓ More Entries? ↓ Yes → Read Next Entry ↓ No ↓ Close Database

29. Related Group Database Functions

Function Purpose
setgrent() Rewind the group database.
endgrent() Close the group database.
getgrent() Read the next group entry.
getgrnam() Search by group name.
getgrgid() Search by group ID.

30. Interview Questions

  1. What information does the group database contain?
  2. Why should applications use getgrgid() instead of reading /etc/group directly?
  3. What is stored in gr_gid?
  4. What is the purpose of gr_mem?
  5. How does getgrnam() differ from getgrgid()?
  6. When should getgrent() be used?
  7. What is the purpose of setgrent()?
  8. Why should endgrent() be called?
  9. Which header file declares the group database functions?
  10. Which structure represents a group database entry?

31. Shadow Password Database

Many UNIX and Linux systems store encrypted password information separately from the public password database. This improves system security because ordinary users can read the password database but cannot access encrypted password information. The exact implementation varies between operating systems.

32. Login Accounting Databases

Some UNIX systems maintain login accounting databases that record user login sessions, logout times, and system activity. The location and format of these databases are implementation-specific. Applications should use operating system interfaces instead of directly parsing database files.

33. Reentrant Database Functions

Traditional database lookup functions return pointers to static storage. For multithreaded programs, POSIX provides reentrant versions that store results in user-supplied buffers.

Standard Function Reentrant Version
getpwnam() getpwnam_r()
getpwuid() getpwuid_r()
getgrnam() getgrnam_r()
getgrgid() getgrgid_r()

34. Why Use Reentrant APIs?

  • Safe for multithreaded applications.
  • Avoid shared static storage.
  • Allow multiple concurrent lookups.
  • Reduce race conditions.
  • Improve application reliability.

35. Example Using getpwuid_r()

#include <pwd.h> struct passwd pwd; struct passwd *result; char buffer[4096]; getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &result);

36. Useful Linux Commands

Command Purpose
id Display user and group IDs.
whoami Display current user name.
groups Display group memberships.
getent passwd Display password database entries.
getent group Display group database entries.

37. Complete Database Lookup Workflow

Application ↓ Database API ↓ Operating System ↓ User / Group Database ↓ Requested Entry Found? ↓ Yes → Return Structure ↓ No → Return NULL

38. Best Practices

  • Use standard library functions instead of parsing database files.
  • Check return values for NULL.
  • Use reentrant functions in multithreaded applications.
  • Close sequential database access using endpwent() and endgrent().
  • Do not assume implementation-specific databases exist on every UNIX system.

39. Common Mistakes

  • Reading /etc/passwd directly.
  • Ignoring API return values.
  • Using non-reentrant functions in multithreaded code.
  • Forgetting to terminate database iteration.
  • Assuming identical database formats across UNIX implementations.

40. Interview Questions

  1. Why are shadow password databases used?
  2. Why are shadow databases considered implementation-dependent?
  3. What problem do reentrant database functions solve?
  4. How does getpwuid_r() differ from getpwuid()?
  5. Which functions should be used in multithreaded applications?
  6. What does the id command display?
  7. What information does the groups command provide?
  8. What is the purpose of the getent command?
  9. Why should applications avoid reading database files directly?
  10. Why is checking for NULL important when performing database lookups?

41. Complete Practical Example

#include #include #include #include int main(void) { struct passwd *pw; struct group *grp; pw = getpwuid(getuid()); if (pw == NULL) return 1; grp = getgrgid(pw->pw_gid); if (grp == NULL) return 1; printf("User Name : %s\n", pw->pw_name); printf("UID : %u\n", (unsigned)pw->pw_uid); printf("Home : %s\n", pw->pw_dir); printf("Shell : %s\n", pw->pw_shell); printf("Group : %s\n", grp->gr_name); printf("GID : %u\n", (unsigned)grp->gr_gid); return 0; }

42. Password and Group API Comparison

Password API Group API Purpose
getpwuid() getgrgid() Lookup by numeric ID.
getpwnam() getgrnam() Lookup by name.
getpwent() getgrent() Sequential iteration.
setpwent() setgrent() Restart iteration.
endpwent() endgrent() Close database access.

43. Complete Database Access Lifecycle

Application Starts ↓ Request User / Group Information ↓ Database Lookup Function ↓ Operating System ↓ Database Search ↓ Entry Found? ↓ Yes → Return Structure ↓ No → Return NULL ↓ Application Processes Data ↓ Database Access Ends

44. Key Takeaways

  • Use standard database APIs instead of reading system database files.
  • Password and group databases provide account information.
  • Use lookup functions for specific users or groups.
  • Use iteration functions when processing all entries.
  • Use reentrant APIs in multithreaded programs.
  • Always verify returned pointers before accessing structure members.

45. Advanced Interview Questions

  1. Why does UNIX provide database APIs instead of requiring applications to read database files directly?
  2. Explain the difference between getpwuid() and getpwnam().
  3. When would you use getpwent()?
  4. How does struct passwd differ from struct group?
  5. Why are reentrant functions preferred in multithreaded applications?
  6. What is the purpose of setpwent() and endpwent()?
  7. What happens when a requested user or group does not exist?
  8. Why is the shadow password database considered implementation-specific?
  9. How do password and group database APIs improve portability?
  10. Which APIs are commonly used to obtain the current user's account information?

46. Common Programming Mistakes

  • Dereferencing a NULL pointer returned by a lookup function.
  • Reading implementation-specific database files directly.
  • Using non-reentrant APIs in multithreaded programs.
  • Ignoring failures returned by lookup functions.
  • Forgetting to call endpwent() or endgrent() after sequential access.

47. Module Summary

This module introduced the UNIX system databases used to store user and group information. It covered the password and group database structures, lookup functions, sequential access functions, reentrant interfaces, implementation-dependent databases, practical programming examples, Linux commands, and recommended programming practices. Understanding these interfaces allows applications to retrieve account information in a portable and reliable manner without depending on implementation-specific database formats.

Knowledge Check

Concept Mastery Assessment — Module 31

Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.

Question 1 of 12
What is the primary architectural purpose of System Databases, User Accounts & Authentication in operating systems?
Explanation
System Databases, User Accounts & Authentication is designed to manage password database (/etc/passwd, getpwnam), shadow passwords (/etc/shadow), group database (/etc/group), and crypt() hashing. within the operating system architecture.
Question 2 of 12
Which system interface or data structure is centrally associated with System Databases, User Accounts & Authentication?
Explanation
Under POSIX and Linux, System Databases, User Accounts & Authentication operates through standardized system calls and kernel control structures.
Question 3 of 12
What safety or concurrency constraint must developers consider when implementing System Databases, User Accounts & Authentication?
Explanation
Proper synchronization, avoidance of race conditions, and handling reentrant execution are critical when working with System Databases, User Accounts & Authentication.
Question 4 of 12
When a system call related to System Databases, User Accounts & Authentication fails in C, how does the POSIX runtime conventionally report the failure?
Explanation
Standard POSIX system calls return -1 or NULL on error, assigning an error integer (e.g. EINVAL, EPERM, EAGAIN) to the thread-local errno variable.
Question 5 of 12
What is the primary performance tradeoff associated with System Databases, User Accounts & Authentication?
Explanation
Operations involving System Databases, User Accounts & Authentication balance performance against kernel transition, memory bandwidth, and cache locality tradeoffs.
Question 6 of 12
What security principle governs access to resources in System Databases, User Accounts & Authentication?
Explanation
The kernel validates credentials, effective permissions, and Linux capabilities before granting access to protected operations.
Question 7 of 12
How does execution of System Databases, User Accounts & Authentication transition between User Space and Kernel Space?
Explanation
User space transitions to kernel mode securely via software traps or SYSCALL instructions that switch processor rings to Ring 0.
Question 8 of 12
What happens to resources allocated by System Databases, User Accounts & Authentication when the owning process terminates?
Explanation
Process resources (open fds, memory maps) are automatically reclaimed by the kernel on termination, while kernel-persistent objects survive.
Question 9 of 12
In production Linux server environments, how is System Databases, User Accounts & Authentication optimized for high throughput?
Explanation
Linux optimizes throughput via zero-copy data paths, asynchronous kernel events, and scalable concurrency primitives.
Question 10 of 12
What standard governs the portability of System Databases, User Accounts & Authentication across Unix, Linux, and macOS platforms?
Explanation
IEEE POSIX standards define consistent APIs across Unix-like platforms ensuring portable source code execution.
Question 11 of 12
Which command-line diagnostic tool is most effective for inspecting and troubleshooting System Databases, User Accounts & Authentication at runtime?
Explanation
Utilities like strace, lsof, and the /proc filesystem provide direct visibility into kernel interactions and resource status.
Question 12 of 12
What is a critical production best practice when developing systems software involving System Databases, User Accounts & Authentication?
Explanation
Defensive programming requires explicit error checking on all system operations, proper resource cleanup, and avoiding memory corruption.
0 / 12 (0%)
Great job! Review the concepts above and proceed when ready.
ADVERTISEMENT
Responsive Ad Unit • Technical Courses & Certifications