https://code.google.com/p/nsscache/wiki/BackgroundOnNameServiceSwitch

The POSIX API

POSIX is a standard that defines an operating system interface and its environment; describing available library calls, utilities, environment vars, escape sequences, regexps, when to take coffee breaks (aka how long your code takes to compile), etc.

GNU/Linux is (generally) POSIX compliant.

The relevant component of POSIX is the definition of function calls to access directory information -- databases of people/groups/hosts/etc.

Here are some examples of the POSIX API functions, the method in which applications access information in the the system databases.

  • get*nam() -> get a database entry by its human readable name
  • get*id() -> get a database entry by its computer readable name
  • get*ent() -> get the next entry in a database; a mechanism for iterating over the entire database

(The asterisk replaces the short name of the database being accessed.)

These functions get called all the time, for example:

  • at login (to find out who you are and what your groups are)
  • ls -l (mapping uid/gid of a file to username/group)
  • resolving hostnames to IP addresses
  • many others: NIS netgroups, automount locations, rpc names, TCP and UDP protocol names

It doesn't matter for the most part that these API calls are made all the time, because when the API was designed, the database that stored this information is a plain text file on the local machine, and accessing that is both fast and 100% reliable (ignoring of course hardware issues on the local machine, at which point you have bigger problems :-)

As we got bigger networks and lots of shared computing infrastructure, we moved to directory services. /etc/hosts stopped scaling, so we got DNS, and it all went downhill from there.

System administrators wanted to get the system databases from other sources like NIS, NIS+, LDAP, Hesiod (gag), DNS, etc. To facilitate that, you want to allow easy runtime configuration changes, i.e. different types of data may need to be stored in different places -- users in/etc/passwd versus hosts in DNS.

First implemented by Sun, this was dubbed the name service switch, or NSS for short.

The Name Service Switch

Perhaps you're familiar with the Name Service Switch configuration file, /etc/nsswitch.conf:

  1. passwd: compat files
    group: compat files
    shadow: compat files
    hosts: files dns

On the right hand side of the colon are the data sources, where NSS will go to retrieve the system database. It progresses left to right, checking each source in turn until the data is found.

On the left hand side of the colon, the groupings of data, the database itself, which we are calling "maps" -- in this example, the passwd database API functions are mapped to the "compat" and "files" data sources.

For our own convenience, this document will refer to both the POSIX API described above, and the GNU libc implementation of the Name Service Switch as both "NSS".

  1. # /etc/nsswitch.conf
    passwd: files

When an NSS function is called, the NSS implementation reads its configuration file /etc/nsswitch.conf, which names the library that implements the data retrieval. NSS dynamically loads this library, in this example, libnss_files.so. The correct function within this library is then called, for example _nss_files_getpwuid().

libnss_files then opens and parses /etc/passwd, and returns (typically a struct).

NSS + RFC 2307 LDAP

  1. # /etc/nsswitch.conf
    passwd: files ldap

Add in a directory service, and you get a situation familiar to many sysadmins. /etc/nsswitch.conf would now also list ldap in addition to filesin this example.

If NSS were to load libnss_files.so, and find nothing, it would then load libnss_ldap.solibnss_ldap.so would make a network connection to the LDAP server, perform a query, and convert the LDAP results into the right return structure.

This means that every query will translate into a TCP connection with handshake overhead, possibly over SSL with its crypto overhead, and then do various ASN.1 and BER en- and decodings within the LDAP protocol itself...

Name Service Cache Daemon

So we also typically run a caching daemon, provided by GNU libc, called nscd.

It's accessed via a UNIX socket, and though poorly demonstrated by this diagram, loads the nss modules itself in order to act as a hit-and-miss cache.

It has several threads to that it can respond to several requests at the same time.

If the cache has the response, it returns it straight away. If not, it dlopens the NSS module, e.g. libnss_ldap.so, waits for the reply, caches it, and then returns it.

The POSIX API/nss/nscd的更多相关文章

  1. 消息队列接口API(posix 接口和 system v接口)

    消息队列 posix API 消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点.信号这种通信方式更像\"即时\"的通信方式,它要求接受信号的进程在某个时间范围内对信 ...

  2. API 设计 POSIX File API

    小结: 1. https://mp.weixin.qq.com/s/qWrSyzJ54YEw8sLCxAEKlA API 设计最佳实践的思考 谷朴 阿里技术 昨天   阿里妹导读:API 是模块或者子 ...

  3. system v和posix的共享内存对比 & 共享内存位置

    参考 http://www.startos.com/linux/tips/2011012822078.html 1)Linux和所有的UNIX操作系统都允许通过共享内存在应用程序之间共享存储空间. 2 ...

  4. VxWorks 6.9 内核编程指导之读书笔记 -- POSIX

    POSIX能力 VxWorks扩展了POSIX,为了移植,VxWorks提供了额外的POSIX接口作为可选组件.VxWorks实现了POSIX 1003.1(POSIX .1)一些传统接口以及POSI ...

  5. Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)

    文章目录:                   1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...

  6. [转] 阿里研究员谷朴:API 设计最佳实践的思考

    API是软件系统的核心,而软件系统的复杂度Complexity是大规模软件系统能否成功最重要的因素.但复杂度Complexity并非某一个单独的问题能完全败坏的,而是在系统设计尤其是API设计层面很多 ...

  7. Xenomai 3 POSIX

    Xenomai 3在架构设计上确实优先Xenomai 2,至少对开发者来说,少维护了不少东西,看下面两张图就知道了 第一张图是Xenomai2的,第二张图是Xenomai3的,Xenomai3在内核中 ...

  8. mingw-w64线程模型:posix vs win32(posix允许使用c++11的std:: thread,但要带一个winpthreads,可能需要额外dll)

    我正在安装 mingw-w64 on Windows,有两个选项: win32线程和posix线程. 我知道win32线程和pthreads之间的区别,但是我不明白这两个选项之间的区别. 我怀疑如果我 ...

  9. MingGW Posix VS Win32 - 明瓜娃的毒因

    MinGW-posix和win32纠缠的瓜娃子 官方首席佛偈(SourceForge)的官网下载页 法克油啊,让我一个小白情何以堪. 盘TA wiki posix wiki中文-UNIX API标准 ...

随机推荐

  1. +new Date()的用法

    var s=+newDate();   var s=+newDate(); 解释如下:=+是不存在的; +new Date()是一个东西; +相当于.valueOf(); 看到回复补充一下.getTi ...

  2. 2018年第九届蓝桥杯【C++省赛B组】

    2标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛.16点阵的字库把每个汉字看成是16x16个像素信息.并把这些信息记录在字节中. 一个字节可以存储8位信息,用32个字节就 ...

  3. cnblogs.com用户体验

    一.是否提供了良好的体验给用户(同时提供价值)? 首先我觉得博客园给我们这些用户提供了良好的用户体验,博客园提供了一个纯净的技术交流空间,在这里我们可以找到几乎所有与IT技术有关的博文,而且可以在这里 ...

  4. 读书笔记 之java编程思想

    本阶段我正在读java的编程思想这本书,这本书只是刚读了第一章的一部分,有些有些要记得所以记录下来, 我认为要记得有就是复用这样可以对对象进行增强,将一个类作为下一个类中基本类型,这样达到的服用的目的 ...

  5. java编程的一些注意事项

    下面是参考网络资源和总结一些在java编程中尽可能做到的一些地方 1.尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例 ...

  6. Week2-作业1:阅读与博客

    Week2-作业1:阅读与博客 第一章 :概论 1. 原文如下: 移山公司程序员阿超的宝贝儿子上了小学二年级,老师让家长每天出30道加减法题目给孩子做.阿超想写一个小程序来做这件事,具体实现可以采用很 ...

  7. ps -aux 命令下的前几行内容解释 与 top命令下的前几行内容解释

    系统进程分为5种不同的状态: R(运行):正在运行或在运行队列中等待 S(中断):休眠中,在等待某个条件的形成或接受到信号 D(不可中断):收到信号不唤醒和不可运行.进程必须等待直到有中断发生 Z(僵 ...

  8. 关于对JSON.parse()与JSON.stringify()的理解

    JSON.parse()与JSON.stringify()的区别   JSON.parse()[从一个字符串中解析出json对象] 例子: //定义一个字符串 var data='{"nam ...

  9. HBase 架构与工作原理5 - Region 的部分特性

    本文系转载,如有侵权,请联系我:likui0913@gmail.com Region Region 是表格可用性和分布的基本元素,由列族(Column Family)构成的 Store 组成.对象的层 ...

  10. windows多线程(十) 生产者与消费者问题

    一.概述 生产者消费者问题是一个著名的线程同步问题,该问题描述如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个具有多个缓冲区的缓冲池 ...