uid-filter.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. *
  3. * Copyright (c) 2003 The Regents of the University of California. All
  4. * rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * - Neither the name of the University nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  20. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * FUSD - The Framework for UserSpace Devices - Example program
  32. *
  33. * Jeremy Elson <jelson@circlemud.org>
  34. *
  35. * uid-filter. This program shows how you can use some of the
  36. * meta-data provided in the fusd_file_info structure to affect your
  37. * driver's behavior.
  38. *
  39. * In particular, this driver creates a device, /dev/my-pid, that can
  40. * not be read by anyone other than the driver owner (not even root!).
  41. * When you read from the device, it returns your PID to you.
  42. *
  43. * $Id$
  44. */
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <errno.h>
  48. #include <unistd.h>
  49. #include "fusd.h"
  50. int do_close(struct fusd_file_info *file)
  51. {
  52. return 0; /* attempts to close the file always succeed */
  53. }
  54. /* EXAMPLE START uid-filter.c */
  55. int do_open(struct fusd_file_info *file)
  56. {
  57. /* If the UID of the process trying to do the read doesn't match the
  58. * UID of the owner of the driver, return -EPERM. If you run this
  59. * driver as a normal user, even root won't be able to read from the
  60. * device file created! */
  61. if (file->uid != getuid())
  62. return -EPERM;
  63. return 0;
  64. }
  65. ssize_t do_read(struct fusd_file_info *file, char *user_buffer,
  66. size_t user_length, loff_t *offset)
  67. {
  68. char buf[128];
  69. int len;
  70. /* The first read to the device returns a greeting. The second read
  71. * returns EOF. */
  72. if (*offset != 0)
  73. return 0;
  74. /* len gets set to the number of characters written to buf */
  75. len = sprintf(buf, "Your PID is %d. Have a nice day.\n", file->pid);
  76. /* NEVER return more data than the user asked for */
  77. if (user_length < len)
  78. len = user_length;
  79. memcpy(user_buffer, buf, len);
  80. *offset += len;
  81. return len;
  82. }
  83. /* EXAMPLE STOP uid-filter.c */
  84. int main(int argc, char *argv[])
  85. {
  86. struct fusd_file_operations fops = {
  87. open: do_open,
  88. read: do_read,
  89. close: do_close };
  90. if (fusd_register("/dev/my-pid", "misc", "my-pid", 0666, NULL, &fops) < 0)
  91. perror("Unable to register device");
  92. else {
  93. printf("/dev/my-pid should now exist - calling fusd_run...\n");
  94. fusd_run();
  95. }
  96. return 0;
  97. }