- again:
- /* try to use an existing key */
- shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
-
- /* if that failed then create one. There is a race condition here
- if we are running from inetd. Bad luck. */
- if (shm_id == -1) {
- if (read_only) return False;
- shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
- IPC_CREAT | IPC_EXCL | IPC_PERMS);
- }
-
- if (shm_id == -1) {
- DEBUG(0,("Can't create or use IPC area. Error was %s\n",
- strerror(errno)));
- return False;
- }
-
-
- profile_h = (struct profile_header *)shmat(shm_id, 0,
- read_only?SHM_RDONLY:0);
- if ((long)profile_p == -1) {
- DEBUG(0,("Can't attach to IPC area. Error was %s\n",
- strerror(errno)));
- return False;
- }
-
- /* find out who created this memory area */
- if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
- DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
- strerror(errno)));
- return False;
- }
-
- if (shm_ds.shm_perm.cuid != sec_initial_uid() ||
- shm_ds.shm_perm.cgid != sec_initial_gid()) {
- DEBUG(0,("ERROR: we did not create the shmem "
- "(owned by another user, uid %u, gid %u)\n",
- shm_ds.shm_perm.cuid,
- shm_ds.shm_perm.cgid));
- return False;
- }
-
- if (shm_ds.shm_segsz != sizeof(*profile_h)) {
- DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
- (int)shm_ds.shm_segsz, sizeof(*profile_h)));
- if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
- goto again;
- } else {
- return False;
- }
- }
-
- if (!read_only && (shm_ds.shm_nattch == 1)) {
- memset((char *)profile_h, 0, sizeof(*profile_h));
- profile_h->prof_shm_magic = PROF_SHM_MAGIC;
- profile_h->prof_shm_version = PROF_SHM_VERSION;
- DEBUG(3,("Initialised profile area\n"));
- }
-
- profile_p = &profile_h->stats;
- message_register(MSG_PROFILE, profile_message);
- message_register(MSG_REQ_PROFILELEVEL, reqprofile_message);
- return True;
+ pid = getpid();
+ ret = tdb_chainlock(smbprofile_state.internal.db->tdb, key);
+ if (ret != 0) {
+ return;
+ }
+ tdb_parse_record(smbprofile_state.internal.db->tdb,
+ key, profile_stats_parser, &acc);
+
+ /*
+ * We may have to fix the disconnect count
+ * in case the process died
+ */
+ s.values.disconnect_stats.count = s.values.connect_stats.count;
+
+ smbprofile_stats_accumulate(&acc, &s);
+
+ acc.magic = profile_p->magic;
+ tdb_store(smbprofile_state.internal.db->tdb, key,
+ (TDB_DATA) {
+ .dptr = (uint8_t *)&acc,
+ .dsize = sizeof(acc)
+ },
+ 0);
+
+ tdb_chainunlock(smbprofile_state.internal.db->tdb, key);
+}
+
+void smbprofile_stats_accumulate(struct profile_stats *acc,
+ const struct profile_stats *add)
+{
+#define SMBPROFILE_STATS_START
+#define SMBPROFILE_STATS_SECTION_START(name, display)
+#define SMBPROFILE_STATS_COUNT(name) do { \
+ acc->values.name##_stats.count += add->values.name##_stats.count; \
+} while(0);
+#define SMBPROFILE_STATS_TIME(name) do { \
+ acc->values.name##_stats.time += add->values.name##_stats.time; \
+} while(0);
+#define SMBPROFILE_STATS_BASIC(name) do { \
+ acc->values.name##_stats.count += add->values.name##_stats.count; \
+ acc->values.name##_stats.time += add->values.name##_stats.time; \
+} while(0);
+#define SMBPROFILE_STATS_BYTES(name) do { \
+ acc->values.name##_stats.count += add->values.name##_stats.count; \
+ acc->values.name##_stats.time += add->values.name##_stats.time; \
+ acc->values.name##_stats.idle += add->values.name##_stats.idle; \
+ acc->values.name##_stats.bytes += add->values.name##_stats.bytes; \
+} while(0);
+#define SMBPROFILE_STATS_IOBYTES(name) do { \
+ acc->values.name##_stats.count += add->values.name##_stats.count; \
+ acc->values.name##_stats.time += add->values.name##_stats.time; \
+ acc->values.name##_stats.idle += add->values.name##_stats.idle; \
+ acc->values.name##_stats.inbytes += add->values.name##_stats.inbytes; \
+ acc->values.name##_stats.outbytes += add->values.name##_stats.outbytes; \
+} while(0);
+#define SMBPROFILE_STATS_SECTION_END
+#define SMBPROFILE_STATS_END
+ SMBPROFILE_STATS_ALL_SECTIONS
+#undef SMBPROFILE_STATS_START
+#undef SMBPROFILE_STATS_SECTION_START
+#undef SMBPROFILE_STATS_COUNT
+#undef SMBPROFILE_STATS_TIME
+#undef SMBPROFILE_STATS_BASIC
+#undef SMBPROFILE_STATS_BYTES
+#undef SMBPROFILE_STATS_IOBYTES
+#undef SMBPROFILE_STATS_SECTION_END
+#undef SMBPROFILE_STATS_END
+}
+
+static int smbprofile_collect_fn(struct tdb_context *tdb,
+ TDB_DATA key, TDB_DATA value,
+ void *private_data)
+{
+ struct profile_stats *acc = (struct profile_stats *)private_data;
+ const struct profile_stats *v;
+
+ if (value.dsize != sizeof(struct profile_stats)) {
+ return 0;
+ }
+
+ v = (const struct profile_stats *)value.dptr;
+
+ if (v->magic != profile_p->magic) {
+ return 0;
+ }
+
+ smbprofile_stats_accumulate(acc, v);
+ return 0;
+}
+
+void smbprofile_collect(struct profile_stats *stats)
+{
+ *stats = (struct profile_stats) {};
+
+ if (smbprofile_state.internal.db == NULL) {
+ return;
+ }
+
+ tdb_traverse_read(smbprofile_state.internal.db->tdb,
+ smbprofile_collect_fn, stats);