Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Systems Programming - SoCoded 2013

Systems Programming - SoCoded 2013

Andy Delcambre

September 20, 2013
Tweet

More Decks by Andy Delcambre

Other Decks in Programming

Transcript

  1. operate and control the computer hardware and to provide a

    platform for running application software http://en.wikipedia.org/wiki/System_software “
  2. Things that are Files (Non Exhaustive) Links DVDs Hard Drives

    USB Keys Sockets* Files Directories Sound Cards Graphics Cards Printers Pipes Terminals UNIX Sockets Shared Memory
  3. 0 -> /dev/null 1 -> unicorn.log 2 -> unicorn.log 3

    -> pipe:[3753224456] 4 -> pipe:[3753224456] 5 -> socket:[19730] 6 -> pipe:[3753224457] 7 -> socket:[19731] 8 -> socket:[19732] 9 -> pipe:[3753224457] 10 -> production.log 11 -> socket:[3766066290] 12 -> socket:[3753228795] 13 -> socket:[3753229266] 14 -> socket:[3753229276] 15 -> socket:[3753229285] 16 -> socket:[3753229307] 17 -> socket:[3753235718] 18 -> socket:[3753235910] 19 -> socket:[3753242775] 20 -> socket:[3753242776] 21 -> socket:[3753242778] 22 -> socket:[3753239097] 23 -> socket:[3753242942] 24 -> socket:[3753242944] 25 -> socket:[3753239202] 26 -> socket:[3753239203] 27 -> socket:[3753239204] 28 -> socket:[3753242981] 29 -> socket:[3753242982] 30 -> socket:[3753243929] 31 -> socket:[3753240046] 32 -> socket:[3753245881] 33 -> socket:[3753248970] 34 -> socket:[3753253476] 35 -> pygments-ruby.log 36 -> /dev/null 37 -> pipe:[3753260503] 38 -> pipe:[3753260504] 39 -> socket:[3753344667] 40 -> pipe:[3753260505] 41 -> socket:[3765810413] 42 -> socket:[3765810440] 43 -> socket:[3765848228] $ ls -l /proc/32660/fd
  4. HTTP Response HTTP/1.1 200 OK Date: Tue, 05 Mar 2013

    23:03:52 GMT Cache-Control: private, max-age=0 Content-Type: text/html Server: gws Transfer-Encoding: chunked <data>
  5. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end
  6. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end Require the socket lib
  7. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end Some constants for later
  8. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end socket = Socket.new(:INET, :STREAM)
  9. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr)
  10. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end socket.listen(5)
  11. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end while client_socket = socket.accept[0]
  12. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end path = client_socket.readline.split[1]
  13. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end path = client_socket.readline.split[1] GET /index.html HTTP/1.1
  14. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end filename = File.expand_path("../#{path}", __FILE__)
  15. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end if File.file?(filename)
  16. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}")
  17. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") OK = "HTTP/1.1 200 OK\n"
  18. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end client_socket.write(NOT_FOUND)
  19. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end client_socket.write(NOT_FOUND) NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
  20. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end client_socket.close
  21. require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"

    OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end
  22. def new_ipv6_server(addr, port, opt) opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port) defined?(IPV6_V6ONLY)

    or abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS" sock = Socket.new(AF_INET6, SOCK_STREAM, 0) sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(Socket.pack_sockaddr_in(port, addr)) IO_PURGATORY << sock Kgio::TCPServer.for_fd(sock.fileno) end ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0] Unicorn
  23. def new_ipv6_server(addr, port, opt) opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port) defined?(IPV6_V6ONLY)

    or abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS" sock = Socket.new(AF_INET6, SOCK_STREAM, 0) sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(Socket.pack_sockaddr_in(port, addr)) IO_PURGATORY << sock Kgio::TCPServer.for_fd(sock.fileno) end ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0] Unicorn sock = Socket.new(AF_INET6, SOCK_STREAM, 0)
  24. def new_ipv6_server(addr, port, opt) opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port) defined?(IPV6_V6ONLY)

    or abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS" sock = Socket.new(AF_INET6, SOCK_STREAM, 0) sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(Socket.pack_sockaddr_in(port, addr)) IO_PURGATORY << sock Kgio::TCPServer.for_fd(sock.fileno) end ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0] Unicorn sock.bind( Socket.pack_sockaddr_in(port, addr))
  25. def new_ipv6_server(addr, port, opt) opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port) defined?(IPV6_V6ONLY)

    or abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS" sock = Socket.new(AF_INET6, SOCK_STREAM, 0) sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(Socket.pack_sockaddr_in(port, addr)) IO_PURGATORY << sock Kgio::TCPServer.for_fd(sock.fileno) end ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0] Unicorn IO.select(l, nil, SELF_PIPE, @timeout)